while(Number_Of_Fruits > 0 & null <11);
{
Console.WriteLine("Please Insert a Number Between 1 and 10");
Number_Of_Fruits = Convert.ToInt32(Console.ReadLine());
}
/*Rest Of Code*/
我需要做的是确保测试仅在数字超过0且更少棕褐色时才有效。出于某种原因,当我输入3时测试失败时,我会得到奇怪的结果或以上。
提前致谢:)
答案 0 :(得分:1)
尝试更改以下代码行...
while(Number_Of_Fruits > 0 & null <11);
到
while(Number_Of_Fruits > 0 && Number_Of_Fruits < 11);
祝你好运!
答案 1 :(得分:1)
你应该使用&amp;&amp;而不是&amp;和Number_Of_Fruits而不是null while(Number_Of_Fruits&gt; 0&amp;&amp; Number_Of_Fruits&lt; 11);
{
Console.WriteLine("Please Insert a Number Between 1 and 10");
Number_Of_Fruits = Convert.ToInt32(Console.ReadLine());
}
答案 2 :(得分:1)
条件状态数字大于0且更少晒黑11 ,所以我不知道您为什么要将11
与null
进行比较...
while(Number_Of_Fruits > 0 && Number_Of_Fruits < 11)
答案 3 :(得分:1)
问题1:您不应该使用semicolon ;
关闭while循环。如果你关闭它,它将不会进入循环,但你应该在do-while
循环中使用它
问题2:您应该使用双&符号&&
进行逻辑和操作
问题3:您不应将null
与integer
进行比较。
解决方案1:
while(Number_Of_Fruits > 0 && Number_Of_Fruits <11)
{
Console.WriteLine("Please Insert a Number Between 1 and 11");
Number_Of_Fruits = Convert.ToInt32(Console.ReadLine());
}
解决方案2:我认为do-while
循环最适合您。
int Number_Of_Fruits = 0;
do
{
Console.WriteLine("Please Insert a Number Between 1 and 11");
Number_Of_Fruits = Convert.ToInt32(Console.ReadLine());
if(Number_Of_Fruits > 0 && Number_Of_Fruits < 11)
{
//do whatever you want
}
} while (Number_Of_Fruits > 0 && Number_Of_Fruits < 11) ;
解决方案3:如果您想以更好的方式处理用户输入,可以使用Int32.TryParse()
代替Convert.ToInt32()
int Number_Of_Fruits = 0;
do
{
Console.WriteLine("Please Insert a Number Between 1 and 11");
if (Int32.TryParse(Console.ReadLine(), NumberStyles.None, CultureInfo.InvariantCulture, out Number_Of_Fruits))
{
if(Number_Of_Fruits > 0 && Number_Of_Fruits < 11)
{
//do whatever you want
}
}
else
{
Console.WriteLine("User Input is Invalid (not a number) -> Terminating");
}
} while (Number_Of_Fruits > 0 && Number_Of_Fruits < 11) ;
答案 4 :(得分:0)
while(Number_Of_Fruits > 0 && Number_Of_Fruits <11)
{
Console.WriteLine("Please Insert a Number Between 1 and 10");
Number_Of_Fruits = Convert.ToInt32(Console.ReadLine());
}
/*Rest Of Code*/
访问[link] http://msdn.microsoft.com/en-us/library/6a71f45d.aspx希望它对您有所帮助。这是一个很好的起点。
答案 5 :(得分:0)
尝试TryParse!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int Number_Of_Fruits;
do
{
Console.WriteLine("Please Insert a Number Between 1 and 10");
}
while (!int.TryParse(Console.ReadLine(), out Number_Of_Fruits) || Number_Of_Fruits < 1 || Number_Of_Fruits > 10);
}
}
}