繁体   English   中英

Java HiLo游戏-无法生成新的猜测

[英]Java HiLo Game - Can't Generate New Guesses

抱歉,我知道这个游戏还有其他帖子,但是我的问题很具体。 我刚开始学习Java,并一起玩了这款HiLo游戏,但我正努力降低2件事-

1)如果用户想再次玩,我如何获得它以产生新的选择? 现在,如果您再次玩游戏,您将获得相同的选择权2)如果用户选择中止游戏,则不会产生消息

求求您,我是我的新手,并且容易受Java的侵扰!

package lab4;

import java.util.Scanner;
import java.util.Random;

public class hilo 
{

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        //randomly pick a number between 1-100
        Random rand = new Random();
        int pick = rand.nextInt(100) + 1;
        int count = 0;
        boolean play = true;

        //prompt user to guess      
        Scanner scan = new Scanner(System.in);
        System.out.println("*---------------------------------*");
        System.out.println("* Guess a number between 1-100:   *");
        System.out.println("* (enter \"0\" to end at any time)  *");
        System.out.println("*---------------------------------*");
        int guess = scan.nextInt();

        //on each guess, say if too low or too high
        while (play = true)
        {   
            while (guess != pick && guess !=0)
            {

                if (guess < pick)
                {
                    count++;
                    System.out.println("Higher...");
                    System.out.println(pick);
                    guess = scan.nextInt();
                }

                else if (guess > pick)
                {  
                    count++;
                    System.out.println("Lower...");
                    System.out.println(pick);
                    guess = scan.nextInt();
                }

            }


            if (guess == pick)
            {   
                System.out.println("You got it! It only took you " + count + " guesses.");
                System.out.println("Want to play again?");
                System.out.println("* Guess a number between 1-100:   *");
                System.out.println("* (enter \"0\" to end at any time)  *");
                guess = scan.nextInt();
                if (guess != 0)
                    play = true;
                else
                    play = false;
            }
        }

        if (play = false || guess == 0) 
        {
            System.out.println("Bummer. See you next time!");
        }

     }
}

两件事

int pick = rand.nextInt(100) + 1;
// ...

//on each guess, say if too low or too high
while (play = true)
{   

一个=是分配,两个==是相等。 所以while (play == true) (冗长) while (play) 然后将pick移入循环。

// int pick = rand.nextInt(100) + 1;
// ...
while (play) {   
    int pick = rand.nextInt(100) + 1;

那应该解决两个问题。

只是分配一个新的值pick你,如果条件变量,你测试的平等guesspick

if (guess == pick)
{   
  System.out.println("You got it! It only took you " + count + " guesses.");
  System.out.println("Want to play again?");
  System.out.println("* Guess a number between 1-100:   *");
  System.out.println("* (enter \"0\" to end at any time)  *");
  guess = scan.nextInt();

  pick = rand.nextInt(100) + 1; // new value

  if (guess != 0)
    play = true;
  else
    play = false;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM