我撰寫了一個程式來接受兩個學生的資料,但是當我運行此代碼時,第二次跳過了“姓名”部分,它從主題開始并給出了 NameformatException?第一次運行代碼是正常的,但是當它第二次運行時,名稱部分被跳過,我必須直接輸入整數值,如果我不輸入,我會得到 NumberFormatException。
import java.util.Scanner;
@SuppressWarnings("serial")
class NegativeValueException extends Exception {
public NegativeValueException() {
}
}
@SuppressWarnings("serial")
class ValueOutOfRange extends Exception{
public ValueOutOfRange() {
System.out.println("Value out of range Exception");
}
}
public class Student1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for(int i=0;i<2;i ) {
String[] name = new String[2];
int sub1=0;
int sub2=0;
int sub3=0;
double avg;
try {
System.out.println("Enter the data of Student " (i 1) " : ");
name[i] = sc.nextLine();
if(name[i].equals(null))
break;
else
if(sc.hasNextInt())
sub1=sc.nextInt();
else
throw new NumberFormatException();
if(sc.hasNextInt())
sub2=sc.nextInt();
else
throw new NumberFormatException();
if(sc.hasNextInt())
sub3=sc.nextInt();
else
throw new NumberFormatException();
if(sub1<0)throw new NegativeValueException();
if(sub1>100)throw new ValueOutOfRange();
if(sub2<0)throw new NegativeValueException();
if(sub2>100)throw new ValueOutOfRange();
if(sub3<0)throw new NegativeValueException();
if(sub3>100)throw new ValueOutOfRange();
avg = (sub1 sub2 sub3)/3;
System.out.println("Name : " name[i]);
System.out.println("Marks in subject 1 : " sub1);
System.out.println("Marks in subject 2 : " sub2);
System.out.println("Marks in subject 3 : " sub3);
System.out.println("Average Marks : " avg);
}catch(NumberFormatException e) {
System.out.println(e);
}catch(NegativeValueException e) {
System.out.println(e);
}catch(ValueOutOfRange e) {
System.out.println(e);
}
}
sc.close();
}
}
uj5u.com熱心網友回復:
正如@Gurkirat 已經指出的錯誤,我只是將我的答案添加為對您的代碼的改進。
import java.util.Scanner;
@SuppressWarnings("serial")
class NegativeValueException extends Exception {
public NegativeValueException() {
System.out.println("Negative Value Exception");
}
}
@SuppressWarnings("serial")
class ValueOutOfRange extends Exception {
public ValueOutOfRange() {
System.out.println("Value out of range Exception");
}
}
public class Student1 {
public static void main(String[] args) {
String[] name = new String[2];
for(int i=0; i<2; i ) {
int sub1, sub2, sub3;
double avg;
try (Scanner sc = new Scanner(System.in)) {
System.out.println("Enter the data of Student " (i 1) " : ");
name[i] = sc.nextLine();
sub1 = sc.nextInt();
sub2 = sc.nextInt();
sub3 = sc.nextInt();
sc.next(); //Added this to solve your problem.
if(sub1 < 0 || sub2 < 0 || sub3 < 0)
throw new NegativeValueException();
if(sub1 > 100 || sub2 > 100 || sub3 > 100)
throw new ValueOutOfRange();
avg = (sub1 sub2 sub3)/3;
System.out.println("Name : " name[i]
"\nMarks in subject 1 : " sub1
"\nMarks in subject 2 : " sub2
"\nMarks in subject 3 : " sub3
"\nAverage Marks : " avg);
} catch(ValueOutOfRange | NegativeValueException e) {
System.out.println(e);
}
}
}
}
uj5u.com熱心網友回復:
問題出現在讀取輸入的第一個周期后,因為在 Scanner 類的最后一個scan.nextInt()方法之后,游標保持在同一行。因此,當您使用 nextLine() 作為名稱時,您的輸入將被視為輸入,當您輸入名稱時,它會顯示 NFE。[https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo]
要解決此問題,您可以做的是,在獲取最后一個整數輸入后,使用nextLine()使用剩余的行,然后您的代碼按預期作業,
import java.util.Scanner;
@SuppressWarnings("serial")
class NegativeValueException extends Exception {
public NegativeValueException() {
}
}
@SuppressWarnings("serial")
class ValueOutOfRange extends Exception {
public ValueOutOfRange() {
System.out.println("Value out of range Exception");
}
}
public class Student1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 2; i ) {
String[] name = new String[2];
int sub1 = 0;
int sub2 = 0;
int sub3 = 0;
double avg;
try {
System.out.println("Enter the data of Student " (i 1) " : ");
name[i] = sc.nextLine();
// sc.next();
if (name[i].equals(null))
break;
if (sc.hasNextInt())
sub1 = sc.nextInt();
else
throw new NumberFormatException();
if (sc.hasNextInt())
sub2 = sc.nextInt();
else
throw new NumberFormatException();
if (sc.hasNextInt())
sub3 = sc.nextInt();
else
throw new NumberFormatException();
if (sub1 < 0)
throw new NegativeValueException();
if (sub1 > 100)
throw new ValueOutOfRange();
if (sub2 < 0)
throw new NegativeValueException();
if (sub2 > 100)
throw new ValueOutOfRange();
if (sub3 < 0)
throw new NegativeValueException();
if (sub3 > 100)
throw new ValueOutOfRange();
avg = (sub1 sub2 sub3) / 3;
System.out.println("Name : " name[i]);
System.out.println("Marks in subject 1 : " sub1);
System.out.println("Marks in subject 2 : " sub2);
System.out.println("Marks in subject 3 : " sub3);
System.out.println("Average Marks : " avg);
sc.nextLine(); // add this
} catch (NumberFormatException e) {
System.out.println(e);
} catch (NegativeValueException e) {
System.out.println(e);
} catch (ValueOutOfRange e) {
System.out.println(e);
}
}
sc.close();
}
}
輸出是
Enter the data of Student 1 :
john
12
32
41
Name : john
Marks in subject 1 : 12
Marks in subject 2 : 32
Marks in subject 3 : 41
Average Marks : 28.0
Enter the data of Student 2 :
jack
12
43
55
Name : jack
Marks in subject 1 : 12
Marks in subject 2 : 43
Marks in subject 3 : 55
Average Marks : 36.0
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/426266.html
上一篇:API例外過濾器在ASP.NETMVC中回傳200例外回應代碼
下一篇:在回圈頭中捕獲例外但不在回圈體中
