我的错误消息如下所示:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at GPA3.answer(GPA3.java:364)
at GPA3.main(GPA3.java:355)
我有我的main方法,其余代码工作正常但是一旦我添加这段代码就会抛出错误。它不允许我在扫描仪中输入任何内容。
import java.util.Scanner;
public class GPA3 {
public static void main(String[] args)
{
System.out.println("Hello! Welcome to the GPA Calculator.");
int Math;
double mathGpa = 0;
System.out.println("Math = ");
Scanner input = new Scanner(System.in);
Math= input.nextInt();
if (Math >100){
System.out.println("You have mistyped something");
}
if (Math >= 94){
System.out.println("You have an A");
mathGpa = 4.0;
System.out.println(mathGpa);
}
if (Math < 94 && Math >=90){
System.out.println("You have an A-");
mathGpa = 3.7;
System.out.println(mathGpa);
}
if (Math < 90 && Math >=87){
System.out.println("You have a B+");
mathGpa = 3.3;
System.out.println(mathGpa);
}
if (Math < 87 && Math >=80){
System.out.println("You have a B");
mathGpa = 3.0;
System.out.println(mathGpa);
}
if (Math < 80 && Math >=77){
System.out.println("You have a B-");
mathGpa = 2.7;
System.out.println(mathGpa);
}
if (Math < 77 && Math >=73){
System.out.println("You have a C+");
mathGpa = 2.3;
System.out.println(mathGpa);
}
if (Math < 73 && Math >=70){
System.out.println("You have a C");
mathGpa = 2.0;
System.out.println(mathGpa);
}
if (Math < 70 && Math >=67){
System.out.println("You have a C-");
mathGpa = 1.7;
System.out.println(mathGpa);
}
if (Math < 67 && Math >=67){
System.out.println("You have a D+");
mathGpa = 1.3;
System.out.println(mathGpa);
}
if (Math < 67 && Math >=63){
System.out.println("You have a D");
mathGpa = 1.0;
System.out.println(mathGpa);
}
if (Math < 63 && Math >=60){
System.out.println("You have a D-");
mathGpa = 0.7;
System.out.println(mathGpa);
}
if (Math < 60){
System.out.println("You have a F");
mathGpa = 0.0;
System.out.println(mathGpa);
}//End of Math
int English;
double englishGpa = 0;
System.out.println("English = ");
English= input.nextInt();
if (English >100){
System.out.println("You have mistyped something");
}
if (English >= 94 && English <101){
System.out.println("You have an A");
englishGpa = 4.0;
System.out.println(englishGpa);
}
if (English < 94 && English >=90){
System.out.println("You have an A-");
englishGpa = 3.7;
System.out.println(englishGpa);
}
if (English < 90 && English >=87){
System.out.println("You have a B+");
englishGpa = 3.3;
System.out.println(englishGpa);
}
if (English < 87 && English >=80){
System.out.println("You have a B");
englishGpa = 3.0;
System.out.println(englishGpa);
}
if (English < 80 && English >=77){
System.out.println("You have a B-");
englishGpa = 2.7;
System.out.println(englishGpa);
}
if (English < 77 && English >=73){
System.out.println("You have a C+");
englishGpa = 2.3;
System.out.println(englishGpa);
}
if (English < 73 && English >=70){
System.out.println("You have a C");
englishGpa = 2.0;
System.out.println(englishGpa);
}
if (English < 70 && English >=67){
System.out.println("You have a C-");
englishGpa = 1.7;
System.out.println(englishGpa);
}
if (English < 67 && English >=67){
System.out.println("You have a D+");
englishGpa = 1.3;
System.out.println(englishGpa);
}
if (English < 67 && English >=63){
System.out.println("You have a D");
englishGpa = 1.0;
System.out.println(englishGpa);
}
if (English < 63 && English >=60){
System.out.println("You have a D-");
englishGpa = 0.7;
System.out.println(englishGpa);
}
if (English < 60){
System.out.println("You have a F");
englishGpa = 0.0;
System.out.println(englishGpa);
}//End of English
int Science;
double SciGpa = 0;
System.out.println("Science = ");
Science= input.nextInt();
if (Science >100){
System.out.println("You have mistyped something");
}
if (Science >= 94 && Science <101){
System.out.println("You have an A");
SciGpa = 4.0;
System.out.println(SciGpa);
}
if (Science < 94 && Science >=90){
System.out.println("You have an A-");
SciGpa = 3.7;
System.out.println(SciGpa);
}
if (Science < 90 && Science >=87){
System.out.println("You have a B+");
SciGpa = 3.3;
System.out.println(SciGpa);
}
if (Science < 87 && Science >=80){
System.out.println("You have a B");
SciGpa = 3.0;
System.out.println(SciGpa);
}
if (Science < 80 && Science >=77){
System.out.println("You have a B-");
SciGpa = 2.7;
System.out.println(SciGpa);
}
if (Science < 77 && Science >=73){
System.out.println("You have a C+");
SciGpa = 2.3;
System.out.println(SciGpa);
}
if (Science < 73 && Science >=70){
System.out.println("You have a C");
SciGpa = 2.0;
System.out.println(SciGpa);
}
if (Science < 70 && Science >=67){
System.out.println("You have a C-");
SciGpa = 1.7;
System.out.println(SciGpa);
}
if (Science < 67 && Science >=67){
System.out.println("You have a D+");
SciGpa = 1.3;
System.out.println(SciGpa);
}
if (Science < 67 && Science >=63){
System.out.println("You have a D");
SciGpa = 1.0;
System.out.println(SciGpa);
}
if (Science < 63 && Science >=60){
System.out.println("You have a D-");
SciGpa = 0.7;
System.out.println(SciGpa);
}
if (Science < 60){
System.out.println("You have a F");
SciGpa = 0.0;
System.out.println(SciGpa);
}//End of Science
int Spanish;
double SpanGpa = 0;
System.out.println("Spanish = ");
Spanish= input.nextInt();
if (Spanish >100){
System.out.println("You have mistyped something");
}
if (Spanish >= 94 && Spanish <101){
System.out.println("You have an A");
SpanGpa = 4.0;
System.out.println(SpanGpa);
}
if (Spanish < 94 && Spanish >=90){
System.out.println("You have an A-");
SpanGpa = 3.7;
System.out.println(SpanGpa);
}
if (Spanish < 90 && Spanish >=87){
System.out.println("You have a B+");
SpanGpa = 3.3;
System.out.println(SpanGpa);
}
if (Spanish < 87 && Spanish >=80){
System.out.println("You have a B");
SpanGpa = 3.0;
System.out.println(SpanGpa);
}
if (Spanish < 80 && Spanish >=77){
System.out.println("You have a B-");
SpanGpa = 2.7;
System.out.println(SpanGpa);
}
if (Spanish < 77 && Spanish >=73){
System.out.println("You have a C+");
SpanGpa = 2.3;
System.out.println(SpanGpa);
}
if (Spanish < 73 && Spanish >=70){
System.out.println("You have a C");
SpanGpa = 2.0;
System.out.println(SpanGpa);
}
if (Spanish < 70 && Spanish >=67){
System.out.println("You have a C-");
SpanGpa = 1.7;
System.out.println(SpanGpa);
}
if (Spanish < 67 && Spanish >=67){
System.out.println("You have a D+");
SpanGpa = 1.3;
System.out.println(SpanGpa);
}
if (Spanish < 67 && Spanish >=63){
System.out.println("You have a D");
SpanGpa = 1.0;
System.out.println(SpanGpa);
}
if (Spanish < 63 && Spanish >=60){
System.out.println("You have a D-");
SpanGpa = 0.7;
System.out.println(SpanGpa);
}
if (Spanish < 60){
System.out.println("You have a F");
SpanGpa = 0.0;
System.out.println(SpanGpa);
}//End of Spanish
int Religion;
double RGpa = 0;
System.out.println("Religion = ");
Religion= input.nextInt();
input.close();
if (Religion >100){
System.out.println("You have mistyped something");
}
if (Religion >= 94 && Religion < 101){
System.out.println("You have an A");
RGpa = 4.0;
System.out.println(RGpa);
}
if (Religion < 94 && Religion >=90){
System.out.println("You have an A-");
RGpa = 3.7;
System.out.println(RGpa);
}
if (Religion < 90 && Religion >=87){
System.out.println("You have a B+");
RGpa = 3.3;
System.out.println(RGpa);
}
if (Religion < 87 && Religion >=80){
System.out.println("You have a B");
RGpa = 3.0;
System.out.println(RGpa);
}
if (Religion < 80 && Religion >=77){
System.out.println("You have a B-");
RGpa = 2.7;
System.out.println(RGpa);
}
if (Religion < 77 && Religion >=73){
System.out.println("You have a C+");
RGpa = 2.3;
System.out.println(RGpa);
}
if (Religion < 73 && Religion >=70){
System.out.println("You have a C");
RGpa = 2.0;
System.out.println(RGpa);
}
if (Religion < 70 && Religion >=67){
System.out.println("You have a C-");
RGpa = 1.7;
System.out.println(RGpa);
}
if (Religion < 67 && Religion >=67){
System.out.println("You have a D+");
RGpa = 1.3;
System.out.println(RGpa);
}
if (Religion < 67 && Religion >=63){
System.out.println("You have a D");
RGpa = 1.0;
System.out.println(RGpa);
}
if (Religion < 63 && Religion >=60){
System.out.println("You have a D-");
RGpa = 0.7;
System.out.println(RGpa);
}
if (Religion < 60){
System.out.println("You have a F");
RGpa = 0.0;
System.out.println(RGpa);
}//End of Religion
answer();
}//End of Main
public static double answer()
{//Begin answer
double sClass=0;
System.out.println("Do you have gym this semester?");
Scanner input = new Scanner(System.in);
String yes = "yes";
String answer = input.nextLine();
if (answer.equals(yes)){
}
input.close();
return sClass;
}//End answer
}
答案 0 :(得分:0)
问题是您尝试在同一个流上同时打开两个不同的Scanner
对象 - System.in
- 两个都同时打开,这导致第二个扫描程序出现问题试图从流中读取。
您可以做的是让answer()
函数接受Scanner对象作为参数,然后使用它而不是在同一个流上创建一个新对象。
示例 -
.... // Lots of logic till here
}//End of Religion
answer(input);
}//End of Main
public static double answer(Scanner input)
{//Begin answer
double sClass=0;
System.out.println("Do you have gym this semester?");
String yes = "yes";
String answer = input.nextLine();
if (answer.equals(yes)){
}
input.close();
return sClass;
}//End answer
您还需要删除input.close();
,因为它会关闭System.in
的基础流。