我正在尝试编写一个程序来输入学生成绩。它们的标记应该在0到100之间。程序应该显示:
a. "note is A" when the score is 90 and more
b. "note is B" when the note is of 80 or more but less than 90
c. "note is C" when the score is 70 or more but less than 80
d. "note is D" when the score is 60 or more but less than 70
e. 'Failure' when the score is less than 60
f. If the input value is less than 0 or greater than 100 the program should display "Enter a value between 0 and 100"
程序必须循环
问题在于,当我运行代码时,它要求用户输入成绩,但不会通过我的if-else来检查将要记录的内容。
private static void note()
{
int[] test = new int[1];
for (int i = 0; i <= test.Length; i++)
{
Console.WriteLine("Please enter test " + i);
Console.ReadLine();
if ( i<=90)
{
Console.WriteLine("note is A");
}
else if (i <=80 || i > 90 )
{
Console.WriteLine("note is B");
}
else if (i <= 70 || i > 80)
{
Console.WriteLine("note is C");
}
else if (i <= 60 || i > 70)
{
Console.WriteLine("note is D");
}
else if (i >60)
{
Console.WriteLine("Failure");
}
else if (i > 0 || i < 100)
{
Console.WriteLine("Enter a value between 0 and 100");
}
else if (i == 999)
{
Console.WriteLine("you Enterd 999 to stop ");
}
test[i] = Console.Read();
Console.ReadKey();
}
}
答案 0 :(得分:4)
这里有几个主要问题。
test
变量只有长度为1,为什么还要把它变成一个数组呢?为什么不使用int
呢?i
。do...while (true)
代替。input >= 90
。实际上,对于几乎所有的if语句都是如此(唯一正确的是“F”小于60)。例如,i <=80 || i > 90
实际应为input >= 80
。您应该已经测试过它是否大于或等于90,因此您不需要再次测试该条件(因此else if
),但即使您确实想要明确检查这一点,也是正确的表达这一点的方法是i >= 80 && i < 90
- 现在你将B定义为“小于80或大于90” - 换句话说,B实际上被定义为除 a B。对于运营商,请记住以下内容:
score >= 80 && score < 90
是“得分至少为80且低于90。”=是“大于或等于”
以上是我在上述代码中的变体:
private static void note()
{
int[] i = new int[1];
// Make sure this is an infinite loop
do
{
Console.WriteLine("Please enter test result");
bool result = int.TryParse(Console.ReadLine(), out i[0]);
if (!result)
{
Console.WriteLine("Please enter a number");
continue;
}
// Test for this and the next conditions first - this'll allow us to shorten the other "if" statements
if (i[0] == 999)
{
Console.WriteLine("You entered 999 to stop");
// End the loop
break;
}
// The only valid answer that's > 100 is 999
else if (i[0] < 0 || i[0] > 100)
{
Console.WriteLine("Enter a value between 0 and 100");
}
// We can't run this condition first because if we did "5000" would "count" as an A (rather than an invalid condition)
else if (i[0] >= 90)
{
Console.WriteLine("note is A");
}
// No need to explicitly check to see if this is < 90, we know it must be
// by virtue of the fact that the previous condition is false
else if (i[0] >= 80)
{
Console.WriteLine("note is B");
}
else if (i[0] >= 70)
{
Console.WriteLine("note is C");
}
else if (i[0] >= 60)
{
Console.WriteLine("note is D");
}
else if (i[0] < 60)
{
Console.WriteLine("Failure");
}
Console.WriteLine("Your result is:" + i[0]);
} while (true);
}
答案 1 :(得分:3)
您从控制台读取但您对用户输入没有任何作用:
Console.ReadLine();
然后检入ifs循环值而不是用户输入。
if ( i<=90)
您需要在某处存储用户输入: 例如:
strig userInmput = Console.ReadLine();
并将其从int
转换为string
例如:
int mark = 0;
bool succes = int.TryParse(userInput, out mark);
if(!succes)
{
Console.WriteLine("Only digits");
}
比较mark
代替i
。
答案 2 :(得分:2)
在检查前加上test[i] = Console.Read();
行。你没有把任何东西放到阵列上。然后检查if
条件检查您的数组值而不是您的计数器(i
):
for (int i = 0; i <= test.Length; i++)
{
Console.WriteLine("Please enter test " + i);
test[i] = Convert.ToInt32(Console.ReadLine()); //putting your value
if (test[i]<=90)//check your value
{
Console.WriteLine("note is A");
}
//... same here
Console.ReadKey();
}
答案 3 :(得分:2)
您需要将值读入变量。把它放在循环之外:
int answer;
bool didItWork;
在循环中阅读:
didItWork = int.TryParse(Console.ReadLine(), out answer);
if (!didItWork)
{
Console.WriteLine("Need a number");
}
else if (answer > 90) {
// Etc
}
请不要将您的价值读入i
,因为您会弄乱您的循环
答案 4 :(得分:2)
你现在开始编码的方式,它是否能给出答案是一个奇迹。 你应该这样做:
private static void note()
{
int[] i = new int[1];
Console.WriteLine("Please enter test result");
i[0]= Convert.ToInt32(Console.ReadLine());
if ( i[0]>=90)
{
Console.WriteLine("note is A");
}
else if (i[0] >=80 && i[0] < 90 )
{
Console.WriteLine("note is B");
}
else if (i[0] >= 70 && i[0] < 80)
{
Console.WriteLine("note is C");
}
else if (i[0] >= 60 && i[0] < 70)
{
Console.WriteLine("note is D");
}
else if (i[0] < 60)
{
Console.WriteLine("Failure");
}
else if (i[0] < 0 || i[0] > 100)
{
Console.WriteLine("Enter a value between 0 and 100");
}
else if (i[0] == 999)
{ Console.WriteLine("You enterd 999 to stop ");
}
Console.Writeline("You're result is:" + i[0]);
Console.ReadKey();
}
1的数组不是很有用。把它变成一个int。 并确保了解&lt;,&gt;,&gt; =,&lt; =和||,&amp;&amp;
之间的区别