0

如果用户说“是”,我被困在你需要从 ArrayList 中选择一个随机引用并再次循环的地方。

我应该使用任何 for 循环或 while 循环之类的吗?请让我知道我应该朝哪个方向前进。

以下是我到目前为止所做的:

while (userGuess != numToGuess) {
    if (userGuess < numToGuess) {
        System.out.println("too low; try again");
        userGuess = s.nextInt();

    } else if (userGuess > numToGuess) {
        System.out.println("too high; try again");
        userGuess = s.nextInt();

    } else {
        System.out.println("wrong; try again!");
        userGuess = s.nextInt();
    }
}
4

4 回答 4

2
import java.util.Scanner; 

imprt 并添加如下更改代码,

public void guessMyNumber()
{
  Random r = new Random();
  numToGuess = r.nextInt(MAX) + 1;
  Scanner input = new Scanner(System.in);
  String answer;
  do{

     System.out.println("Please guess a number between 1 and 10");
     userGuess = s.nextInt();

     while(userGuess != numToGuess)
     {
         if(userGuess < numToGuess)
         {
             System.out.println("too low; try again");
             userGuess = s.nextInt();
         }
         else if(userGuess > numToGuess){
             System.out.println("too high; try again");
             userGuess = s.nextInt();
         }
         else{
             System.out.println("wrong; try again!");
             userGuess = s.nextInt();
         }
     }
     System.out.println("Congratulations! You've won a quotation.");
     System.out.println("Do you want to continue ?(Yes/No)");
     System.out.println("Quotation is :"+numbersget(r.nextInt(numbers.size())));
     answer=input.next();
  }while((answer.equalsIgnoreCase("Yes")) 
} 

这应该这样做。

于 2013-07-03T05:19:42.757 回答
1

一种简单的方法是将数组作为 list.toArray() 从列表中取出。然后在数组大小上使用 Math.Random 随机选择数组索引并使用它来选择随机引用。

于 2013-07-03T05:22:45.707 回答
0
public static void guessMyNumber()
    {
        Random r = new Random();
        String playAgain="Yes";
        while(playAgain.equalsIgnoreCase("Yes")){
             numToGuess = r.nextInt(MAX) + 1;
        System.out.println("Please guess a number between 1 and 10");
        userGuess = s.nextInt();

        while(userGuess != numToGuess)
        {
            if(userGuess < numToGuess)
            {
                System.out.println("too low; try again");
                userGuess = s.nextInt();
            }
            else if(userGuess > numToGuess){
                System.out.println("too high; try again");
                userGuess = s.nextInt();
            }
            else{
                System.out.println("wrong; try again!");
                userGuess = s.nextInt();
            }
        }
        System.out.println("Congratulations! You've won a quotation.");
        System.out.println("Quotation is :"+numbers.get(r.nextInt(numbers.size())));
        System.out.println("play again? Yes or No?");
        playAgain = s.next();
    }
    }
于 2013-07-03T05:30:20.523 回答
0

你想在这里达到什么目的?

   if(userGuess < numToGuess)
    {
        System.out.println("too low; try again");
        userGuess = s.nextInt();
    }
    else if(userGuess > numToGuess){
        System.out.println("too high; try again");
        userGuess = s.nextInt();
    }
    else{
        System.out.println("wrong; try again!");
        userGuess = s.nextInt();
    }

如果userGuess小于numToGuess你说它“太低”,如果它大于你说“太高”。如果相等,你说“错了;再试一次”。可怜的用户会怎么做?:P

对于它的价值......最后的 else 情况永远不会发生(并且应该被删除),因为你不会进入 while 循环 if userGuess == numToGuess

于 2013-07-03T05:31:39.710 回答