我认为程序的主要部分是完整的,但最后我需要输出:主题(subIn或sub1),等级(gradeIn或grade1)和点(pointsIn或points1)。
我需要用循环(分配点)来做这个,所以当我尝试运行程序时,我可以输入主题和等级,但我无法弄清楚如何输出所有信息。
当问题“再做一次?”时无论我输入“N”,它都不会输出任何东西。
(注意,我知道我在这段代码中没有输出任何信息,但是我试图将Console.WriteLine
放在代码中并提供必要的输入,但无济于事。
代码:
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 = "";
pointsIn = 0;
// 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 (string.Equals(gradeIn, "H1", StringComparison.OrdinalIgnoreCase))
{
pointsIn = 100;
}
else if (string.Equals(gradeIn, "H2", StringComparison.OrdinalIgnoreCase))
{
pointsIn = 88;
}
else if (string.Equals(gradeIn, "H3", StringComparison.OrdinalIgnoreCase))
{
pointsIn = 77;
}
else if (string.Equals(gradeIn, "H4", StringComparison.OrdinalIgnoreCase))
{
pointsIn = 66;
}
else if (string.Equals(gradeIn, "H5", StringComparison.OrdinalIgnoreCase))
{
pointsIn = 56;
}
else if (string.Equals(gradeIn, "H6", StringComparison.OrdinalIgnoreCase))
{
pointsIn = 46;
}
else if (string.Equals(gradeIn, "H7", StringComparison.OrdinalIgnoreCase))
{
pointsIn = 37;
}
else if (string.Equals(gradeIn, "H8", StringComparison.OrdinalIgnoreCase))
{
pointsIn = 0;
}
else if (string.Equals(gradeIn, "O1", StringComparison.OrdinalIgnoreCase))
{
pointsIn = 56;
}
else if (string.Equals(gradeIn, "O2", StringComparison.OrdinalIgnoreCase))
{
pointsIn = 46;
}
else if (string.Equals(gradeIn, "O3", StringComparison.OrdinalIgnoreCase))
{
pointsIn = 37;
}
else if (string.Equals(gradeIn, "O4", StringComparison.OrdinalIgnoreCase))
{
pointsIn = 28;
}
else if (string.Equals(gradeIn, "O5", StringComparison.OrdinalIgnoreCase))
{
pointsIn = 20;
}
else if (string.Equals(gradeIn, "O6", StringComparison.OrdinalIgnoreCase))
{
pointsIn = 12;
}
else if (string.Equals(gradeIn, "O7", StringComparison.OrdinalIgnoreCase))
{
pointsIn = 0;
}
else if (string.Equals(gradeIn, "O8", StringComparison.OrdinalIgnoreCase))
{
pointsIn = 0;
}
else
{
Console.WriteLine("Input is wrong, please redo. ");
}
// Assigning each grade,subject and points depending on what X is //
if (x == 1)
{
sub1 = subIn;
grade1 = gradeIn;
points1 = pointsIn;
}
if (x == 2)
{
sub2 = subIn;
grade2 = gradeIn;
points2 = pointsIn;
}
if (x == 3)
{
sub3 = subIn;
grade3 = gradeIn;
points3 = pointsIn;
}
if (x == 4)
{
sub4 = subIn;
grade4 = gradeIn;
points4 = pointsIn;
}
if (x == 5)
{
sub5 = subIn;
grade5 = gradeIn;
points5 = pointsIn;
}
if (x == 6)
{
sub6 = subIn;
grade6 = gradeIn;
points6 = pointsIn;
}
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 :(得分:0)
因为你在学习我不想破坏乐趣。这是你正在做的伪代码。我已经为您想要仔细查看的部分添加了评论。
do
{
currentCount = 1;
while (currentCount <= maxCount)
{
subject = ReadSubject();
grade = ReadGrade();
points = GetPointsFor(grade);
if (IsValid(points)) //You currently have a bug here
{
StoreSubjectValues(currentCount, subject, grade, points);
currentCount++;
}
else
{
PrintError(subject, grade);
}
}
//This is what you want to add for the printout
for (currentCount = 1; currentCount <= maxCount; currentCount++)
{
PrintSubjectValues(subjectValuesFor[currentCount]);
}
userWantsToContinue = AskUserToContinue();
} while (userWantsToContinue);
关于循环,这是一个方便的指南:
do
循环很好,如果你知道你想要至少循环一次,但在你开始之前不知道有多少循环。while
循环是好的,如果你不知道你是否会循环,并且在你开始之前不知道何时结束。for
循环是好的。当您知道需要存储许多相同类型值的实例时,请查看使用数组。在你的情况下科目,成绩和分数。在学习的后期阶段,您将使用集合和类/结构来存储这些,但在走这条路之前要熟悉数组。
祝你好运!