如何根据数组大小的用户输入给出错误? [英] How can i give an error depending on the users input for the array size?

查看:49
本文介绍了如何根据数组大小的用户输入给出错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,更新

所以我得到了这个,我想让第二个输入不小于0,但它不会比数组的大小更大?







Hello, UPDATE
So I got this far and i want to make that the 2nd input is not smaller then 0 but its not also bigger then the size of the array?



using System;
class MainClass

{
        public static void Main (string[] args)
    {
            int[] randomSizedArray;
            string sizeOfArray;
            int convertedSizeArray = -1;
        do{
    Console.WriteLine ("Please Enter the Size of the Array Between 1-99");
    sizeOfArray = Console.ReadLine();
    convertedSizeArray = Int32.Parse(sizeOfArray);
         } while ( convertedSizeArray < 1);

        randomSizedArray= new int[convertedSizeArray];
        Random randomSize = new Random();
        Console.WriteLine ("");
            for (int i=0; i < convertedSizeArray; i++){
                randomSizedArray[i] = randomSize.Next(1,99);
            }

            for (int i=0; i < convertedSizeArray; i++){
                Console.Write(randomSizedArray[i] + " ");
            }


    string swapindex1;
    string swapindex2;
    int index1;
    int index2;
        Console.WriteLine("");
do
  {
    Console.WriteLine ("Please Enter the first value to swap with");
    swapindex1 = Console.ReadLine();
    index1 = Int32.Parse(swapindex1);
  } while ( index1< 1);

do
  {
    Console.WriteLine ("Please Enter the second value to swap with");
    swapindex2 = Console.ReadLine();
    index2 = Int32.Parse(swapindex2);
  } while ( index2< 1 );

        int temp = randomSizedArray[index1];
        randomSizedArray[index1] = randomSizedArray[index2];
        randomSizedArray[index2] = temp;
        Console.WriteLine ("");
            for (int j=0; j < convertedSizeArray; j++){
                Console.Write(randomSizedArray[j] + " ");
        }

    }
}

推荐答案

您可以替换

You may replace
Quote:

Console.WriteLine(请输入1-99之间的数组大小);

sizeOfArray = Console.ReadLine();

convertedSizeArray = Int32.Parse(sizeOfArray);

Console.WriteLine ("Please Enter the Size of the Array Between 1-99");
sizeOfArray = Console.ReadLine();
convertedSizeArray = Int32.Parse(sizeOfArray);



类似




with something like

do
{
  Console.WriteLine ("Please Enter the Size of the Array Between 1-99");
  sizeOfArray = Console.ReadLine();
  convertedSizeArray = Int32.Parse(sizeOfArray);
} while ( convertedSizeArray <1 || convertedSizeArray>99);


替换这些

replace these
sizeOfArray = Console.ReadLine();
convertedSizeArray = Int32.Parse(sizeOfArray);



这些:


with these :

while (true)// a while loop will get data from user until user enter correct data
{
 sizeOfArray = Console.ReadLine();//get the user input
 if (char.IsDigit(sizeOfArray.ToString(), 0))//user entered a digit?
    if (int.Parse(sizeOfArray) > 1 && int.Parse(sizeOfArray) < 99)//the user digit is in correct range?
{
//DO ANYTHING YOU WANT WITH DIGITS HERE.
       break;//so break the while loop if you get correct data
}
    else
                        Console.WriteLine("Enter a digit between 1 to 99");//a message to user
 else
                    Console.WriteLine("Enter a digit between 1 to 99");//a message to user
}


这篇关于如何根据数组大小的用户输入给出错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