writeline问题不断重复 [英] writeline issue keeps repeating

查看:75
本文介绍了writeline问题不断重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我似乎无法找到错误。正如您在我的代码中看到的那样,需要输入值1或0(否则会出现一条消息并要求重新输入值)但是此消息会不断重复。



 Console.WriteLine( 请输入第一个值:< /跨度>); 
iValue1 =
Convert.ToInt16(Console.ReadLine());
while (iValue1 > 1
{
Console.WriteLine( 抱歉,值必须为1或0。重新输入值:);
}







i似乎无法找到解决方法,发生这种情况对于我正在使用的所有三个值。



预览问题:

http://i.imgur.com/WAcT2Ho.png?1 [ ^ ]

解决方案

我想这一行



 iValue1 = 
Convert.ToInt16(Console.ReadLine());





必须再次进入while循环才能获得用户的新输入。



 Console.WriteLine( 请输入第一个值:); 
iValue1 =
Convert.ToInt16(Console.ReadLine());
while (iValue1 > 1
{
Console.WriteLine( 抱歉,值必须为1或0。重新输入值:);
iValue1 = Convert.ToInt16(Console.ReadLine());

}


您好,我无法重现您的问题。在控制台我输入1并单击输入按钮,我没有看到错误消息。您的代码似乎有些问题,您能否提供主要方法的完整代码



  public   static   void  Main( string  [] args)
{
int iValue1;
Console.WriteLine( 请输入第一个值:);
iValue1 =
Convert.ToInt16(Console.ReadLine());
while (iValue1 > 1
{
Console.WriteLine( 抱歉,值必须为1或0。重新输入值:);
}
Console.ReadLine();





我之前认为你输入时会出现这个错误1或0,你没有明确提到这个错误。



你的错误原因是'while循环'说你输入5然后循环将继续作为条件运行时(iValue1> 1)总是正确

而不是while循环使用如果条件



如您在这里提供的代码是解决方案:使用do而不是while循环,如下所示



  do  

{
if (iValue1 > < span class =code-digit> 1 )
{
Console.WriteLine( 抱歉值必须为1或0.请重新输入值:);
iValue1 = Convert.ToInt16(Console.ReadLine());
}
}
while (iValue1 > 1 );


within my code there is error i cant seem to find. as you can see by my code a value of 1 or 0 needs to be entered (otherwise a message occurs and asks for the value to be re-entered) however this message keeps repeating constantly.

Console.WriteLine("Please enter first value: ");
iValue1 =
    Convert.ToInt16(Console.ReadLine());
while (iValue1 > 1)
{
    Console.WriteLine("Sorry value must be 1 or 0. Please re-enter value: ");
}




i can't seem to find out how to fix this, this happens for all three values i am using.

Preview of Problem:
http://i.imgur.com/WAcT2Ho.png?1[^]

解决方案

I guess this line

iValue1 =
    Convert.ToInt16(Console.ReadLine());



must come again inside the while loop to get the fresh input from the user.

Console.WriteLine("Please enter first value: ");
iValue1 =
    Convert.ToInt16(Console.ReadLine());
while (iValue1 > 1)
{
    Console.WriteLine("Sorry value must be 1 or 0. Please re-enter value: ");
    iValue1 = Convert.ToInt16(Console.ReadLine());

}


Hi, I am not able to reproduce your issue. at console i enter 1 and click enter button, i do not see error message. It seems some thing is wrong with your code, Can you provide the complete code of main method

public static void Main(string[] args)
       {
           int iValue1;
           Console.WriteLine("Please enter first value: ");
           iValue1 =
               Convert.ToInt16(Console.ReadLine());
           while (iValue1 > 1)
           {
               Console.WriteLine("Sorry value must be 1 or 0. Please re-enter value: ");
           }
           Console.ReadLine();



I thought earlier that you are getting this error when you enter 1 or 0, you have not mentioned explicitly when this error is coming.

Reason of your error is 'while loop' Say you entered 5 then while loop will keep on running as condition while (iValue1 > 1) always true
instead of while loop use If condition

As you have provided your code here is the solution: use do while instead of while loop as shown below

do

{
    if (iValue1 > 1)
    {
        Console.WriteLine("Sorry value must be 1 or 0. Please re-enter value: ");
        iValue1 = Convert.ToInt16(Console.ReadLine());
    }
}
while (iValue1 > 1);


这篇关于writeline问题不断重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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