我试图在回答问题后使用户输入“ 1”继续,并使用代码中的上一个循环显示下一个问题。我该怎么办?
string a = "x²";
var rand = new Random();
int num1 = 0;
int num2 = 0;
int b = rand.Next(-100, 100);
int c = rand.Next(-100, 100);
while (!((num1 + num2 == b) && (num1 * num2 == c)))
{
b = rand.Next(-50, 50);
c = rand.Next(-50, 50);
num1 = rand.Next(-50, 50);
num2 = rand.Next(-50, 50);
}
Console.WriteLine("{0} {1} {2}", a, b, c);
int x = int.Parse(Console.ReadLine());
int y = int.Parse(Console.ReadLine());
if ((x + y == b) && (x * y == c))
{
Console.WriteLine("Correct!");
Console.WriteLine("Continue? 1 for yes 0 for no");
int next = int.Parse(Console.ReadLine());
if ((next == 1) || (next == 0))
{
if (next == 1)
{
//display another trinomial question
}
else
{
//close application
}
}
else
{
Console.WriteLine("You must input either a 1 or a 0");
}
}
else
{
Console.WriteLine("Wrong. Try Again");
x = int.Parse(Console.ReadLine());
y = int.Parse(Console.ReadLine());
}
我刚刚开始学习c#,但我不知道如何执行这些功能,我试图在网上查找它,但找不到任何解决方案。谢谢你然后 欢迎所有答案
答案 0 :(得分:0)
首先我无法理解该程序的逻辑,但是我认为这就是您想要的。
我做什么:
我分开了继续检查。我创建了一个do {} while (condition)
循环(您可以了解更多here),在这里我使用从Continue()
方法返回的值。如果用户用true
响应,则此方法返回1
;如果响应是false
,则返回0
。当该值为true时,循环运行。在该循环中,在另一个用于测试用户答案的循环之后,它将显示问题。如果用户的答案不正确,那么它将再次询问,直到用户以正确的答案做出响应。之后它将要求继续。
namespace SO_Test
{
class Program
{
const string a = "x²";
static void Main(string[] args)
{
do
{
//Ask question
var rand = new Random();
int num1 = 0;
int num2 = 0;
int b = rand.Next(-100, 100);
int c = rand.Next(-100, 100);
while (!((num1 + num2 == b) && (num1 * num2 == c)))
{
b = rand.Next(-50, 50);
c = rand.Next(-50, 50);
num1 = rand.Next(-50, 50);
num2 = rand.Next(-50, 50);
}
Console.WriteLine("{0} {1} {2}", a, b, c);
//Test answer
int x;
int y;
do
{
x = int.Parse(Console.ReadLine());
y = int.Parse(Console.ReadLine());
if (!((x + y == b) && (x * y == c)))
Console.WriteLine("Wrong. Try Again");
}
while (!((x + y == b) && (x * y == c))) ;
}
while (Continue()) ; //Ask the user to continue
Console.WriteLine("Press any key to exit");
Console.Read(); //Prevent auto closing
}
private static bool Continue()
{
int next; //Declare variable
Console.WriteLine("Correct!");
Console.WriteLine("Continue? 1 for yes 0 for no");
while (!new[] { 1, 0 }.Contains(next = int.Parse(Console.ReadLine())))
{
Console.WriteLine("You must input either a 1 or a 0"); //Print this line until the user input is 1 or 0
}
if (next == 1)
return true; //Continue? Yes!
else
return false; //Continue? No.
}
}
}
答案 1 :(得分:0)
因此,函数/方法是一段代码,通常执行一个小的但可重复的任务。它可以根据您指定的参数获取参数和/或返回某些值。函数始终以相同的方式声明;
[accessibility] [return type] [name] ([parameters]) ie:
public/private void/int/string doSomething(int x, int y){
//Do the logic
return somethingThatsOfYourReturnType
}
在您的情况下,您需要多个返回值。执行此操作的方法之一是使用关键字out。
public void createQuestion(out int x, out int y);
out关键字表示该函数将设置x和y的值,从而使您可以返回多个值。
通过一些重组,您可能会得到以下内容:
using System;
public class Program
{
public static void Main()
{
int x = 0;
int y = 0;
while (true)
{
//Call create question function
createQuestion(out x, out y); //out means that the function will set these numbers
bool answeredCorrectly = false;
while (!answeredCorrectly) // ! means not
{
Console.WriteLine("Printing Question {0} {1}", x, y);
var inputX = Console.ReadLine();
var inputY = Console.ReadLine();
if (x.ToString() == inputX && y.ToString() == inputY)
{
answeredCorrectly = true;
}
else
{
Console.WriteLine("Try Again");
}
}
Console.WriteLine("Correct!");
Console.WriteLine("Continue? 1 for yes 0 for no");
int next = int.Parse(Console.ReadLine());
while (next != 1 && next != 0)
{
Console.WriteLine("Sorry must be 1 or 0");
next = int.Parse(Console.ReadLine());
}
if (next == 1)
{
continue;
}
else if (next == 0)
{
return;
}
}
}
//definition of create question function
public static void createQuestion(out int x, out int y)
{
//Do the math magic
x = 1;
y = 2;
}
}
答案 2 :(得分:0)
我认为这是解决您问题的最简单方法:
static void Quiz(int b, int c)
{
int x = int.Parse(Console.ReadLine());
int y = int.Parse(Console.ReadLine());
if ((x + y == b) && (x * y == c))
{
Console.WriteLine("Correct!");
Console.WriteLine("Continue? 1 for yes 0 for no");
int next = int.Parse(Console.ReadLine());
if ((next == 1) || (next == 0))
{
if (next == 1)
{
Console.WriteLine("tutaj powt");
}
else
{
return;
}
}
else
{
Console.WriteLine("You must input either a 1 or a 0");
}
}
else
{
Console.WriteLine("Wrong. Try Again");
x = int.Parse(Console.ReadLine());
y = int.Parse(Console.ReadLine());
}
}
static void Main(string[] args)
{
string a = "x²";
var rand = new Random();
int num1 = 0;
int num2 = 0;
int b = rand.Next(-100, 100);
int c = rand.Next(-100, 100);
while (!((num1 + num2 == b) && (num1 * num2 == c)))
{
b = rand.Next(-50, 50);
c = rand.Next(-50, 50);
num1 = rand.Next(-50, 50);
num2 = rand.Next(-50, 50);
}
Console.WriteLine("{0} {1} {2}", a, b, c);
Quiz(b, c);
}