gpt4 book ai didi

java - Java程序中的加法等于不同的值

转载 作者:太空宇宙 更新时间:2023-11-04 11:46:57 26 4
gpt4 key购买 nike

我的 Java 代码有问题。这是一份可以计算您的成绩的问卷。一切都很顺利,直到最后一部分显示“当前分数”,整个方程应该等于 33.16,但实际上却等于 24。

我更改了一些值,做了一些研究,但我还没有找到我要找的东西。

import java.util.Scanner;

public class GradeCalculator {

public static void main(String[] args) {
System.out.println("Grading Scale:");
System.out.println("A\t 90 - 100");
System.out.println("B\t 80 - 89");
System.out.println("C\t 70 - 79");
System.out.println("D\t 60 - 69");
System.out.println("F\t below 60");

System.out.println("What letter grade do you want to achieve for the course?");
String desiredGrade;

Scanner keyboard = new Scanner(System.in);
desiredGrade = keyboard.next();

if (desiredGrade.equalsIgnoreCase("A") || desiredGrade.equalsIgnoreCase("B")
|| desiredGrade.equalsIgnoreCase("C") || desiredGrade.equalsIgnoreCase("D")
|| desiredGrade.equalsIgnoreCase("F")) {// is this necessary? vv
System.out.println("Enter Percentage Weights");
}

else {
System.out.println("Input error.");
System.exit(0);
}

int exam1, exam2, finalExam, labs, projects, attendance, quizzes;

System.out.println("Exam 1:\t");
exam1 = keyboard.nextInt();

System.out.println("Exam 2:\t");
exam2 = keyboard.nextInt();

System.out.println("Final Exam:\t");
finalExam = keyboard.nextInt();

System.out.println("Labs:\t");
labs = keyboard.nextInt();

System.out.println("Projects:\t");
projects = keyboard.nextInt();

System.out.println("Attendance:\t");
attendance = keyboard.nextInt();

System.out.println("Quizzes:\t");
quizzes = keyboard.nextInt();

// so the semicolon isn't needed after the if statement?
if (exam1 + exam2 + finalExam + labs + projects + attendance + quizzes != 100) {
System.out.println("Weights don't add up to 100, program exiting");
System.exit(0);
}

System.out.println("Enter your scores out of a 100:");
System.out.println("Do you know your Exam 1 score?");

String answer;
int exam1score = 0, exam2score = 0, finalExamScore = 0, labAverage = 0, projectAverage = 0, quizAverage = 0, attendanceAverage = 0;
double currentScore = 0;
answer = keyboard.next();

// ask about this

if (answer.equalsIgnoreCase("yes") || answer.equalsIgnoreCase("y")) {
// why can't i put int here?
System.out.println("Score received on exam 1:");
exam1score = keyboard.nextInt();

System.out.println("Do you know your Exam 2 score?");
answer = keyboard.next();

if (answer.equalsIgnoreCase("yes") || answer.equalsIgnoreCase("y")) {
System.out.println("Score received on exam 2:");
exam2score = keyboard.nextInt();

System.out.println("Do you know your Final Exam score?");
answer = keyboard.next();
}
if (answer.equalsIgnoreCase("yes") || answer.equalsIgnoreCase("y")) {
System.out.println("Score received on final exam");
finalExamScore = keyboard.nextInt();
}
}

System.out.println("Do you know your lab average?");
answer = keyboard.next();
if (answer.equalsIgnoreCase("yes") || answer.equalsIgnoreCase("y")) {
System.out.println("Average Lab Grade:");
labAverage = keyboard.nextInt();
}

System.out.println("Do you know your project average?");
answer = keyboard.next();
if (answer.equalsIgnoreCase("yes") || answer.equalsIgnoreCase("y")) {
System.out.println("Average Project Grade:");
projectAverage = keyboard.nextInt();
}

System.out.println("Do you know your quiz average?");
answer = keyboard.next();
if (answer.equalsIgnoreCase("yes") || answer.equalsIgnoreCase("y")) {
System.out.println("Average Quiz Grade:");
quizAverage = keyboard.nextInt();
}

System.out.println("Do you know your attendance average?");
answer = keyboard.next();
if (answer.equalsIgnoreCase("yes") || answer.equalsIgnoreCase("y")) {
System.out.println("Average Attendance Grade:");
attendanceAverage = keyboard.nextInt();
}

currentScore = ((double)exam1score*exam1 + exam2score*exam2 +finalExam*finalExamScore + labs*labAverage + projects*projectAverage + attendance*attendanceAverage + quizzes*quizAverage)/((double)exam1+exam2+finalExam+labs+projects+attendance+quizzes);
System.out.println("Current Grade Score:\t " + currentScore);

String grade;

if (currentScore >= 90)
grade = "A";
else if (currentScore >= 80)
grade = "B";
else if (currentScore >= 70)
grade = "C";
else if (currentScore >= 60)
grade = "D";
else
grade = "F";
}
}

最佳答案

以下仅将 exam1score*exam1 值转换为 double,而不是整个表达式。

(double) exam1score*exam1 + exam2score*exam2 + finalExam*finalExamScore + ....

所以,你应该这样做。

int nominator = exam1score*exam1 + exam2score*exam2 + finalExam*finalExamScore 
+ labs*labAverage + projects*projectAverage
+ attendance*attendanceAverage + quizzes*quizAverage;
int denominator = exam1 + exam2 + finalExam + labs + projects + attendance + quizzes;
currentScore = (double) nominator / denominator;

或者

int nominator = exam1score*exam1 + exam2score*exam2 + finalExam*finalExamScore
+ labs*labAverage + projects*projectAverage
+ attendance*attendanceAverage + quizzes*quizAverage);
int denominator = exam1 + exam2 + finalExam + labs + projects + attendance + quizzes;
currentScore = (nominator * 1.0) / denominator;

关于java - Java程序中的加法等于不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42260212/

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