好吧,所以我今天早些時候開始這個程序,以實踐我迄今爲止學到的東西。我幾乎完成了它,除了我無法在中間獲得這個聲明來爲我工作。對於數學運算中的選擇,我希望如果用戶選擇一個我沒有的選項,它會告訴他它不是一個選項並關閉/重新啓動,但由於某種原因,else語句在它說明它不是無論我選擇實際操作,操作。這是我的代碼。第一天程序員其他語句爲計算器程序
using System;
class Program
{
//varibale for do-while loop
private static string endAnswer;
public static void Main() // <----- The Entry point
{
//Variables
string Choice1;
string mathChoice;
int Num1;
int Num2;
int Answer;
Console.WriteLine("Would you like to use Lane's Custom Calculator? ");
Choice1 = Console.ReadLine();
if (Choice1 == "Yes")
{
do
{
Console.WriteLine("Would you like to Add, Subtract, Multiply, or Divide? (Case Sensitive)");
mathChoice = Console.ReadLine();
//User inputs the 2 numbers
//Math Choices
if (mathChoice == "Add")
{
Console.WriteLine("What 2 numbers would you like to use?");
Console.Write("Number 1 is: ");
Num1 = int.Parse(Console.ReadLine());
Console.Write("Number 2 is: ");
Num2 = int.Parse(Console.ReadLine());
Answer = Num1 + Num2;
Console.WriteLine("The answer is: " + Answer);
}
if (mathChoice == "Subtract")
{
Console.WriteLine("What 2 numbers would you like to use?");
Console.Write("Number 1 is: ");
Num1 = int.Parse(Console.ReadLine());
Console.Write("Number 2 is: ");
Num2 = int.Parse(Console.ReadLine());
Answer = Num1 - Num2;
Console.WriteLine("The answer is: " + Answer);
}
if (mathChoice == "Multiply")
{
Console.WriteLine("What 2 numbers would you like to use?");
Console.Write("Number 1 is: ");
Num1 = int.Parse(Console.ReadLine());
Console.Write("Number 2 is: ");
Num2 = int.Parse(Console.ReadLine());
Answer = Num1 * Num2;
Console.WriteLine("The answer is: " + Answer);
}
if (mathChoice == "Divide")
{
Console.WriteLine("What 2 numbers would you like to use?");
Console.Write("Number 1 is: ");
Num1 = int.Parse(Console.ReadLine());
Console.Write("Number 2 is: ");
Num2 = int.Parse(Console.ReadLine());
Answer = Num1/Num2;
Console.WriteLine("The answer is: " + Answer);
}
//This is the else statement that is giving me problems!!
else
{
Console.WriteLine("This is not an option! Shutting Down..");
Console.ReadKey();
Environment.Exit(0);
}
//varibale for while loop to continue if selected Yes.
Console.WriteLine("Another Equation? ");
endAnswer = Console.ReadLine();
} while (endAnswer == "Yes");
//Goodbye Message
Console.WriteLine("Thank you for using my program, goodbye ");
Console.ReadKey();
Environment.Exit(0);
}
**//If someone selects no for wanting to use my program.
else
{
Console.WriteLine("Not and option...exiting Program...");
Console.ReadKey();
Environment.Exit(0);
}**
}
}
而不是多個If語句,請嘗試Switch語句。這個比較好。 – Bej
在調試器中逐步執行代碼... –
或者將您的第二,第三和第四個'if'語句更改爲'else if' – Deolus