用Java打印返回值

时间:2018-07-05 10:45:03

标签: java return-value

我目前正在用Java做一个高低猜猜游戏。我相信我有两个问题阻碍了游戏的正常运行。

首先,我在 playGame 方法中有一个while语句,该语句现在不会发生变化,而且我很难理解如何做出正确的猜测才能使其发生变化。据我了解,我无法通过GiveResponse方法更改 boolean bool = true; 的值,而必须通过同一方法 playGame 来完成此操作?是否有比使用while语句更好的方法?

第二,当做出正确的猜测并且 playGame 方法将参数 guessCount 的值返回给主方法时,应将其打印出来。我对这款游戏的说明说,返还后应该用main方法打印出来。

import java.io.Reader;
import java.util.Scanner;
public class HiLo2 {
 public static void main(String[] args) {

  System.out.println("Decide a level of difficulty\r"
    + "1. 1-10 \r"
    + "2. 1-100 \r"
    + "3. 1-1000");

  Scanner reader = new Scanner(System.in);
  int n = reader.nextInt();

   if (n==1) {
             playGame(10);
   }
         else if (n==2) {
             playGame(100);
         }
         else if (n==3) {
          playGame(1000);
         }
         else {
          System.out.println("This level of difficulty doesn't exist. Please try one between 1 and 3.");
         }
  }

 public static int playGame(int maxNumber) {
  int answer = (int)(Math.random() * maxNumber) +1;
  int guessCount = 0;
  boolean bool = true;
  Scanner reader = new Scanner(System.in);
  System.out.println("Guess a number between 1 and " + maxNumber);

  while (bool) { 
  int guess = reader.nextInt();
  guessCount = guessCount + 1;
  giveResponse(answer, guess);
  }

  return guessCount;
 }

 public static void giveResponse(int answer, int guess) {  
  if (guess == answer) {
   System.out.println("Your guess is correct!\r");
  }
  else if (guess > answer) {
   System.out.println("Your guess is too high, guess again:\r");
  }
  else if (guess < answer) {
   System.out.println("Your guess is too low, guess again:\r");
  }
 }
}

我希望这些问题足够明确和具体。任何朝着正确方向的指针都值得赞赏。

4 个答案:

答案 0 :(得分:3)

您可以使用GiveResponse方法返回布尔值

static boolean giveResponse(...) 

