在控制台中为用户编号输入设置最大和最小值

发布于 2025-01-21 22:36:34 字数 759 浏览 2 评论 0原文

Console.WriteLine("     **************************************");
Console.WriteLine("     ****** Reviewer Awarding Points ******");
Console.WriteLine("     **************************************");
Console.WriteLine();
string[]properties= { "Clarity", "Orginality", "Difficulty" };
        
int[] a = new int[3];
int[] b = new int[3];
        
for(int i = 0; i < 3; i++)
{ 
    Console.WriteLine("     ***" + properties[i]+"***");
    Console.Write(" Alice:   ");
            
    a[i] = Convert.ToInt16(Console.ReadLine());
    if(a[i]>100 || a[i] < 1)
        Console.Write("   Bob:   ");
    b[i] = Convert.ToInt16(Console.ReadLine());
    Console.WriteLine();
}
Console.Read();
}

我希望用户输入1到100之间的值(包括1和100)。那么我要做什么?

Console.WriteLine("     **************************************");
Console.WriteLine("     ****** Reviewer Awarding Points ******");
Console.WriteLine("     **************************************");
Console.WriteLine();
string[]properties= { "Clarity", "Orginality", "Difficulty" };
        
int[] a = new int[3];
int[] b = new int[3];
        
for(int i = 0; i < 3; i++)
{ 
    Console.WriteLine("     ***" + properties[i]+"***");
    Console.Write(" Alice:   ");
            
    a[i] = Convert.ToInt16(Console.ReadLine());
    if(a[i]>100 || a[i] < 1)
        Console.Write("   Bob:   ");
    b[i] = Convert.ToInt16(Console.ReadLine());
    Console.WriteLine();
}
Console.Read();
}

I want user to enter a value between 1 and 100(including 1 and 100).So what I am gonna do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

高冷爸爸 2025-01-28 22:36:34

您的问题不清楚
尝试

   int value;
   do{
   Console.Write("Enter a value");
   value=int.parse(Console.ReadLine());
   }while(value<1 || value>100);

your question is not clear
try

   int value;
   do{
   Console.Write("Enter a value");
   value=int.parse(Console.ReadLine());
   }while(value<1 || value>100);
愁杀 2025-01-28 22:36:34

制作一种检查范围内结果的方法:

public int AskInt(string question, int min, int max){

  while(true){
    Console.WriteLine(question);
    string input = Console.ReadLine();

    if(int.Tryparse(input, out int value) && value >= min && value <= max) 
        return value;
  }
}

逃脱循环的唯一方法是输入范围内的有效数字,否则问题只能重复,

然后您可以在代码中多次使用它:

int age = AskInt("Enter an age between 10 and 100: ", 10, 100);
int weight = AskInt("Enter a weight between 100 and 350: " 100, 350);

Make a method that checks the result for being in range:

public int AskInt(string question, int min, int max){

  while(true){
    Console.WriteLine(question);
    string input = Console.ReadLine();

    if(int.Tryparse(input, out int value) && value >= min && value <= max) 
        return value;
  }
}

The only way to escape the loop is to enter a valid number that is in range, otherwise the question just repeats

Then you can use it multiple times in your code:

int age = AskInt("Enter an age between 10 and 100: ", 10, 100);
int weight = AskInt("Enter a weight between 100 and 350: " 100, 350);
转瞬即逝 2025-01-28 22:36:34

Try this:

if(a[i]>=100 || a[i] <= 1)

You can learn about C# operators at this site: https://www.w3schools.com /CS/cs_operators.php

Try this:

if(a[i]>=100 || a[i] <= 1)

You can learn about C# operators at this site: https://www.w3schools.com/cs/cs_operators.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文