当我执行我的程序时,程序显示 java.util.NoSuchElementException: No line found
When I execute my program the program displays java.util.NoSuchElementException: No line found
我对 java 有点陌生,我想知道我的代码哪里做错了。问题出在 "System.out.println(pw.next());" 此代码适用于 Pokemon 之类的游戏,扫描器应该扫描此人的用户名。我还远远没有完成代码,而且布局有点奇怪,因为我尝试自己修复错误。
如果有人有创建有趣游戏的提示,我们也将不胜感激。
package test;
import java.lang.Math;
import java.util.*;
public class Pokemon {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to Daniel's Game");
System.out.println("\nFire. Water. Earth. Air. The four nations lived in harmony until the Avatar attacked. \nFueled by anger, this avatar, whose name remains unknown, has sought to take over the world.\nLegends say that he lives deep in the Himalayas and wields the power of all four elements, but nobody can say for sure");
System.out.println("It is your job to save the world from catastrophe");
System.out.println("Are you up for the Challenge?");
String y = sc.next();
if(y.equals("yes")||y.equals("Yes")) {
System.out.println("You better be");
} else {
System.out.println("The world ends cuz you suck");
System.exit(0);
}
sc.close();
name();
}
public static void name() {
System.out.println("What is Your Name?");
Scanner pw = new Scanner(System.in);
String o = pw.next();
pw.close();
Scanner ew = new Scanner(System.in);
System.out.println("\nChoose a Pokemon: \n1. Fire \n2. Water\n 3. Earth \n4. Air");
int x = ew.nextInt();
ew.close();
}
}
下面应该有效:
package test;
import java.lang.Math;
import java.util.*;
public class Pokemon {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.println("Welcome to Daniel's Game");
System.out.println("\nFire. Water. Earth. Air. The four nations lived in harmony until the Avatar attacked. \nFueled by anger, this avatar, whose name remains unknown, has sought to take over the world.\nLegends say that he lives deep in the Himalayas and wields the power of all four elements, but nobody can say for sure");
System.out.println("It is your job to save the world from catastrophe");
System.out.println("Are you up for the Challenge?");
String y = sc.next();
if(y.equals("yes")||y.equals("Yes")) {
System.out.println("You better be");
} else {
System.out.println("The world ends cuz you suck");
System.exit(0);
}
// sc.close();
name();
}
public static void name() {
System.out.println("What is Your Name?");
String name = sc.next();
// Scanner pw = new Scanner(System.in);
// String o = pw.next();
// pw.close();
//Scanner ew = new Scanner(System.in);
System.out.println("\nChoose a Pokemon: \n1. Fire \n2. Water\n 3. Earth \n4. Air");
int x = sc.nextInt();
sc.close();
}
}
也许你的错误发生是因为你使用 Scanners
的方式不对,一个就够了,将它作为参数传递给 main class 是一个很好的习惯,因为你可以在之后关闭它函数终止。
试试这个...
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to Daniel's Game");
System.out.println("\nFire. Water. Earth. Air. The four nations lived in harmony until the Avatar attacked. \nFueled by anger, this avatar, whose name remains unknown, has sought to take over the world.\nLegends say that he lives deep in the Himalayas and wields the power of all four elements, but nobody can say for sure");
System.out.println("It is your job to save the world from catastrophe");
System.out.println("Are you up for the Challenge?");
String y = sc.next();
if (y.equals("yes") || y.equals("Yes")) {
System.out.println("You better be");
} else {
System.out.println("The world ends cuz you suck");
System.exit(0);
}
name(sc);
sc.close();
}
public static void name(Scanner sc) {
System.out.println("What is Your Name?");
String o = sc.next();
System.out.println("\nChoose a Pokemon: \n1. Fire \n2. Water\n 3. Earth \n4. Air");
int x = sc.nextInt();
}
我对 java 有点陌生,我想知道我的代码哪里做错了。问题出在 "System.out.println(pw.next());" 此代码适用于 Pokemon 之类的游戏,扫描器应该扫描此人的用户名。我还远远没有完成代码,而且布局有点奇怪,因为我尝试自己修复错误。
如果有人有创建有趣游戏的提示,我们也将不胜感激。
package test;
import java.lang.Math;
import java.util.*;
public class Pokemon {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to Daniel's Game");
System.out.println("\nFire. Water. Earth. Air. The four nations lived in harmony until the Avatar attacked. \nFueled by anger, this avatar, whose name remains unknown, has sought to take over the world.\nLegends say that he lives deep in the Himalayas and wields the power of all four elements, but nobody can say for sure");
System.out.println("It is your job to save the world from catastrophe");
System.out.println("Are you up for the Challenge?");
String y = sc.next();
if(y.equals("yes")||y.equals("Yes")) {
System.out.println("You better be");
} else {
System.out.println("The world ends cuz you suck");
System.exit(0);
}
sc.close();
name();
}
public static void name() {
System.out.println("What is Your Name?");
Scanner pw = new Scanner(System.in);
String o = pw.next();
pw.close();
Scanner ew = new Scanner(System.in);
System.out.println("\nChoose a Pokemon: \n1. Fire \n2. Water\n 3. Earth \n4. Air");
int x = ew.nextInt();
ew.close();
}
}
下面应该有效:
package test;
import java.lang.Math;
import java.util.*;
public class Pokemon {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.println("Welcome to Daniel's Game");
System.out.println("\nFire. Water. Earth. Air. The four nations lived in harmony until the Avatar attacked. \nFueled by anger, this avatar, whose name remains unknown, has sought to take over the world.\nLegends say that he lives deep in the Himalayas and wields the power of all four elements, but nobody can say for sure");
System.out.println("It is your job to save the world from catastrophe");
System.out.println("Are you up for the Challenge?");
String y = sc.next();
if(y.equals("yes")||y.equals("Yes")) {
System.out.println("You better be");
} else {
System.out.println("The world ends cuz you suck");
System.exit(0);
}
// sc.close();
name();
}
public static void name() {
System.out.println("What is Your Name?");
String name = sc.next();
// Scanner pw = new Scanner(System.in);
// String o = pw.next();
// pw.close();
//Scanner ew = new Scanner(System.in);
System.out.println("\nChoose a Pokemon: \n1. Fire \n2. Water\n 3. Earth \n4. Air");
int x = sc.nextInt();
sc.close();
}
}
也许你的错误发生是因为你使用 Scanners
的方式不对,一个就够了,将它作为参数传递给 main class 是一个很好的习惯,因为你可以在之后关闭它函数终止。
试试这个...
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to Daniel's Game");
System.out.println("\nFire. Water. Earth. Air. The four nations lived in harmony until the Avatar attacked. \nFueled by anger, this avatar, whose name remains unknown, has sought to take over the world.\nLegends say that he lives deep in the Himalayas and wields the power of all four elements, but nobody can say for sure");
System.out.println("It is your job to save the world from catastrophe");
System.out.println("Are you up for the Challenge?");
String y = sc.next();
if (y.equals("yes") || y.equals("Yes")) {
System.out.println("You better be");
} else {
System.out.println("The world ends cuz you suck");
System.exit(0);
}
name(sc);
sc.close();
}
public static void name(Scanner sc) {
System.out.println("What is Your Name?");
String o = sc.next();
System.out.println("\nChoose a Pokemon: \n1. Fire \n2. Water\n 3. Earth \n4. Air");
int x = sc.nextInt();
}