我对编程完全陌生,我的前奏CS课的教授给了我们一个项目,该项目使用c#创建菜单,同时可以选择2种游戏。我决定去玩数学游戏,但是我遇到了一个问题,我的部分代码无法读取,我想知道为什么吗?
private static void FindTheAnswer()
{
Console.WriteLine("Find the Answer Math Game");
Console.WriteLine("1. 12x = 3");
Console.WriteLine("Choose the Answer: ");
Console.WriteLine("1) 1/4 ");
Console.WriteLine("2) 4 ");
Console.WriteLine("3) 12x-3 ");
Console.WriteLine("1) 3-12x");
string userValue = Console.ReadLine();
if (userValue =="1/4")
// ...
}
我希望程序会问我这些行:
“寻找答案数学游戏”
“ 1。12x = 3”
“选择答案:”
但是,它最多只能读取“ 1. 12x = 3”,而无论我从输入的代码中输入的答案是什么,都不会读取,并且会不断重复菜单。
我拥有完整的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MainGameMenu
{
class Program
{
static void Main(string[] args)
{
//when you do not know how many times to itterate upfront
bool displayMenu = true;
while (displayMenu == true)
{
displayMenu = MainMenu();
}
}
private static bool MainMenu()
{
Console.WriteLine("Choose an option: ");
Console.WriteLine("1) Option 1");
Console.WriteLine("2) Option 2");
Console.WriteLine("3) Exit");
string result = Console.ReadLine();
if (result == "1")
{
FindTheAnswer();
return true;
}
if (result == "2")
{
FindTheLoot();
return true;
}
if (result == "3")
{
return false;
}
else
{
return true;
}
}
private static void FindTheAnswer()
{
Console.WriteLine("Find the Answer Math Game");
Console.WriteLine("1. 12x = 3");
Console.WriteLine("Choose the Answer: ");
Console.WriteLine("1) 1/4 ");
Console.WriteLine("2) 4 ");
Console.WriteLine("3) 12x-3 ");
Console.WriteLine("1) 3-12x");
string userValue = Console.ReadLine();
if (userValue == "1/4")
{
string message = "The Answer is Correct!";
Console.WriteLine(message);
}
else if (userValue == "4")
{
string message = "Incorrect Answer";
Console.WriteLine(message);
}
else if (userValue == "12x-3")
{
string message = "Incorret Answer";
Console.WriteLine(message);
}
else if (userValue == "3-12x")
{
string message = "Incorrect Answer";
Console.WriteLine(message);
}
else
{
string message = "Sorry, We didn't understand.";
Console.WriteLine(message);
}
Console.ReadLine();
}
private static void FindTheLoot()
{
Console.WriteLine("Select the Number that Matches");
Console.ReadLine();
}
}
}
答案 0 :(得分:0)
这对我有用
Console.WriteLine("Find the Answer Math Game");
Console.WriteLine("1. 12x = 3");
Console.WriteLine("Choose the Answer: ");
Console.WriteLine("1) 1/4 ");
Console.WriteLine("2) 4 ");
Console.WriteLine("3) 12x-3 ");
Console.WriteLine("1) 3-12x");
string userValue = Console.ReadLine();
if (userValue =="1/4")
Console.WriteLine("correct");
else
Console.WriteLine("wrong");