这是代码
package Question2;
import java.util.Scanner;
import java.util.Random;
public class PlayTheGame {
public static void main(String[]args){
Random randomNumber = new Random();
int numberToGuess;
int numberOfTries;
int numberOfLimit=5;
int UserInput;
Scanner scan = new Scanner (System.in);
int start =1;
boolean winTracker =false;
while(1==start){
numberToGuess=randomNumber.nextInt(10);
UserInput = 0;
numberOfTries=0;
System.out.println("Please guess the number between 1 and 10");
while(numberToGuess!=UserInput && numberOfTries < numberOfLimit){
UserInput=scan.nextInt();
numberOfTries++;
if (UserInput == numberToGuess){
System.out.println("You win!");
System.out.println("The guess number is "+numberToGuess+"and it's correct");
System.out.println("It took a total of "+numberOfTries+"guesses");
}
If user has guessed in correctly, how to print the number that user input and tell the user that these are the numbers that you have input?
else if (UserInput < numberToGuess){
System.out.println("Oppss..The number is too small");
System.out.println("Try next guess :");
}
else if (UserInput > numberToGuess){
System.out.println("Oppss..The number is too Highh");
System.out.println("Try next guess");
}
}
if (numberToGuess!=userInput){
System.out.println("Oppss..You are out guess limitation!The correct number is : "+numberToGuess);
}
}
}
}
如何在猜谜游戏中打印出用户输入的数字? 任何帮助将不胜感激。
答案 0 :(得分:1)
由于UserInput实际上是用户输入的内容,因此只需像其他任何内容一样打印。
else if (UserInput < numberToGuess){
System.out.println("You have guessed: "+UserInput); // <------ prints the user's choice
System.out.println("Oppss..The number is too small");
System.out.println("Try next guess :");
}
///在您的评论之后编辑
每次用户输入数字时,请将其存储在arraylist中。
ArrayList <Integer> inputs = new ArrayList();
然后每当用户输入一个数字时,
inputs.add(UserInput);
当该人获胜时,只需将其打印出来即可。
for(int i=0; i<inputs.size(); i++){
System.out.print(inputs.get(i) + ",");
}
以下是代码的外观:
ArrayList <Integer> inputs = new ArrayList();
Scanner sc = new Scanner(System.in);
int UserInput = sc.nextInt();
.... code
else if (UserInput < numberToGuess){ // lets say the person got the number wrong, add to the list
inputs.add(UserInput);
System.out.println("You have guessed so far: "+ inputs);
System.out.println("Oppss..The number is too small");
System.out.println("Try next guess :");
}
编辑 - 最终的RUNNABLE CODE
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
public class PlayTheGame {
public static void main(String[] args) {
Random randomNumber = new Random();
int numberToGuess;
int numberOfTries;
int numberOfLimit = 5;
int UserInput;
ArrayList <Integer> prevInputs = new ArrayList();
Scanner scan = new Scanner(System.in);
int start = 1;
boolean winTracker = false;
while (1 == start) {
numberToGuess = randomNumber.nextInt(10);
UserInput = 0;
numberOfTries = 0;
System.out.println("Please guess the number between 1 and 10");
while (numberToGuess != UserInput && numberOfTries < numberOfLimit) {
UserInput = scan.nextInt();
numberOfTries++;
if (UserInput == numberToGuess) {
System.out.println("You win!");
System.out.println("The guess number is " + numberToGuess + "and it's correct");
System.out.println("It took a total of " + numberOfTries + "guesses");
prevInputs.clear(); // removes all the elements when you win for a new game
} else if (UserInput < numberToGuess) {
prevInputs.add(UserInput);
System.out.println("So far you have guessed: "+prevInputs);
System.out.println("Oppss..The number is too small");
System.out.println("Try next guess :");
} else if (UserInput > numberToGuess) {
prevInputs.add(UserInput);
System.out.println("So far you have guessed: "+prevInputs);
System.out.println("Oppss..The number is too Highh");
System.out.println("Try next guess");
}
}
if (numberToGuess != UserInput) {
System.out.println("Oppss..You are out guess limitation!The correct number is : "
+ numberToGuess);
}
}
}
}
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
public class PlayTheGame {
public static void main(String[] args) {
Random randomNumber = new Random();
int numberToGuess;
int numberOfTries;
int numberOfLimit = 5;
int UserInput;
ArrayList <Integer> prevInputs = new ArrayList();
boolean play = true;
Scanner scan = new Scanner(System.in);
int start = 1;
boolean winTracker = false;
while (play) {
numberToGuess = randomNumber.nextInt(10);
UserInput = 0;
numberOfTries = 0;
while (numberToGuess != UserInput && numberOfTries < numberOfLimit) {
System.out.println("Please guess the number between 1 and 10 ");
UserInput = scan.nextInt();
numberOfTries++;
if (UserInput == numberToGuess) {
System.out.println("You win!");
System.out.println("The guess number is " + numberToGuess + " and it's correct");
System.out.println("It took a total of " + numberOfTries + " guesses");
prevInputs.clear(); // removes all the elements when you win for a new game
System.out.println("Would you like to play again? y/n");
String playAgain = scan.next();
if(playAgain.equals("n")){
System.out.println("Thank you for playing!");
play = false;
}
} else if (UserInput < numberToGuess) {
prevInputs.add(UserInput);
System.out.println("So far you have guessed: "+prevInputs);
System.out.println("Oppss..The number is too small");
System.out.println("Try next guess :");
} else if (UserInput > numberToGuess) {
prevInputs.add(UserInput);
System.out.println("So far you have guessed: "+prevInputs);
System.out.println("Oppss..The number is too Highh");
System.out.println("Try next guess");
}
}
if (numberToGuess != UserInput) {
System.out.println("Oppss..You are out guess limitation!The correct number is : "
+ numberToGuess);
}
}
}
} //hey how is it going?
答案 1 :(得分:0)
一种方法是添加列表
ArrayList<Integer> guessesSoFar = new ArrayList<>();
然后在numberOfTries++;
添加此
guessesSoFar.add(userInput);
然后输出
System.out.println("Guesses so far: " + Arrays.toString(guessesSoFar.toArray()));
PS。在userInput
;)