c# - 我是编程新手,正在努力解决 C# 中的一个简单的咖啡任务
问题描述
我刚开始做一些 C# 并给自己制作一个简单的基于控制台的咖啡店程序的任务,它只是简单地列出你想要的东西,然后当你把它全部输入时将它输出给你。我唯一的方法到目前为止发现对我来说效果很好的是这样做:
string Tea = "tea";
string Option1 = Console.ReadLine();
if ((Option1 == Tea)
{
Console.Clear();
//Confirmation Text
Console.WriteLine("So you want a " + Option1 + ".");
//Second Question
Console.WriteLine("Do you want milk, sugar, both or none?");
//Second Option
string Option2 = Console.ReadLine();
//Milk
if (string.Equals(Milk, Option2))
{
Console.Clear();
//Confirmation Text
Console.WriteLine("So you want a " + Option1 + " with " + Option2 + ".");
//Third Question
Console.WriteLine("And so what size will that be, small, medium, or large?");
//Third Option
string Option3 = Console.ReadLine();
if (string.Equals(Large, Option3))
{
Console.Clear();
//Last Confirmation
Console.WriteLine("Here is your " + Option3 + " " + Option1 + " with " + Option2 + ".");
Console.ReadKey();
}
else if (string.Equals(Medium, Option3))
{
Console.Clear();
//Last Confirmation
Console.WriteLine("Here is your " + Option3 + " " + Option1 + " with " + Option2 + ".");
Console.ReadKey();
}
else if (string.Equals(Small, Option3))
{
Console.Clear();
//Last Confirmation
Console.WriteLine("Here is your " + Option3 + " " + Option1 + " with " + Option2 + ".");
Console.ReadKey();
}
else Console.WriteLine("Sorry that is not one of our sizes.");
}
但这似乎太多的代码无法完成一项简单的任务,因为我不得不一遍又一遍地重复事情。我试图查找其他方法,但我似乎无法理解您如何将它们实施到我正在尝试做的事情中。并且实现循环也是一个目标,但我也无法弄清楚。
解决方案
通常在软件开发中,如果您一遍又一遍地重用相同的代码,您应该将该代码移动到一个方法中。
我的以下解决方案远非完美,因为我不得不使用静态方法,因为我必须访问我从静态本身的“主”类编写的方法。对我来说,创建一个类来处理逻辑并实例化该类会好得多。
考虑到您说您是编程新手,我避免做任何可能令人困惑的事情,而是尝试在可能的情况下重用代码,只是为了演示代码重用。
这是我对您的问题的解决方案:
using System;
class MainClass {
public static void Main (string[] args) {
string Option1 = Console.ReadLine();
if (Option1 == "Tea")
{
string[] questionSet1 = { "So you want a " + Option1 + ".",
"Do you want milk, sugar, both or none?"};
//Confirmation Text
string Option2 = PrintTextArray(questionSet1);
//Milk
if (Option2.Equals("Milk"))
{
string[] questionSet2 = { "So you want a " + Option1 + " with " + Option2 + ".",
"And so what size will that be, small, medium, or large?"};
//Confirmation Text
string Option3 = PrintTextArray(questionSet2);
if (Option3.Equals("Large"))
{
//Last Confirmation
PrintText("Here is your " + Option3 + " " + Option1 + " with " +
Option2 + ".");
}
else if (Option3.Equals("Medium"))
{
//Last Confirmation
PrintText("Here is your " + Option3 + " " + Option1 + " with " + Option2 + ".");
}
else if (Option3.Equals("Small"))
{
//Last Confirmation
PrintText("Here is your " + Option3 + " " + Option1 + " with " + Option2 + ".");
}
else Console.WriteLine("Sorry that is not one of our sizes.");
}
}
}
private static string PrintText(string textToPrint)
{
string[] textListToPrint = { textToPrint };
return PrintTextArray(textListToPrint);
}
private static string PrintTextArray(string[] textListToPrint)
{
Console.Clear();
for(int i = 0; i < textListToPrint.Length; i++)
{
Console.WriteLine(textListToPrint[i]);
}
return Console.ReadLine();
}
}
基本上我写了两个方法,其中一个是重载方法,它接受一个字符串或字符串数组,将它们打印出来并返回下一个按键。
我不会坐下来假装这是完美的解决方案,因为根据单一职责原则,一个方法应该只做一件事,但是,再次考虑到您是编程新手,我试图避免过度复杂化。我强烈建议学习SOLID 原则。
最后,有几个指针,在 C# 中,方法的名称应以大写字母开头声明,变量应使用驼峰式大小写约定声明。在此处查找有关骆驼案的更多信息
推荐阅读
- javascript - nodejs express 错误 TS2531:对象可能为“空”
- python - Tkinter - 如何更改默认笔记本边框颜色?
- windows - 如果超过一分钟的 Windows 批处理文件,则退出循环
- flutter - firebase 消息传递和flutter sdk 驱动程序版本解决失败
- python - 发送 ctrl+c 按键需要 0.5 秒才能运行。如何加快速度?
- mysql - 如果数据类型是 Char(1),如何处理 Mysql 中文本文件中的 null?
- android - FaceDetector 没有通过 FaceDetector.release() 释放
- apache-kafka - 卡夫卡生产者保证
- r - 如何使用 ggplot2 在 R 中为多个 y 系列制作线性拟合线?
- java - 有没有办法将鼠标坐标移动到 JFrame 的标题?