这是学校的一项胶粘剂作业,它使用一种称为“代码运行器”的东西来运行和检查代码,每次我将代码放入其中时,都会给我带来运行时错误。当我在Java博士上使用它时,它可以很好地工作并且完全没有错误。我不知道为什么会这样。我是编码新手,只了解Java基础知识。
测试时,我仅使用整数。我没有输入任何文字或字母。仅当我将其放在Code Runner中时才会出现此错误。代码运行器会自动对其进行测试。
这是我自己编写的代码,它永远无法工作。
import java.io.*;
import static java.lang.System.*;
import java.util.Scanner;
import java.lang.Math;
class Main {
public static void main(String str[]) throws IOException {
Scanner scan = new Scanner(System.in);
int num1 = 3;
int num2 = (int) (Math.random() * 100) + 33;
int num3 = 21;
int c = 0;
System.out.println("PHASE 1");
System.out.println("Enter a number: ");
while (c != 5) {
int an1 = scan.nextInt();
if (an1 == num1) {
System.out.println("Correct!");
c = 5;
} else
c++;
}
System.out.println("PHASE 2");
System.out.println("Enter a number: ");
c = 0;
while (c != 5) {
int an2 = scan.nextInt();
if (an2 == num2) {
System.out.println("Correct!");
c = 5;
} else
c++;
}
System.out.println("PHASE 3");
System.out.println("Enter a number: ");
c = 0;
while (c != 5) {
int an3 = scan.nextInt();
if (an3 == num3) {
System.out.println("Correct!");
c = 5;
} else
c++;
}
}
}
这是我不断遇到的运行时错误。
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:190)
at Ideone.test(Main.java:111)
at Ideone.test(Main.java:31)
at Ideone.main(Main.java:23)
/*
Chris Walter
8/9/18
Assignment 3: Crack the code
In this assignment, you will be creating a program that requires a secret code to “unlock.” The program should first welcome the user and ask the user to input his/her name. Then the program will greet the user using the entered name.
In order to “crack the code,” the user must input three integer numbers which satisfy the following conditions:
The first number must be the number 3.
The second number can either be the number 1 or be between 33 and 100, inclusive.
The third number must be a positive number that is either evenly divisible by 3 or evenly divisible by 7
As the user enters each number, the program should immediately check whether or not the number satisfies its respective requirement(s) listed above. If the requirement(s) is satisfied, then a “Correct!” confirmation should be printed out. If the requirement(s) is NOT satisfied, then an error message should be printed out, and the user cannot input any more numbers. (i.e. if the user input 5 as the first number, then the user has failed to crack the code, and cannot guess the second or third number.)
Please refer to the sample outputs below for formatting. (Any output statements from the program must match the sample formatting exactly.)
Sample run 1:
Welcome. What is your name?
Jane Doe
Hello Jane Doe. Try your best to crack the code!
PHASE 1
Enter a number:
3
Correct!
PHASE 2
Enter a number:
40
Correct!
PHASE 3
Enter a number:
6
Correct!
You have cracked the code!
Sample run 2:
Welcome. What is your name?
Jane Doe
Hello Jane Doe. Try your best to crack the code!
PHASE 1
Enter a number:
0
Sorry, that was incorrect!
Better luck next time!
Sample Run 3:
Welcome. What is your name?
John Doe
Hello John Doe. Try your best to crack the code!
PHASE 1
Enter a number:
3
Correct!
PHASE 2
Enter a number:
2
Sorry, that was incorrect!
Better luck next time!
*/
import java.util.Scanner;
import java.lang.Math;
class Main {
public static void main(String[] args) {
// Setup the scanner
Scanner scan = new Scanner(System.in);
System.out.println("Hello, what is your name?");
String name = scan.nextLine();
System.out.println("Hello " + name + ". Try your best to crack the code!");
System.out.println("\nPHASE 1");
System.out.println("Enter a number");
int first = scan.nextInt();
if (first == 3) {
System.out.println("Correct!");
System.out.println("\nPHASE 2\nEnter a number");
int second = scan.nextInt();
if (second == 1 || (second >= 33 && second <= 100)) {
System.out.println("Correct!");
System.out.println("\nPHASE 3\nEnter a number");
int third = scan.nextInt();
if (third > 0 && (third % 3 == 0 || third % 7 == 0))
{
System.out.println("Correct!\nYou have cracked the code!");
}
else {
youLost();
}
}
else {
youLost();
}
}
else {
youLost();
}
}
public static void youLost () {
System.out.println("Sorry, that was incorrect!\nBetter luck next time!");
}
}
此代码是我在网上找到的,并且在Code Runner上运行良好。它还包括我需要编写的代码和示例代码。有人可以告诉我区别是什么,以便将来我可以再避免这个问题。
答案 0 :(得分:0)
错误Exception in thread "main" java.util.InputMismatchException
表示您的程序正在尝试读取一个整数,但没有得到一个整数。
由于您是在非实际的Java平台上运行此程序,因此可能存在一个错误,而不是代码中的错误。
堆栈跟踪显示的不是您的代码的事实:
...
at Main.main(Main.java:190)
at Ideone.test(Main.java:111)
at Ideone.test(Main.java:31)
at Ideone.main(Main.java:23)
使它更具可疑性(您的代码没有190行)
我建议在标准平台上运行。
我运行您的代码,它运行得很好:
~/c/j/$ javac Main.java
~/c/j/$ java Main
PHASE 1
Enter a number:
1
3
Correct!
PHASE 2
Enter a number:
1
2
3
4
5
PHASE 3
Enter a number:
6
7
21
Correct!
所以您使用的工具肯定有问题。
因此,继续安装Java编译器并学习如何编译和执行Java程序,乍一看似乎令人生畏,但越早熟悉它越好。