并将return false;放在if (guess == answer){的情况下,并将return true放在方法的末尾。代替使用giveResponse作为void方法,而使用bool = giveResponse(answer,guess)

对于第二个问题,在if语句之前的main方法中添加一个int numberOfGuesses,并使用numberOfGuesses = playGame(...)调用playGame,然后调用System.out.println(“您猜到了” + numberOfGuesses +“次。”);在if语句之后。

答案 1 :(得分:0)

我认为这就是您想要做的。

public class HiLo2 {
 public static void main(String[] args) {

  System.out.println("Decide a level of difficulty\r"
    + "1. 1-10 \r"
    + "2. 1-100 \r"
    + "3. 1-1000");

  Scanner reader = new Scanner(System.in);
  int n = reader.nextInt();

   if (n==1) {
             System.out.println("Guess count: "+playGame(10));
   }
         else if (n==2) {
             System.out.println("Guess count: "+playGame(100));
         }
         else if (n==3) {
          System.out.println("Guess count: "+playGame(1000));
         }
         else {
          System.out.println("This level of difficulty doesn't exist. Please try one between 1 and 3.");
         }
  }

 public static int playGame(int maxNumber) {
  int answer = (int)(Math.random() * maxNumber) +1;
  int guessCount = 0;
  boolean bool = false;
  Scanner reader = new Scanner(System.in);
  System.out.println("Guess a number between 1 and " + maxNumber);

  while (! bool) { 
  int guess = reader.nextInt();
  guessCount = guessCount + 1;
  bool = giveResponse(answer, guess);
  }

  return guessCount;
 }

 public static boolean giveResponse(int answer, int guess) {  
  if (guess == answer) {
   System.out.println("Your guess is correct!\r");
   return true;
  }
  else if (guess > answer) {
   System.out.println("Your guess is too high, guess again:\r");
   return false;
  }
  else if (guess < answer) {
   System.out.println("Your guess is too low, guess again:\r");
   return false;
  }
 }
}

答案 2 :(得分:0)

这是您的完整解决方案

import java.io.Reader;
import java.util.Scanner;

public class HereSOl {
    public static void main(String[] args) {
        System.out.println("Decide a level of difficulty\r" + "1. 1-10 \r" + "2. 1-100 \r" + "3. 1-1000");
        Scanner reader = new Scanner(System.in);
        int n = reader.nextInt();

        if (n == 1) {
            System.out.println("Guesstcount : " + playGame(10));
        } else if (n == 2) {
            System.out.println("Guesstcount : " + playGame(100));           
        } else if (n == 3) {
            System.out.println("Guesstcount : " + playGame(1000));          
        } else {
            System.out.println("This level of difficulty doesn't exist. Please try one between 1 and 3.");
        }
    }

    public static int playGame(int maxNumber) {
        int answer = (int) (Math.random() * maxNumber) + 1;
        int guessCount = 0;
        boolean bool = true;
        Scanner reader = new Scanner(System.in);
        System.out.println("Guess a number between 1 and " + maxNumber);

        while (bool) {
            int guess = reader.nextInt();
            guessCount = guessCount + 1;
            boolean ans =giveResponse(answer, guess); 
            if (ans == true) {              
                break;
            }
        }

        return guessCount;
    }

    public static boolean giveResponse(int answer, int guess) {
        if (guess == answer) {
            System.out.println("Your guess is correct!\r");
            return true;
        } else if (guess > answer) {
            System.out.println("Your guess is too high, guess again:\r");
            return false;
        } else if (guess < answer) {
            System.out.println("Your guess is too low, guess again:\r");
            return false;
        }
        return false;
    }
}

答案 3 :(得分:0)

我认为您应该使用枚举,这是我的解决方案。

import java.util.Scanner;

public class HiLo2 {
  public static void main(String[] args) {
        System.out.println("Decide a level of difficulty\r" + "1. 1-10 \r" + "2. 1-100 \r" + "3. 1-1000");

    Scanner reader = new Scanner(System.in);
    int n = reader.nextInt();

    if (n == 1) {
      playGame(10);
    } else if (n == 2) {
      playGame(100);
    } else if (n == 3) {
      playGame(1000);
    } else {
      System.out.println("This level of difficulty doesn't exist. Please try one between 1 and 3.");
    }
  }
  public static int playGame(int maxNumber) { 
    int answer = (int) (Math.random() * maxNumber) + 1;
    int guessCount = 0;
    boolean bool = true;
    Scanner reader = new Scanner(System.in);
    System.out.println("Guess a number between 1 and " + maxNumber);
    while (bool) {
      int guess = reader.nextInt();
      guessCount = guessCount + 1;
      Answer ans = giveResponse(answer, guess);
      System.out.println(ans.getMessage());
      if (ans == Answer.CORRECT) {
        bool = false;
        System.out.println("You took " + guessCount + " guess to get right number.");
      }
    }
    return guessCount;
  }

  public static Answer giveResponse(int answer, int guess) {
    if (guess == answer) {
      return Answer.CORRECT;
    } else if (guess > answer) {
      return Answer.HIGH;
    } else {
      return Answer.LOW;
    }
  }

  private enum Answer {
    CORRECT("Your guess is correct!\r"), HIGH("Your guess is too high, guess again:\r"), LOW("Your guess is too low, guess again:\r");

    private String message;

    public String getMessage() {
      return this.message;
    }
    Answer(String message) {
      this.message = message;
    }
  }
}