简体   繁体   中英

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. 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.

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. Would that be possible in a for loop? 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 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. 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". You seem to need to catch every input exception while you were thinking using try catch method. In this case, I'll suggest defining a new integer class for you. since you want to use 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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