java如何重复userinput_java - 用户输入以重复Java程序 - 堆栈内存溢出

===============>>#1 票数:2

执行以下操作,您的代码将起作用:

替换所有input.nextInt(); 与Integer.parseInt(input.nextLine());

用(playAgain.equalsIgnoreCase("Y"))替换(playAgain == "Y" || playAgain == "y") (playAgain.equalsIgnoreCase("Y"))

相反,在开始时在inputGuess()内部初始化answer 。

取代的主体 if(playAgain.equalIgnoreCase("Y"))与inputGuess();

当您通过控制台输入整数值时,它还包含一个\\n (下一行)。 但是,当您使用nextInt() ,它不会读取此\\n ,但是当您尝试使用input.nextLine()获取下一行时,它将查找\\n (下一行),该值已经存在于整数条目中,并且此后一无所有。 代码查找“ Y”或“ y”并中断,因为找不到任何一个。

这就是为什么Integer.parseInt(input.nextLine()); 在这里工作

===============>>#2 票数:2 已采纳

这是完整的代码,完全可以使用并经过测试。

public static void main(String[] args)

{

String playAgain = "";

Scanner scan = new Scanner(System.in);

do

{

ClassName.inputGuess();

System.out.println("Would you like to play again? Enter Y to play or any other key to quit: ");

playAgain = scan.nextLine();

}

while(playAgain.equalsIgnoreCase("Y"));

System.out.println("Thanks for playing!");

}

public void inputGuess()

{

Random num = new Random();

int answer = num.nextInt(10)+1;

Scanner input = new Scanner(System.in);

int guess;

System.out.println("Enter a number between 1 and 10 as your first guess: ");

guess = input.nextInt();

do

{

if (guess < 1 || guess > 10)

{

System.out.println("That is not a valid entry. Please try again: ");

guess = input.nextInt();

}

else

if (guess > answer)

{

System.out.println("Too high, Try Again: ");

guess = input.nextInt();

}

else

if (guess < answer)

{

System.out.println("Too low, Try Again: ");

guess = input.nextInt();

}

input.nextLine();

}

while (guess != answer);

System.out.println("Congratulations, You guessed the number!");

}

===============>>#3 票数:1

这是代码:

private Random num = new Random();

private int answer = num.nextInt(10) +1;

private int guess;

private String playAgain;

Scanner input = new Scanner(System.in);

public void inputGuess(){

System.out.println("Enter a number between 1 and 10 as your first guess: ");

guess = input.nextInt();

do{

if (guess < 1 || guess > 10){

System.out.println("That is not a valid entry. Please try again: ");

guess = input.nextInt();

}else if (guess > answer){

System.out.println("Too high, Try Again: ");

guess = input.nextInt();

}else if (guess < answer){

System.out.println("Too low, Try Again: ");

guess = input.nextInt();

}

if(guess == answer) {

System.out.println("Congratulations, You guessed the number!");

System.out.println("Would you like to play again? Enter Y to play or any other key to quit: ");

playAgain = input.nextLine();

}

}while (!playAgain.equals("Y") && !playAgain.equals("y"));

}

您只需要在while内引入赢/输逻辑,条件就是结束/继续标记。

比较字符串以使用equals方法时始终要记住另一件事,因为==将比较对象引用而不是String值,在某些情况下,==对于相等的字符串将返回true,因为JVM存储字符串的方式是:确保始终使用equals 。

===============>>#4 票数:0

一期 :

不要用==比较两个字符串的内容,只应使用equals()

想象在这种情况下正确的答案是一个

按照此作为您的蓝图样本

int answer = 0;

String yes = "";

Scanner input = new Scanner(System.in);

do{

do{

System.out.println("Enter your number");

answer = input.nextInt();

} while ( answer != 1);

System.out.println("Would you like to play again?");

yes = input.next();

} while ( yes.equalsIgnoreCase("yes"));

输出:

aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9VOHNsVC5naWY=

===============>>#5 票数:0

尝试这样的事情:

