所以,我是一名软件新生,我正在做我的任务,这可能看起来像一个简单的问题,但它正在破坏我的头脑。
分配是让用户输入6个学校科目和他们为每个科目获得的分数,然后将分数计算为分数并显示。我觉得我已经完成了代码的骨架,但是当我运行程序时,无论我在答案中输入什么输入似乎总是无效。
感谢您的帮助, 乔
这是我正在使用的代码,它在Visual Studio中使用。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace caoAssignment
{
class Program
{
static void Main(string[] args)
{
// Delcaring variables //
string subIn, sub1, sub2, sub3, sub4, sub5, sub6;
string gradeIn, grade1, grade2, grade3, grade4, grade5, grade6;
int pointsIn, points1, points2, points3, points4, points5, points6;
int x;
string doAgain = "";
// Getting the user to input necessary information. 2 peices needed.
do
{
x = 1;
while (x <= 6)
{
Console.WriteLine("Please enter a subject ");
subIn = Console.ReadLine();
Console.WriteLine("Please enter the grade you got in that subject (H1 - O8) ");
gradeIn = Console.ReadLine();
// Processing the grades in points //
if (gradeIn == "H1")
pointsIn = 100;
if (gradeIn == "H2")
pointsIn = 88;
if (gradeIn == "H3")
pointsIn = 77;
if (gradeIn == "H4")
pointsIn = 66;
if (gradeIn == "H5")
pointsIn = 56;
if (gradeIn == "H6")
pointsIn = 46;
if (gradeIn == "H7")
pointsIn = 37;
if (gradeIn == "H8")
pointsIn = 0;
if (gradeIn == "O1")
pointsIn = 56;
if (gradeIn == "O2")
pointsIn = 46;
if (gradeIn == "O3")
pointsIn = 37;
if (gradeIn == "O4")
pointsIn = 28;
if (gradeIn == "O5")
pointsIn = 20;
if (gradeIn == "O6")
pointsIn = 12;
if (gradeIn == "O7")
pointsIn = 0;
if (gradeIn == "O8")
pointsIn = 0;
else
Console.WriteLine("Input is wrong, please redo. ");
// Assigning each grade,subject and points depending on what X is //
if (x == 1)
{
subIn = sub1;
gradeIn = grade1;
pointsIn = points1;
}
if (x == 2)
{
subIn = sub2;
gradeIn = grade2;
pointsIn = points2;
}
if (x == 3)
{
subIn = sub3;
gradeIn = grade3;
pointsIn = points3;
}
if (x == 4)
{
subIn = sub4;
gradeIn = grade4;
pointsIn = points4;
}
if (x == 5)
{
subIn = sub5;
gradeIn = grade5;
pointsIn = points5;
}
if (x == 6)
{
subIn = sub6;
gradeIn = grade6;
pointsIn = points6;
}
x++;
} // End of the while loop
Console.WriteLine("Do again? (Y/N) ");
doAgain = Console.ReadLine();
} // End of the do loop
while (doAgain == "Y");
// Command to keep the program open for the user //
Console.ReadKey();
}
}
}
答案 0 :(得分:1)
看一下代码的这一部分:
if (gradeIn == "O7")
pointsIn = 0;
if (gradeIn == "O8")
pointsIn = 0;
else
Console.WriteLine("Input is wrong, please redo. ");
如果成绩不是“08”会怎样? else
语句仅适用于最后一个if
语句。另外一个好的经验法则是始终在if
,else
等之后添加花括号来表示范围并避免错误。
如果您使用if
链接else
语句,则告诉程序下一个if
仅在未应用上一个if (gradeIn == "O7")
{
pointsIn = 0;
}
else if (gradeIn == "O8")
{
pointsIn = 0;
}
else
{
Console.WriteLine("Input is wrong, please redo. ");
}
时才会应用。
==
使用string.Equals
运算符比较字符串时,比较区分大小写。要使其不区分大小写,请使用重载的if(string.Equals(gradeIn, "O7", StringComparison.OrdinalIgnoreCase))
方法:
public void setCurrentFragment(Fragment currentFragment) {
this.currentFragment = currentFragment;
}
答案 1 :(得分:0)
首先,项目不应该编译,因为代码试图使用未分配的变量。 SUB1
//Assigning each grade,subject and points depending on what X is
if (x == 1)
{
subIn = sub1;
gradeIn = grade1;
pointsIn = points1;
}
其次
subIn = sub2;
这应该是另一种方式。 第一个值是您要分配第二个值的变量。