这个程序是一个猜谜游戏,你在a和j之间输入一个字母,并尝试猜测随机选择的答案。它工作正常,但我想使用for循环来包含所有三个猜测,而不是分别写出它们。关于如何开始这个的任何建议?
import java.util.Scanner;
import java.lang.Math;
import java.util.Random;
class Guess{
public static void main( String[] args ){
Scanner sc = new Scanner(System.in);
System.out.println("I'm thinking of a letter in the range a to j. You have three guesses. ");
for(int i = 0; i<3; i++){ //this is where I'm having trouble
System.out.print("Enter your first guess: ");
Random r = new Random();
char i = (char)(r.nextInt(10) + 'a');
char b = sc.next().charAt(0);
if(b>i){
System.out.println("Your guess is too high. ");
} else if(b<i){
System.out.println("Your guess is too low. ");
} else if(b==i){
System.out.println("You win! ");
return;
}
System.out.print("Enter your second guess: ");
b = sc.next().charAt(0);
if(b>i){
System.out.println("Your guess is too high. ");
} else if(b<i){
System.out.println("Your guess is too low. ");
} else if(b==i){
System.out.println("You win! ");
return;
}
System.out.print("Enter your third guess: ");
b = sc.next().charAt(0);
if(b>i){
System.out.println("Your guess is too high. ");
} else if(b<i){
System.out.println("Your guess is too low. ");
} else if(b==i){
System.out.println("You win! ");
}
if(b!=i){
System.out.println("You lose. The letter was " +i+ ".");
}
}
}
答案 0 :(得分:2)
将用户猜测的角色存储在循环外的变量中。
然后在循环内部让他们尝试猜测数字i + 1,其中i是循环计数器(或者你可以改为使用基于1的索引)。
如果在循环之后继续执行,则意味着它们丢失了。
以下是一个例子:
import java.util.Random;
import java.util.Scanner;
class Guess {
public static void main(String[] args) {
int maxGuesses = 3;
System.out.println("I'm thinking of a letter in the range a to j. You have " + maxGuesses + " guesses. ");
Random r = new Random();
char i = (char) (r.nextInt(10) + 'a');
Scanner sc = new Scanner(System.in);
String[] guessDescription = { "First", "Second", "Third" };
for (int g = 0; g < maxGuesses; g++) {
// use predefined guess description for 1-3, otherwise a generic description that works for any number of guesses above 3
if (g < guessDescription.length) {
System.out.print("Enter your " + guessDescription[g] + " guess:");
} else {
System.out.print("Enter guess #" + (g + 1) + ":");
}
char b = sc.next().charAt(0);
if (b > i) {
System.out.println("Your guess is too high. ");
} else if (b < i) {
System.out.println("Your guess is too low. ");
} else if (b == i) {
System.out.println("You win! ");
return;
}
}
System.out.println("You lose. The letter was " + i + ".");
}
}
答案 1 :(得分:0)
您只需要更换行输入您的....猜测。
基于i,你应该保留像
这样的映射
{ 0:First,1:Second,2:Third}
Scanner sc = new Scanner(System.in);
System.out.println("I'm thinking of a letter in the range a to j. You have three guesses. ");
List < String > strList = new ArrayList <String> ( );
strList.add("FIRST");
strList.add("SECOND");
strList.add("THIRD");
Random r = new Random();
char randX = (char)(r.nextInt(10) + 'a');
for(int i = 0; i<3; i++){ //this is where I'm having trouble
System.out.print("Enter your"+ strList.get(i)+ " guess: ");
char b = sc.next().charAt(0);
if(b==i){
System.out.println("You win! ");
return;
}
else
{
if(b>randX)
{
System.out.println("Your guess is too high. ");
}
else
System.out.println("Your guess is too low. ");
if ( b !=randX && i==2 )
{
System.out.println("You lose. The letter was " + randX + ".");
}
}
}
答案 2 :(得分:0)
你可以使用嵌套循环。 这部分基本相同,可以在内循环(3次迭代)
System.out.print("Enter your ... guess: ")
char b = sc.next().charAt(0);
if(b>i){
System.out.println("Your guess is too high. ");
} else if(b<i){
System.out.println("Your guess is too low. ");
} else if(b==i){
System.out.println("You win! ");
return;
}
但System.out.print("Enter your ... guess: ")
每次迭代都不同。您可以使用字符串{“first”,“second”,“third”}创建字符串的const数组,并通过循环索引访问正确的字符串(在此处使用格式字符串...)
答案 3 :(得分:0)
将您的信件映射到列表&lt;&gt;或ArrayList&lt;&gt;然后使用for-each循环
检查此answer以获取如何使用for-each循环的简单示例:
答案 4 :(得分:0)
这是一个快速示例,如果游戏结束,可以再次播放循环
class Guess {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random r = new Random();
char toGuess;
char yourGuess;
boolean playing = true;
boolean lost = true;
while(playing) {
toGuess = (char) (r.nextInt(10) + 'a');
System.out.println("NEW GAME");
System.out.println("========");
System.out.println("I'm thinking of a letter in the range a to j. You have three guesses. ");
for (int j = 0; j < 3; j++) {
switch (j) {
case 0:
System.out.print("Enter your first guess: ");
break;
case 1:
System.out.print("Enter your second guess: ");
break;
case 2:
System.out.print("Enter your third guess: ");
break;
default:
System.out.println("Something went wrong. Game is closing!");
System.exit(0);
break;
}
yourGuess = sc.next().charAt(0);
if (yourGuess > toGuess) {
System.out.println("Your guess is too high.");
} else if (yourGuess < toGuess) {
System.out.println("Your guess is too low. ");
} else if (yourGuess == toGuess) {
System.out.println("You win! ");
lost = false;
break;
}
}
if(lost==false){
System.out.println("Sorry, you lost!");
}
System.out.print("Do you want to play again? (y=yes | n=no): ");
if(!sc.next().equals("y")){
playing=false;
}
}
}
}