gpt4 book ai didi

c# - CAO分配,输出不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 15:30:01 24 4
gpt4 key购买 nike

我认为我已经完成了程序的主要部分,但最后我需要输出:主题(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();

}
}
}
`

最佳答案

既然你正在学习,我不想破坏乐趣。这是您正在做的事情的伪代码。我已经为您想仔细查看的部分添加了评论。

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 循环非常有用。

当您知道需要存储许多相同类型值的实例时,考虑使用数组。在您的情况下,科目,成绩和分数。在您学习的后期阶段,您将使用集合和类/结构来存储它们,但在走这条路之前先熟悉数组。

祝你好运!

关于c# - CAO分配,输出不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34102509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com