[英]Java cannot be resolved to a variable issue
Java 和编码的新手。 我尝试在 if-else 语句之外对其进行初始化,以查看是否可以解决问题,但这也不起作用,因为它声称变量没有值。 当我尝试打印 currentScore 时,我收到 currentScore 无法解析为变量的错误消息。 这是我当前的代码。
import java.util.Scanner;
public class GradeCalculator {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
System.out.println("Please input the letter grade you would like to receive:");
char desiredGrade = keyboard.next().charAt(0);
System.out.println("Please put either 'Yes' or 'No' whether or not you know the score for each graded item");
System.out.println("Do you know the grade to Exam 1?");
String doesUserKnowExamOne = keyboard.next();
if (doesUserKnowExamOne.equalsIgnoreCase("Yes")) {
System.out.println("Please input the grade for Exam 1");
double examOneGrade = keyboard.nextDouble();
System.out.println("Please input the weight for Exam 1");
int examOneWeight = keyboard.nextInt();
System.out.println("Do you know the grade to Exam 2?");
String doesUserKnowExamTwo = keyboard.next();
double currentScore = (examOneGrade * examOneWeight) / (examOneWeight);
if (doesUserKnowExamTwo.equalsIgnoreCase("Yes")) {
System.out.println("Please input the grade for Exam 2");
double examTwoGrade = keyboard.nextDouble();
System.out.println("Please input the weight for Exam 2");
int examTwoWeight = keyboard.nextInt();
System.out.println("Do you know the grade to Final Exam?");
String doesUserKnowFinal = keyboard.next();
currentScore = (examOneGrade * examOneWeight) + (examTwoGrade * examTwoWeight) / (examOneWeight + examTwoWeight);
if (doesUserKnowFinal.equalsIgnoreCase("Yes")) {
System.out.println("Please input the grade for Final Exam");
double finalExamGrade = keyboard.nextDouble();
System.out.println("Please input the weight for Final Exam");
int finalExamWeight = keyboard.nextInt();
currentScore = (examOneGrade * examOneWeight) + (examTwoGrade * examTwoWeight) + (finalExamGrade + finalExamWeight) / (examOneWeight + examTwoWeight + finalExamWeight);
} else {
System.out.println("Final Exam not taken yet");
}
} else {
System.out.println("Exam Two and Final Exam not taken yet");
}
} else {
System.out.println("Exam One, Exam Two, and Final Exam not taken yet");
}
System.out.println(currentScore);
}
}
问题是您在第一个if
的 scope 中声明currentScore
变量,并试图在 scope 之外读取它。
尝试声明:
double currentScore = 0;
在您的第一个if
然后而不是声明之上,只需执行以下操作:
currentScore = (examOneGrade * examOneWeight)/(examOneWeight);
在第一个if
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.