***更新我的帖子以显示我的所有代码,希望这提供更多上下文。
当我运行该程序时,它会跳过我的第一个“if”语句并抛出错误。***
我收到错误Exception in thread "main" java.lang.NumberFormatException: For input string: ""
我正在尝试在 while 循环的第一个 else 语句中将字符串变量转换为 int...错误发生在int examScore = Integer.parseInt(userInput).
import java.util.Scanner;//imports scanner
public class ExamScoresCalculator{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);//creates new scanner object
//prompts user for lowest possible exam score
System.out.println("Enter the lowest possible value for the exam: ");
int lowRange = scan.nextInt();
//prompts user for highest possible exam score
System.out.println("Enter the highest possible value for the exam: ");
int highRange = scan.nextInt();
boolean flag = true; //while loop flag
double lowestExamScore = highRange; // holds lowest score
double highestExamScore = lowRange; //holds highest score
double totalPoints = 0; //holds sum of all scores entered
int totalExams = 0; //holds the total number of exams entered by the user
while (flag == true){
System.out.println("Enter an exam score between " + lowRange + " and " + highRange + " or type exit. "); //asks user for an exam score within correct range
String userInput = scan.nextLine();
if ((userInput.equals("exit")) || (userInput.equals("Exit")) || (userInput.equals("EXIT"))){ //checks if user enters "exit"
flag = false; //ends while loop
}
慕婉清6462132
慕慕森
忽然笑
相关分类