gpt4 book ai didi

java - 根据 Java 中的表格分配成绩

转载 作者:行者123 更新时间:2023-12-01 14:03:51 25 4
gpt4 key购买 nike

我一直在做的这项作业是要求用户输入他们的测试分数和家庭作业分数,然后使用此表为他们分配成绩:

enter image description here

我们还没有学习数组,到目前为止只有基本的循环。有没有办法使用 for 或 while 循环而不是数十个 if 语句来做到这一点?这是我到目前为止所拥有的:

import java.util.Scanner;

public class GradeCalculator {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
int numberOfAssignments = 7;
int numberOfLabs = 16 ;
int totalAssignmentScore;
int totalLabScore;
int totalTestScore = 0;
int totalHomeworkScore = 0;
int zyante;
int attendance;
int midterm1;
int midterm2;
int finalTest;
int quizScore;
int pat;


for (int i = 1; i < numberOfAssignments; i++) {
System.out.println("Enter your score obtained in Assignment No."+ i + ":");
totalAssignmentScore = scanner.nextInt();
{
totalHomeworkScore += totalAssignmentScore;
System.out.println(totalHomeworkScore);
}

}
for (int i = 1; i < numberOfLabs; i++) {
System.out
.println("Enter your score obtained in Lab No." + i + ":");
totalLabScore = scanner.nextInt();
{
totalHomeworkScore += totalLabScore;
System.out.println(totalHomeworkScore);
}
}
System.out.println("Enter your score obtained in Zyante: ");
zyante = scanner.nextInt();
totalHomeworkScore +=zyante;

System.out.println("Enter your total score obtained in Attendance: ");
attendance = scanner.nextInt();
totalHomeworkScore += attendance;

System.out.println("Enter your Midterm no. 1 score: ");
midterm1 = scanner.nextInt();
totalTestScore += midterm1;

System.out.println("Enter your Midterm no. 2 score: ");
midterm2 = scanner.nextInt();
totalTestScore += midterm2;

System.out.println("Enter your Final exam score: ");
finalTest = scanner.nextInt();
totalHomeworkScore += finalTest;

System.out.println("Enter your total score in the in-class Quizzes: ");
quizScore = scanner.nextInt();
totalHomeworkScore += quizScore;

System.out.println("Enter your score for PAT: ");
pat = scanner.nextInt();
totalHomeworkScore += pat;


System.out.println("Your total test score is: " + totalTestScore );
System.out.println("your total homework score is: " + totalHomeworkScore);





}
}

最佳答案

如果你真的不应该使用数组,那么你只需要分析你的数组并从中提取逻辑。

从以下内容开始:

enum Grade {
P,
A,
G;
}

public Grade score ( int tests, int homework ) {
if ( homework <= 599
|| tests <= 149 ) {
return Grade.P;
}
return Grade.G;
}

enter image description here

然后再剥掉一些:

public Grade score ( int tests, int homework ) {
if ( homework <= 599
|| tests <= 149
|| (homework <= 719 && tests <= 179 )) {
return Grade.P;
}
return Grade.G;
}

你有:

enter image description here

依此类推,直到您详细了解所有数据。

有一些稍微整洁的方法来组织 if 语句,以及更逻辑的方法来剥离表的各个部分,但本质上,没有数组或实际的算法,这实际上是所有可能的。

关于java - 根据 Java 中的表格分配成绩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19097600/

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