gpt4 book ai didi

java - 错误消息指出 java 的构造函数放错位置

转载 作者:太空宇宙 更新时间:2023-11-04 06:29:05 26 4
gpt4 key购买 nike

所以我对其进行了一些修改,但现在我收到了“完成 while 语句?”的额外错误?

/* This is a program in that can figure out a number chosen by a human user
*/
import java.util.Random;
import java.util.Scanner;

public class NumberGuesser {
public static void main(String[] args) {
boolean playAgain = false;

do {
playOneGame();
playAgain = shouldPlayAgain();
} while (playAgain);
}

public static void playOneGame() {
int continueGuessing = 0;
int guess = 50;
int low = 1;
int high = 100;
Scanner keyboard = new Scanner(System.in);
char response;

System.out.println("Guess a number between 1 and 100.\n");
do {
guess = getMidpoint (low, high);
response = getUserResponseToGuess(guess);
if (response == 'h') {
high = guess;
continueGuessing = 1;
} else if (response == 'l') {
low = guess;
continueGuessing = 0;
}
while(response != 'h' && response != 'l')
}
}

/*get midpoint method
*/
public static int getMidpoint(int high, int low);
int range = high- low;
Random generator = new Random();

if (range >0){
int midpoint = generator.nextInt(range) + low +1;
return midpoint;
} else
return guess;

public static char getUserResponseToGuess(int guess) {
Scanner keyboard = new Scanner(System.in);
String input;
char response;

do {
System.out.println("Is it " + guess + "? (h/l/c): ");
input = keyboard.nextLine();
response = input.charAt(0);
} while (response != 'h' && response != 'l' && response != 'c');
return response;
}

static boolean shouldPlayAgain() {
Scanner keyboard = new Scanner (System.in);
String input;
char response;

do {
System.out.println("Do you want to play again? (y/n); ");
input = keyboard.nextLine();
response = input.charAt(0);
} while (response != 'y' && response != 'n')

if (response == 'y') {
return true;
} else
return false;
}
}

所以我要创建一个程序,可以计算出人类用户选择的数字。人类用户会想到 1 到 100 之间的数字。程序将进行猜测,用户将告诉程序猜测更高或更低。

程序的示例运行可能如下所示:

  Guess a number between 1 and 100.
Is it 50? (h/l/c): h
Is it 75? (h/l/c): h
Is it 87? (h/l/c): l
Is it 81? (h/l/c): c
Great! Do you want to play again? (y/n): y
Guess a number between 1 and 100.
Is it 50? (h/l/c): l
Is it 25? (h/l/c): h
Is it 37? (h/l/c): c
Great! Do you want to play again? (y/n): n

到目前为止我所做的尝试给了我很多错误:

 /* This is a program in that can figure out a number chosen by a human user
*/
import java.util.Random;
import java.util.Scanner;

public class NumberGuesser {
public static void main(String[] args) {
boolean playAgain = false;
do{
playOneGame();
playAgain = shouldPlayAgain();
}while (playAgain);
}
/*get midpoint method
*/

public static void playOneGame() {
private int continueGuessing = 0;
private int guess = 50,
low = 1,
high = 100;
Scanner keyboard = new Scanner(System.in);
char response;
System.out.println("Guess a number between 1 and 100.\n");
do{
guess = getMidpoint(low, high, guess);
response = getUserResponseToGuess(guess);

if (response == 'h') {
high = guess;
continueGuessing = 1;
}

else if (response == 'l'){
low = guess;
continueGuessing = 0;
}
}

while (continueGuessing == 1);


public static int getMidpoint;
int range = high- low;
Random generator = new Random();

if (range >0){
int midpoint = generator.nextInt(range) + low +1;
return midpoint;
}

else
return guess;


public static char getUserResponseToGuess(int guess){
Scanner keyboard = new Scanner(System.in);
String input;
char response;

do {
System.out.println("Is it " + guess + "? (h/l/c): ");
input = keyboard.nextLine();
response = input.charAt(0);
}
while (response != 'h' && response != 'l' && response != 'c');

return response;
}

static boolean shouldPlayAgain() {
Scanner keyboard = new Scanner (System.in);
String input;
char response;

do{
System.out.println("Do you want to play again? (y/n); ");
input = keyboard.nextLine();
response = input.charAt(0);
}
while (response != 'y' && response != 'n');

if (response == 'y') {
return true;
}
else return false;

}
}

请帮忙!

最佳答案

Java 期望 do-while 语句如下所示:

do {
... statements
}
while (some test);

这是你在多个地方都有的。但在一个地方,这里:

   do {
guess = getMidpoint (low, high);
response = getUserResponseToGuess(guess);
if (response == 'h') {
high = guess;
continueGuessing = 1;
} else if (response == 'l') {
low = guess;
continueGuessing = 0;
}
while(response != 'h' && response != 'l')
} // <-- move this up to above the while

右大括号位置错误,它需要位于 while 之前。

当然 getMidpoint() 也需要大括号和参数列表。 public static int getMidpoint; 是用于类变量的语法(不能在方法中声明)。

// added arguments, opening brace
public static int getMidpoint(int low, int high, int guess) {
int range = high- low;
Random generator = new Random();

if (range >0){
int midpoint = generator.nextInt(range) + low +1;
return midpoint;
}

else
return guess;
} // added closing brace here

我的印象是,这是您已经知道的东西,您只是还没有形成防止错误蔓延所需的工作习惯。建议您一致地格式化代码并以匹配对的方式引入控制结构的评论是个好建议。如果您将代码分成小块编写,可以随时尝试,而不是一次输入所有内容,这也会有所帮助,因为一次遇到所有错误可能会让人不知所措。

关于java - 错误消息指出 java 的构造函数放错位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26370088/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com