![](/img/trans.png)
[英]I get an error message as follows: Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 0
[英]how can i get java if statment promopt a user with error message when a number out of the range
我是新手,正在做作業,條件如下:-使用條件語句檢查成績是否超出0-101的范圍並顯示錯誤消息。 -否則,如果等級在0-101范圍內,則將等級添加到總計中,並得出等級的平均值。 我有下面的代碼,它工作正常,但是當我輸入0-101范圍以外的數字時,不會出現錯誤消息。 誰能幫助我解決我做錯的事情,感謝您的幫助。 這是我的代碼:import java.util.Scanner;
public class GradeCalculator {
public void readGrade() {
// 1. Creatin a Scanner using the InputStream available.
Scanner keyboard = new Scanner( System.in );
// 2. Using the Scanner to read int from the user.
// 3. returns the next double.
}
public void WorkOutGrade(){
Scanner keyboard = new Scanner(System.in);
//declaring the variables.
int testScoreA;
int testScoreB;
int testScoreC;
int testScoreD;
int testScoreE;
double sum;
double average;
//
do{
System.out.println("welcome to the grade calculator , please Enter a grade between 0-101 ");
System.out.print("And press enter key on your keyboard to type the next score, ");
System.out.println("please Enter your first test score: ");
testScoreA = keyboard.nextInt();
if (testScoreA >= 0 && testScoreA < 101 );
else if (testScoreA < 0 && testScoreA > 101 )
System.out.print("wront input ");
System.out.print("Enter your second test score: ");
testScoreB = keyboard.nextInt();
System.out.print("Enter your third test score: ");
testScoreC = keyboard.nextInt();
System.out.print("Enter your fourth test score: ");
testScoreD = keyboard.nextInt();
System.out.print("Enter your fifth test score: ");
testScoreE = keyboard.nextInt();
keyboard.nextLine();
sum =(testScoreA+testScoreB+ testScoreC+ testScoreD+ testScoreE/2);
average=sum/4;
System.out.println(" your grades are: "+" grade 1 = "+testScoreA+
" grade 2 = " +testScoreB+ " grade 3 = "+ testScoreC+ " grade 4 = "+ testScoreD+ " grade 5 = "+ testScoreE);
System.out.println("your grade average is :"+ average+".");
} while (testScoreA >= 0 && testScoreA < 101 );
if (testScoreA < 0 && testScoreA > 101 )
System.out.print("wront input ");
else System.out.println();
keyboard.close();
}
}
對於錯誤情況,應使用or或代替and:
所以代替:
else if (testScoreA < 0 && testScoreA > 101 )
您應該使用:
else if (testScoreA < 0 || testScoreA > 101 )
在第一行中,您正在檢查小於0且大於101的數字。據我所知,這是不可能的。 因此,如果您使用or或(||),則將檢查該數字是小於0還是大於101
這是一個示例,其中您閱讀了5個成績(如果它們不正確,請重復)並打印平均值:
import java.util.Scanner;
public class GradeCalculator {
//Maximum number of grades
private static final int NUM_SCORES = 5;
public void readGrade() {
Scanner keyboard = new Scanner(System.in);
// declaring the variables.
int sum = 0;
System.out.println("Welcome to the grade calculator , please Enter a grade between 0-101 ");
System.out.println("And press enter key on your keyboard to type the next score:");
for (int i = 0; i < NUM_SCORES; i++) {
int testScore;
do {
System.out.println("Please enter your test score:");
testScore = keyboard.nextInt();
//if the score is not correct: Output Error
if (testScore < 0 || testScore > 101)
System.err.println("Wront input. Inpute the test score again!");
else{
//if the score is correct, then add the score to the sum
sum += testScore;
}
//repeat if the score is not correct, to get a correct score
}while (testScore < 0 || testScore > 101);
}
//The average is the sum of the scores divided by the number of scores
System.out.println("Your grade average is :" + sum/NUM_SCORES);
keyboard.close();
}
public static void main(String[] args) {
new GradeCalculator().readGrade();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.