public void inputGuess(){

System.out.println("Enter a number between 1 and 10 as your first guess: ");

Scanner input = new Scanner(System.in);

guess = input.nextInt();

playAgain = "Y";

do{

if (guess < 1 || guess > 10){

System.out.println("That is not a valid entry. Please try again: ");

guess = input.nextInt();

}else if (guess > answer){

System.out.println("Too high, Try Again: ");

guess = input.nextInt();

}else if (guess < answer){

System.out.println("Too low, Try Again: ");

guess = input.nextInt();

}

if(guess == answer)

{

System.out.println("Congratulations, You guessed the number!");

System.out.println("Would you like to play again? Enter Y to play or N to quit: ");

input.nextLine();

playAgain = input.next();

answer = num.nextInt(10);

guess = -1;

if(!playAgain.equalsIgnoreCase("N"))

{

System.out.println("Enter a number between 1 and 10 as your first guess: ");

guess = input.nextInt();

}

}

}while (!playAgain.equalsIgnoreCase("N"));

}

您需要您的代码来检查他们是否要在循环中再次播放。 这样,您就可以等到他们正确猜出数字后再询问是否要再次玩。 如果他们不这样做,那么您将重新启动该过程,否则,您将退出循环。

===============>>#6 票数:0

我上面看到的某些解决方案是不正确的。 随机数,您需要加1以获得1到10之间的值,还需要与equals进行比较。 我在这里不区分大小写。

以下代码根据需要工作。

import java.util.Random;

import java.util.Scanner;

public class Test2 {

private static Random num = new Random();

private static int answer = 0;

private static int guess;

private static String playAgain;

public static void main(String[] args) {

inputGuess();

}

// Guess Method.

public static void inputGuess(){

// create answer.

answer = 1+ num.nextInt(10);

System.out.println("Enter a number between 1 and 10 as your first guess: ");

Scanner input = new Scanner(System.in);

guess = input.nextInt();

do{

if (guess < 1 || guess > 10){

System.out.println("That is not a valid entry. Please try again: ");

guess = input.nextInt();

}else if (guess > answer){

System.out.println("Too high, Try Again: ");

guess = input.nextInt();

}else if (guess < answer){

System.out.println("Too low, Try Again: ");

guess = input.nextInt();

}

}while (guess != answer);

System.out.println("Congratulations, You guessed the number!");

System.out.println("Would you like to play again? Enter Y to play or any other key to quit: ");

playAgain = input.nextLine();

if( playAgain.equalsIgnoreCase("Y") ){

inputGuess();

}

}

}

===============>>#7 票数:0

到现在为止,您已经猜到了正确的方法。 这是我将如何处理的方法

public class Test {

public static void main (String...a) {

inputGuess();

}

public static void inputGuess() {

Scanner input = new Scanner(System.in);

String playAgain = "Y";

int guess;

Random ran = new Random();

int answer = ran.nextInt(10) + 1;

while (playAgain.equalsIgnoreCase("Y")) {

System.out.println("Enter a number between 1 and 10 as your first guess: " + answer);

guess = input.nextInt();

do {

if (guess < 1 || guess > 10) {

System.out.println("That is not a valid entry. Please try again: ");

guess = input.nextInt();

} else if (guess > answer) {

System.out.println("Too high, Try Again: ");

guess = input.nextInt();

} else if (guess < answer) {

System.out.println("Too low, Try Again: ");

guess = input.nextInt();

}

} while (guess != answer);

System.out.println("Congratulations, You guessed the number!");

System.out.println("Would you like to play again? Enter Y to play or any other key to quit: ");

input.nextLine();

playAgain = input.nextLine();

answer = ran.nextInt(10) + 1

}

}

}

===============>>#8 票数:0

该代码可以满足您的目的...

import java.util.Random;

import java.util.Scanner;

public class GuessingGame

{

private Random num = new Random();

private int answer ;

private int guess;

private String playAgain;

public void inputGuess()

{

answer = num.nextInt(11);

System.out.println("Enter a number between 1 and 10 as your first guess: ");

Scanner input = new Scanner(System.in);

guess = input.nextInt();

do {

if (guess < 1 || guess > 10) {

System.out

.println("That is not a valid entry. Please try again: ");

guess = input.nextInt();

} else if (guess > answer) {

System.out.println("Too high, Try Again: ");

guess = input.nextInt();

} else if (guess < answer) {

System.out.println("Too low, Try Again: ");

guess = input.nextInt();

}

} while (guess != answer);

System.out.println("Congratulations, You guessed the number!");

System.out.println("Would you like to play again? Enter Y to play or any other key to quit: ");

do

{

playAgain = input.nextLine();

}while(playAgain.length()<1);

if (playAgain.trim().equalsIgnoreCase("y"))

{

inputGuess();

}

else

{

System.out.println("Good Bye!!!");

}

}

public static void main(String[] args) {

new GuessingGame().inputGuess();

}

}

ask by Mac translate from so

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值