Java 猜游戏错误(主要是扫描器)
编辑1-我将所有sc.nextInt更改为Integer.parseInt(sc.nextLine()),现在我有一些错误,但更少 编辑2-它现在运行,但我得到东西打印2倍,游戏没有结束在第10轮结束,我错过了一个总胜利线在结束。我认为错误发生在获胜之后 编辑3-2倍冷/热打印固定。修复了2x标题(在wins方法中重复左行)。新的(希望是最后一个错误)-第一轮游戏只允许3次尝试,其他轮4次。样本运行:Java 猜游戏错误(主要是扫描器),java,runtime-error,Java,Runtime Error,编辑1-我将所有sc.nextInt更改为Integer.parseInt(sc.nextLine()),现在我有一些错误,但更少 编辑2-它现在运行,但我得到东西打印2倍,游戏没有结束在第10轮结束,我错过了一个总胜利线在结束。我认为错误发生在获胜之后 编辑3-2倍冷/热打印固定。修复了2x标题(在wins方法中重复左行)。新的(希望是最后一个错误)-第一轮游戏只允许3次尝试,其他轮4次。样本运行: I will choose a number between 1 and 10 You hav
I will choose a number between 1 and 10
You have 3 tries to get it right
Round 1
Enter a new guess: 5
Cold
5
Cold
5
You lose!
You have won 0 out of 1 rounds.
Round 2
Enter a new guess: 5
Cold
5
Cold
5
Cold
5
You lose!
You have won 0 out of 2 rounds.
Round 3
Enter a new guess: 5
Cold
5
Cold
5
Cold
5
You lose!
You have won 0 out of 3 rounds.
第1类:
import java.util.Scanner;
public class GuessRunner
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("I will choose a number between 1 and 10" +
'\n' + "You have 3 tries to get it right" + '\n');
GuessCalc game = new GuessCalc();
while (game.getRounds() <= 10)
{
System.out.println('\n' + "Round " + game.getRounds() + '\n');
System.out.print("Enter a new guess: ");
int guess = Integer.parseInt(sc.nextLine());
do
{
game.rounds(guess);
guess = Integer.parseInt(sc.nextLine());
}
while (game.roundOver == false);
System.out.println(game.wins());
}
sc.close();
}
}
扫描仪的问题是,扫描仪非常非常奇怪。我已经用了好几年了,她讨厌我只把她用在快速和肮脏的输入上。我甚至不想开始描述发生的奇怪的事情,但解释通常围绕着非-nextLine()方法如何处理换行符(无论它们是否使用/忽略它们) 我对scanner的建议是只使用它的hasNextLine()和nextLine()方法。它们是我发现的唯一方法,因为使用它们的每个人都可以预测方法的行为。然后,您可以检查它是一个数字(匹配(“[1-9]+[0-9]*”),还是仅为野生型,直接执行Integer.parseInt() 看见 游戏=新的猜测计算(猜测); 是一个循环在类1中看起来很奇怪。这看起来像是一个错误,因为轮次将不断重置 编辑1: 如果您的代码不希望在每一轮重置随机数并在每一轮重置“尝试”计数(代码在每一轮都丢弃当前游戏),以下代码可能会帮助您:
import java.util.Scanner;
public class GuessRunner {
public static void main(String[] args) throws InterruptedException {
System.out.print("I will choose a number between 1 and 10" + '\n'
+ "You have 3 tries to get it right" + '\n');
GuessCalc game = new GuessCalc();
while (game.getRounds() <= 10) {
game.playRound();
System.out.println(game.wins());
}
}
}
您的代码格式不正确(无法编译),我不100%知道您的意图(例如,如果要在10轮中获得尽可能多的正确随机数,或者他们在10轮中有3次猜测,则未知)。祝你好运。尝试添加
sc.nextLine()代码>在sc.nextInt()之后代码>哪个将使用新行字符可以从我的第二个类获取输入吗?我可以在那里创建另一个方法来处理新的轮次,但我试图在我的客户机类(1)中保留所有输入@LazerWing,你可以,只是不像你的代码现在这样。在第二个类中,您使用的是nextInt,这就是导致新错误的原因。我将编辑我的答案。我的想法是每轮3次尝试,10轮,每轮都有一个新的数字。makeGuess方法的布尔返回的目的是什么我不认为它是正确的used@LazerWing如果是每个随机数三次尝试,则希望类在循环中循环。“makeGuess方法上的布尔返回的目的是什么?我看不到它被使用了”与之前返回的字符串的目的相同。
import java.util.Scanner;
public class GuessRunner {
public static void main(String[] args) throws InterruptedException {
System.out.print("I will choose a number between 1 and 10" + '\n'
+ "You have 3 tries to get it right" + '\n');
GuessCalc game = new GuessCalc();
while (game.getRounds() <= 10) {
game.playRound();
System.out.println(game.wins());
}
}
}
import java.util.Scanner;
public class GuessCalc {
private int wins, rounds = 0, num = (int) (1 + Math.random() * 10);
private int tries = 0;
private Scanner sc=new Scanner(System.in);
/**
* Constructs the game
*
* @param guess
* the player's guess
*/
public GuessCalc() {
}
/**
* Runs the rounds and determines if they win
*
* @return outcome if they won (true) or lost (false);
*/
public boolean playRound() {
startNewRound();
System.out.printf("Round %d \n\n", this.getRounds());
while(true){
System.out.println("Enter a new guess: ");
int guess = Integer.parseInt(sc.nextLine());
printHotOrCold(guess);
if (guess == num) {
wins++;
System.out.println("Jackpot! Setting a new random number");
return true;
}else if(tries==3){
System.out.println("Oops, didn't succeed. Better luck next time. The number was "+num);
return false;//ran out of tries
}else{
//got it wrong but still has guesses left
}
}
}
public final void startNewRound() {
rounds++;
tries = 0;
num = (int) (1 + Math.random() * 10);// set a new random number
}
/**
* Tells the player if they are hot or cold
*/
public void printHotOrCold(int guess) {
int offBy = guess - num;
if (offBy == 1 || offBy == -1) {// if they are over or under by 1
System.out.println("Hot");
} else if (guess != num) {// if they are further than 1 away
System.out.println("Cold");
}
tries++;
}
/**
* Returns the number of wins and makes a new header
*
* @return the String with the number of wins and new header
*/
public String wins() {
String record = String.format("You have won %d out of %d rounds. \n\n",wins,rounds);
return record;
}
/**
* Returns the number of rounds played
*
* @return rounds the number of rounds
*/
public int getRounds() {
return rounds;
}
}