繁体   English   中英

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

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

只是从for循环开始,我对此问题有些困惑。 在下面的代码中,我能够创建一个大小为4的数组。我需要能够允许用户放入4个不同的整数(范围为1-100)并将其存储在数组中。

我还需要确保用户输入的任何内容都不小于0或大于100。在这种情况下,我正在考虑使用try-catch方法。 在for循环中可能吗? 请指教:

 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); 
}

我会根据我想您要执行的操作将其更改为:

        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不是用于测试输入条件的,即if语句适用的条件。 尝试捕获是否要测试该代码是否可以运行以及是否无法提供替代方法。 因此,例如,如果用户输入了字符串,则try and catch语句将停止程序崩溃。 希望这可以帮助

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");
    }
}

我假设您知道如何在“正确的情况”下使用try-catch。 在考虑使用try catch方法时,您似乎需要捕获每个输入异常。 在这种情况下,我建议为您定义一个新的整数类。 因为您想使用try-catch。 对于输入数据超出范围或期望的范围,抛出异常。 代码将如下所示:

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