一直在努力解决这个问题。似乎无法弄清楚这个文件中的内容或主文件中的某些内容是否会导致它。这是我的数字猜谜游戏的代码:
package pkgjava.program;
import java.util.*;
public class NumberGame {
int decision;
void numberGame() {
do {
// Number game instructions
System.out.println();
System.out.println("Welcome to the number game! Enter a number between 0 and 20 below."
+ " The program will tell you if you need to enter a larger or smaller number. Let's begin!");
System.out.println();
System.out.println("Enter a number: ");
// Case 1 Variables
Scanner input = new Scanner(System.in);
Random rand = new Random();
int computerNumber = rand.nextInt(20);
while (!input.hasNextInt()) {
System.out.println();
System.out.println("Invalid entry! Please enter a number: ");
input.next();
}
int userNumber = input.nextInt();
int tries = 1;
if (userNumber > 20 || userNumber < 0) {
System.out.println();
System.out.println("Error: Number out of scope.\nTry again: ");
while (!input.hasNextInt()) {
System.out.println();
System.out.println("Invalid entry! Please enter a number: ");
input.next();
}
userNumber = input.nextInt();
} // IF user enters a smaller number...
else if (userNumber < computerNumber) {
do {
if (userNumber > 20 || userNumber < 0) {
System.out.println();
System.out.println("Error: Number out of scope.\nTry again: ");
while (!input.hasNextInt()) {
System.out.println();
System.out.println("Invalid entry! Please enter a number: ");
input.next();
}
userNumber = input.nextInt();
} else {
System.out.println();
System.out.println("Nope! The number you entered is smaller than mine. Try again: ");
while (!input.hasNextInt()) {
System.out.println("Invalid entry! Please enter a number: ");
input.next();
}
userNumber = input.nextInt();
tries++;
}
} while (userNumber < computerNumber);
} // IF user enters a larger number
else if (userNumber > computerNumber) {
do {
if (userNumber > 20 || userNumber < 0) {
System.out.println();
System.out.println("Error: Number out of scope.\nTry again: ");
while (!input.hasNextInt()) {
System.out.println();
System.out.println("Invalid entry! Please enter a number: ");
input.next();
}
userNumber = input.nextInt();
} else {
System.out.println();
System.out.println("Nope! Think smaller. Try again: ");
while (!input.hasNextInt()) {
System.out.println();
System.out.println("Invalid entry! Please enter a number: ");
input.next();
}
userNumber = input.nextInt();
tries++;
}
} while (userNumber > computerNumber);
} // IF user guesses correctly
else {
System.out.println();
System.out.println("You got it! My number was: " + computerNumber + "!" + " It took you " + tries
+ " tries. Would you like to play again? 1. Yes / 2. No");
while (!input.hasNextInt()) {
System.out.println("Invalid command! Please enter the number corresponding to your menu choice: ");
input.next();
}
this.decision = input.nextInt();
if (decision != 1) {
Main.check++;
}
else {
decision = 0;
}
}
} while(decision == 0);
}
}
这是我的主要课程:
/**
* @projectname Simple Java Program
* @author Joseph Kiebler
*/
package pkgjava.program;
import java.util.*;
public class Main {
static int check = 0;
public static void main(String[] args) {
Menu menu1 = new Menu();
NumberGame num1 = new NumberGame();
ConvertFraction conv1 = new ConvertFraction();
ChatSim sim1 = new ChatSim();
Scanner userIn = new Scanner(System.in);
System.out.println("Welcome to the program! Please consider the menu "
+ "options and enter the corresponding number below.");
System.out.println();
menu1.mainMenu();
while (!userIn.hasNextInt()) {
System.out.println();
System.out.println("Invalid entry! Try again: ");
userIn.next();
}
int choice = userIn.nextInt();
switch (choice) {
case 1:
num1.numberGame();
break;
case 2:
sim1.chatSim();
break;
case 4:
conv1.convertFraction();
break;
case 5:
System.out.println("Exiting program...");
System.exit(0);
break;
default:
System.out.println("Invalid selection! Try again: ");
userIn.nextInt();
}
while (check != 0) {
System.out.println("Welcome to the program! Please consider the menu "
+ "options and enter the corresponding number below.");
System.out.println();
menu1.mainMenu();
while (!userIn.hasNextInt()) {
System.out.println();
System.out.println("Invalid entry! Try again: ");
userIn.next();
}
choice = userIn.nextInt();
switch (choice) {
case 1:
num1.numberGame();
break;
case 2:
sim1.chatSim();
break;
case 4:
conv1.convertFraction();
break;
case 5:
System.out.println("Exiting program...");
System.exit(0);
break;
default:
System.out.println("Invalid selection! Try again: ");
userIn.nextInt();
}
}
System.out.println("Exiting program...");
}
}
我知道我使用过的方法并不是最有效的方法,但这是我的第一种编程语言,而且我已经做了两周了,所以......放轻松我:P。无论如何,这两个类中是否存在任何会导致上述错误的冲突?
答案 0 :(得分:3)
问题是你只会在if-else语句中进入其中一个案例。
你的程序假设一旦你进入其中一个案例,比如说(userNumber&lt; computerNumber),一旦用户输入了正确的数字,它就会点击那个else语句。不幸的是,它并没有这样做。在像这样的if-else块中,你只会选择其中一个案例,然后它将跳到else语句之后的任何内容。一旦已经选择了案例,它就不会重新评估案例。下一步是循环回到程序的开头,这样一旦它完成评估&#34; else-if&#34;它就会做什么。声明。
您需要做的是确保循环回if语句,而不是程序的开头。这样做的一种可能方法是布尔标志,表示您何时选择了正确的答案,以及一个测试该值的while循环。这样,当找到正确答案时,您可以将标志设置为true,如果不是,则可以循环回if语句的开头。