我无法理解此代码的错误。在Dr.Java上 一切正常,但在另一个名为edhesive的代码运行平台(这是我被分配这个项目的地方)它给了我一个错误。我已经检查了一些我认为出错的但仍然不知道出了什么问题。
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Welcome. What is your name?");
String name = scan.next();
System.out.println("Hello " + name + ". Try your best to crack the code!");
System.out.println("PHASE 1");
System.out.println("Enter a number:");
int phaseOneNum = scan.nextInt();
if (phaseOneNum == 3)
{
System.out.println("Correct!");
System.out.println("PHASE 2");
System.out.println("Enter a number:");
int phaseTwoNum = scan.nextInt();
if (phaseTwoNum == 1 || (phaseTwoNum > 33 && phaseTwoNum<= 100))
{
System.out.println("Correct!");
System.out.println("PHASE 3");
System.out.println("Enter a number:");
int phaseThreeNum = scan.nextInt();
if (phaseThreeNum > 0 && ((phaseThreeNum % 3 == 0) || (phaseThreeNum % 7 == 0)))
{
System.out.println("Correct!");
System.out.println("You have cracked the code!");
}
else
{
System.out.println("Sorry, that was incorrect!");
System.out.println("Better luck next time!");
}
}
else
{
System.out.println("Sorry, that was incorrect!");
System.out.println("Better luck next time!");
}
}
else
{
System.out.println("Sorry, that was incorrect!");
System.out.println("Better luck next time!");
}
}
}
在edhesive上运行后,我收到此错误
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Main.main(Main.java:184)
at Ideone.test(Main.java:111)
at Ideone.test(Main.java:31)
at Ideone.main(Main.java:23)
有人能帮助我吗?
答案 0 :(得分:0)
运行时错误 - 线程“main”中的异常 java.util.InputMismatchException
scan.nextInt();
仅接受整数,您可以传递String。所以,它会给你java.util.InputMismatchException
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Welcome. What is your name?");
String name = scan.next();
System.out.println("Hello " + name + ". Try your best to crack the code!");
System.out.println("PHASE 1");
System.out.println("Enter a number:");
int phaseOneNum = 0;
do {
while (!scan.hasNextInt()) {
System.out.println("That's not a number!");
scan.next(); // this is important!
}
phaseOneNum = scan.nextInt();
} while (phaseOneNum <= 0);
if (phaseOneNum == 3) {
System.out.println("Correct!");
System.out.println("PHASE 2");
System.out.println("Enter a number:");
int phaseTwoNum;
do {
while (!scan.hasNextInt()) {
System.out.println("That's not a number!");
scan.next(); // this is important!
}
phaseTwoNum = scan.nextInt();
} while (phaseTwoNum <= 0);
if (phaseTwoNum == 1 || (phaseTwoNum > 33 && phaseTwoNum <= 100)) {
System.out.println("Correct!");
System.out.println("PHASE 3");
System.out.println("Enter a number:");
int phaseThreeNum;
do {
while (!scan.hasNextInt()) {
System.out.println("That's not a number!");
scan.next(); // this is important!
}
phaseThreeNum= scan.nextInt();
} while (phaseThreeNum<= 0);
if (phaseThreeNum > 0 && ((phaseThreeNum % 3 == 0) || (phaseThreeNum % 7 == 0))) {
System.out.println("Correct!");
System.out.println("You have cracked the code!");
} else {
System.out.println("Sorry, that was incorrect!");
System.out.println("Better luck next time!");
}
} else {
System.out.println("Sorry, that was incorrect!");
System.out.println("Better luck next time!");
}
} else {
System.out.println("Sorry, that was incorrect!");
System.out.println("Better luck next time!");
}
}
}