我试图制作这个节目:
我不确定是否应该使用if else或某种循环或两者兼而有之。这是我的第一个编程课程,所以任何帮助都会很棒。
到目前为止,这是我的工作,但它不是我想要的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment_3
{
class Program
{
static void Exercise1(){
}
static void Exercise2()
{
double number;
string character;
Console.Write("Enter an integer or 'q' to quit ");
number = double.Parse(Console.ReadLine());
character = Console.ReadLine();
if (number >= 1 && number <= 10 || character =="q")
{
Console.WriteLine("Coool");
}
else
{
Console.WriteLine("Your number must be between 1 and 10");
}
}
static void Main(string[] args)
{
Exercise1();
Exercise2();
Console.WriteLine("Press enter to end");
Console.ReadLine();
}
}
}
答案 0 :(得分:1)
static void Exercise2()
{
string entry;
Console.Write("Enter an integer or 'q' to quit: ");
entry = Console.ReadLine();
while (entry.ToLower() != "q")
{
int number;
if (int.TryParse(entry, out number))
{
if (number >= 1 && number <= 10)
{
Console.WriteLine("Coool");
}
else
{
Console.WriteLine("Your number must be between 1 and 10");
}
}
Console.Write("Enter an integer or 'q' to quit: ");
entry = Console.ReadLine();
}
}
答案 1 :(得分:0)
将ReadLine()的结果存储为变量并进行评估,检查“q”然后,如果不起作用,则将变量解析为int。