简体   繁体   English

使用for循环允许用户输入1-100的输入

[英]Using a for loop to allow the user to put in an input from 1-100

Just starting out with for loops and I'm kinda stumped on this question. 只是从for循环开始,我对此问题有些困惑。 In the code below I was able to create an array with size of 4. I need to be able to allow the users to put in 4 different integers that range from 1-100 and store it in a array. 在下面的代码中,我能够创建一个大小为4的数组。我需要能够允许用户放入4个不同的整数(范围为1-100)并将其存储在数组中。

I would also need to make sure that the user doesn't input anything that is less than 0 or larger than 100. In this case I was thinking using a try-catch method. 我还需要确保用户输入的任何内容都不小于0或大于100。在这种情况下,我正在考虑使用try-catch方法。 Would that be possible in a for loop? 在for循环中可能吗? Please advise: 请指教:

 const int SIZE = 4;
int[] array = new int[SIZE];
int numberOfTests = 4;

for (int count = 0; count < numberOfTests; count++) // Start the count from 0-4
{
    int min = 0;
    int max = 100;

    Console.WriteLine("Please enter test score " + count); 
}

I would change it to this based on what I think you want to do: 我会根据我想您要执行的操作将其更改为:

        const int SIZE = 4;
        int[] array = new int[SIZE];
        int numberOfTests = 4;

        for (int count = 0; count < numberOfTests; count++) // Start the count from 0-4
        {
            int min = 0;
            int max = 100;

            int countf = count + 1;

            Console.WriteLine("Please enter test score " + countf);
            int a = Convert.ToInt32(Console.ReadLine());
            if (a > 0 && a < 101)
            {
                array[count] = a;
            }

        }
        Console.WriteLine("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
        Console.WriteLine(array[0]);
        Console.WriteLine(array[1]);
        Console.WriteLine(array[2]);
        Console.WriteLine(array[3]);
        Console.ReadLine();

Try and catch isn't for testing the inputs conditions thats what an if statement is for. Try and catch不是用于测试输入条件的,即if语句适用的条件。 Try and catch if for testing that the code is able to run and if not providing an alternative. 尝试捕获是否要测试该代码是否可以运行以及是否无法提供替代方法。 So for example if a user entered a string a try and catch statement would stop the program crashing. 因此,例如,如果用户输入了字符串,则try and catch语句将停止程序崩溃。 Hope this helps 希望这可以帮助

const int numberOfTests = 4;
int[] array = new int[numberOfTests];

for (int count = 0; count < numberOfTests; count++)
{
    Console.WriteLine("Please enter test score " + count);
    try
    {
        int answer = Convert.ToInt32(Console.ReadLine());

        if (answer > 0 && answer < 101)
             array[count] = answer;
        else Console.WriteLine("Please provide a value between 1-100");

    }
    catch
    {
        Console.WriteLine("Please provide an integer");
    }
}

I assume you know how to use try-catch in "correct scenario". 我假设您知道如何在“正确的情况”下使用try-catch。 You seem to need to catch every input exception while you were thinking using try catch method. 在考虑使用try catch方法时,您似乎需要捕获每个输入异常。 In this case, I'll suggest defining a new integer class for you. 在这种情况下,我建议为您定义一个新的整数类。 since you want to use try-catch. 因为您想使用try-catch。 Throw an exception for the input data out of range or whatever you expect to be. 对于输入数据超出范围或期望的范围,抛出异常。 The code will be like this: 代码将如下所示:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class MyInt
{
    public MyInt(int i)
    {
        Data = i;
    }

    private int _data;
    public int Data
    {
        get => _data;
        set
        {
            if(value<0||value>100)throw new ArgumentOutOfRangeException();
            _data = value;
        }
    }
}

public static void Main()
{
    MyInt[] array = new MyInt[4];

    int min = 0;
    int max = 100;
    for (int count = 0; count < array.Length; count++) // Start the count from 0-4
    {
        Console.WriteLine("Please enter test score " + count);
        try
        {
            array[count] = new MyInt(int.Parse(Console.ReadLine()));
        }
        catch (Exception exception)
        {
            Console.WriteLine($"Input exception :{exception}/n, Please input a valid number between {min} and {max}");
            count--;
        }
    }

    Console.WriteLine($"The array result here, eg. Sum of all :{array.Sum(i=>i.Data)}");
    Console.ReadKey();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM