在猜想Java

时间:2016-11-30 13:20:20

标签: java

我正在用Java思考Java:如何像计算机科学家那样思考?书中没有练习答案,通常我最终会在不同的网站上找到类似的练习,但不是因为我有精确的指示。你能告诉我它是否正确吗? 我认为问题已经解决,工作已经完成,但是有更简单的方法吗? 非常感谢

练习5-7。 现在我们有条件语句,我们可以回到练习3-4中的“猜猜我的数字”游戏。 您应该已经有一个选择随机数的程序,提示用户猜测它,并显示猜测和所选数字之间的差异。 一次添加少量代码,并在进行测试时修改程序,以便告知用户猜测是否过高或过低,然后提示用户进行另一次猜测。 该程序应该继续,直到用户正确。提示:使用两种方法, 并使其中一个递归。

import java.util.Random;
import java.util.Scanner;
public class GuessStarter {

  public static void Lower(int number,int number2) {
    Scanner in = new Scanner(System.in);
    System.out.print("Too Low , try again  ");
    number2 = in.nextInt();
    if (number2==number) {
      System.out.println("You're right");}
    else if (number2>number) 
      Higher(number,number2);
    else 
      Lower(number,number2); }

  public static void Higher(int number,int number2) {
    Scanner in = new Scanner(System.in);
    System.out.print("Too high , try again  ");
     number2 = in.nextInt();
    if (number2==number) {
      System.out.println("You're right");}
    else if (number2>number) 
      Higher(number,number2);
    else 
      Lower(number,number2); }



  public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      Random random = new Random();
      int number = random.nextInt(100) + 1;
      int number2;
      System.out.print("Type a number:  ");
      number2 = in.nextInt();
    if (number2==number) {
      System.out.println("You're right");}
    else if (number2>number) 
      Higher(number,number2);
    else 
      Lower(number,number2);}
}

3 个答案:

答案 0 :(得分:0)

不知道它现在是否有用,但是,当我解决相同的解决方案时,想让它知道是否有人发现它有用:

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

/**
 * Created by RajU on 27-Jun-17.
 */
public class GuessNumber2 {

    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {

        message("I'm thinking of a number between 1 and 10 (including both).\n" +
                "Can you guess what it is?\n" +
                "Type a number: ");
        int userNumber = input.nextInt();

        tryAgain(userNumber, calculateRandom(10));

    }

    public static int calculateRandom(int n) {
        Random random = new Random();
        return random.nextInt(n) + 1;
    }

    public static void tryAgain(int userNumber, int generateRandom) {

        if (userNumber == generateRandom) {
            message("You're absolutely right!");
        } else {
            if (userNumber > generateRandom) {
                message("Think of a lesser number: ");
            } else {
                message("Think of a greater number: ");
            }
            userNumber = input.nextInt();
            tryAgain(userNumber, generateRandom);
        }

    }

    public static void message(String m) {
        System.out.print(m);
    }

}

答案 1 :(得分:0)

我刚刚完成了这个练习。看到一些不同的方法真的很有趣!以下是我的看法:

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

public class GuessGameLevelUp {
/*
 * A guessing game where you try to guess a random number between and including 1 and 100.
 * This version allows you to continue guessing until you get the right answer.
 * When you're off, a hint will let yet know whether your most recent guess was too high or low.
 */
public static void main (String [] args) {

    //Generates a random number for the game
    Random random = new Random();
    int answer = random.nextInt(100) +1;

    //Introduces the game and gives a prompt
    System.out.println("I'm thinking of a number between and including "
            + "1 and 100, can you guess which?");
    System.out.print("Take a guess: ");

    //Enables guess value input and parrots guess
    Scanner in = new Scanner(System.in);
    int guess;
    guess = in.nextInt();   
    System.out.println("Your guess is: "+guess);

    //Stacks a new class to determine the outcome of the game
    tooHighTooLow(answer, guess);

}

public static void tooHighTooLow(int a, int g) {

    //Triggers and parrots guess if correct
    if (a==g) {
    System.out.print("Correct! The number I was thinking of was: "+g);
    //Triggers "Too Low" prompt and uses recursive to offer another attempt
    } else if (a>g) {
        System.out.print("Too Low! Try again: ");
        Scanner in = new Scanner(System.in);
        g = in.nextInt();   
        System.out.println("Your guess is: "+g); //Parrots guess
        tooHighTooLow(a, g);
    //Triggers "Too High" prompt and uses recursive to offer another attempt
    }else
            System.out.print("Too high! Try again: ");
            Scanner in = new Scanner(System.in);
            g = in.nextInt();   
            System.out.println("Your guess is: "+g); //Parrots guess
            tooHighTooLow(a, g);

    }   
}

答案 2 :(得分:-1)

我也遇到了这个问题,但是你的代码帮助我找到了解决方案。这就是我想出的:

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

public class Ch5Ex7 {

  public static void compareNumbers(int userNumber,int randomNumber) {
    if(userNumber == randomNumber) {
        System.out.println("You got it!");
    } else if ( userNumber > randomNumber ) {
        System.out.println("Too high. Guess again: ");
        Scanner in = new Scanner(System.in);
        userNumber = in.nextInt();
        compareNumbers(userNumber, randomNumber);
    } else {
        System.out.print("Too low. Guess again: ");
        Scanner in = new Scanner(System.in);
        userNumber = in.nextInt();
        compareNumbers(userNumber,randomNumber); 
    }
}

  public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      Random random = new Random();
      int randomNumber = random.nextInt(100) + 1;
      int userNumber;
      System.out.print("Type a number: ");
      userNumber = in.nextInt();

      compareNumbers(userNumber, randomNumber);
  }
}

感谢您指出我正确的方向!