-1

这是我遇到问题的特定功能。

  static boolean check(double money)
  {
    String scont, yes = "yes", no = "no";
    boolean bcont;
    if (money == 0) {
      System.out.println("You are broke and can no longer play.");
      bcont = false;
      return bcont;
    }
    System.out.println("You have " + form.format(money) + " left.");
    System.out.println("Would you like to continue playing? (Yes or no?)");
    scont = in.nextLine();
    if (scont.equalsIgnoreCase(yes)) {
      bcont = true;
      return bcont;
    }
    else if (scont.equalsIgnoreCase(no)) {
      bcont = false;
      return bcont;
    }
    else {
      System.out.println("Invalid answer.");
      bcont = check(money);
      return bcont;
    }
  }

这是整个程序。

import java.util.Random;
import java.util.Scanner;
import java.text.*;

public class JS4B
{
  static Scanner in = new Scanner(System.in);
  static DecimalFormat form = new DecimalFormat("$#.00");

  public static void main(String [] args)
  {
    int roundnum = 1;
    double beginmoney, money;
    boolean cont;

    intro();
    beginmoney = hmmoney();
    money = beginmoney;

    do{
      System.out.println("\nRound " + roundnum + ":");
      System.out.println("-------\n");
      money = round(money);
      cont = check(money);
    }while(cont == true);

    if (money > beginmoney) {
      System.out.println("Congratulations! You have completed " + 
    rounds(roundnum) + " and ended up with more money than you started!");
    }
    else if (money == beginmoney) {
      System.out.println("You broke even! You have completed " + 
    rounds(roundnum) + ".");
    }
    else if (money != 0) {
      System.out.println("You have less money than you started with, but " + 
    "at least you didn't lose it all. You completed " + rounds(roundnum) + ".");
    }
    else {
      System.out.println("You have completed " + rounds(roundnum) + ".");
    }

    System.out.println("You started with " + form.format(beginmoney) + 
    " and ended with " + form.format(money));
  }

  static void intro()
  {
    System.out.println("     Guess the Number!     ");
    System.out.println("===========================");
    System.out.println("In this game, a random \n" + 
                       "number between 1 and 100 \n" +
                       "will be chosen. You have to \n" +
                       "guess what it is in 4 tries.\n");
  }

  static double hmmoney()
  {
    double money;
    System.out.print("How much money would you like to start with?\nI would like" +
                     " to start with... ");
    money = in.nextDouble();
    System.out.println("");

    return money;
  }

  static double round(double money)
  {
    int guess, actual, guessnum = 1;
    double bet = bet(money);
    boolean correct;

    actual = genint();

    for (;;)
    {
      guess = guess();
      correct = check(guess, actual);
      if (correct == false) {
        guessnum++;
        if (guessnum > 4) {
          System.out.println("You have made the max number of guesses " +
                             "and have lost this round.");
          System.out.println("The correct number was... " + actual + "\n");
          money -= bet;
          break;
        }
        else {
          hint(guess, actual);
        }
      }
      else {
        money += bet;
        break;
      }
    }

    return money;
  }

  static double bet(double money)
  {
    double bet;

    System.out.print("How much money would you like to bet? ");
    bet = in.nextDouble();
    System.out.println("");

    if (bet > money) {
      System.out.println("You can't bet more than you have!");
      bet = bet(money);
    }

    return bet;
  }

  static int genint()
  {
    Random gen = new Random();
    int actual;
    actual = gen.nextInt(100) + 1;

    return actual;
  }

  static int guess()
  {
    int guess;
    System.out.print("I think that the number is... ");
    guess = in.nextInt();
    System.out.println("");

    return guess;
  }

  static boolean check(int guess, int actual)
  {
    if (guess != actual) {
      System.out.println("That is incorrect.");
      return false;
    }
    else {
      System.out.println("Congratulations! You guessed the correct number!");
      return true;
    }
  }

  static boolean check(double money)
  {
    String scont, yes = "yes", no = "no";
    boolean bcont;
    if (money == 0) {
      System.out.println("You are broke and can no longer play.");
      bcont = false;
      return bcont;
    }
    System.out.println("You have " + form.format(money) + " left.");
    System.out.println("Would you like to continue playing? (Yes or no?)");
    scont = System.in.readline();
    if (scont.equalsIgnoreCase(yes)) {
      bcont = true;
      return bcont;
    }
    else if (scont.equalsIgnoreCase(no)) {
      bcont = false;
      return bcont;
    }
    else {
      System.out.println("Invalid answer.");
      bcont = check(money);
      return bcont;
    }
  }

  static void hint(int guess, int actual)
  {
    if (guess > actual) {
      System.out.println("Guess lower!");
    }
    else {
      System.out.println("Guess higher!");
    }
  }

  static String rounds(int roundnum)
  {
    String result;
    if (roundnum == 1) {
      result = "1 Round";
      return result;
    }
    else {
      result = Integer.toString(roundnum) + " Rounds";
      return result;
    }
  }
}

顺便说一句,这是针对我的 AP Java 类的,这就是程序如此的原因。在该函数中,它将绕过用户输入并直接转到其后面的 else 语句。然后,它会调用自己并在此之后接受用户输入。

4

3 回答 3

0

我最近在使用 Scanner 时遇到了类似的问题,正如 Dennis 解释的那样,我很确定这与之前在执行中使用 Scanner 有关。这是我发布的问题的链接。

Try/Catch inside While 在静态 main 方法中

看看那里给出的一些答案。有一个特别直观地解释了扫描仪是如何工作的,我从中学到了很多东西。

于 2013-10-21T02:25:19.833 回答
0

因此,在这部分代码中:

do{
  System.out.println("\nRound " + roundnum + ":");
  System.out.println("-------\n");
  money = round(money);
  cont = check(money);
}while(cont == true);

round()并且check()都是使用Scanner在您的类中声明为字段的对象的函数。round()使用nextInt(),同时check()使用nextLine()

发生的事情是,在round()解析猜测时,nextInt()不会解析数字输入后的任何尾随空格(如按 的换行符Enter)。所以,当你调用 时check(),它还有一行可以读入,所以当你点击这条线时

scont = in.nextLine();

它改为读取该行,并且由于它显然不等于"yes"or "no",因此它转到 else 块,然后在该块中调用自身。

因为这上课用的,所以我实际上不会说如何解决这个问题。

于 2013-10-21T02:13:15.553 回答
0

你不能在else部分:

  System.out.println("Invalid answer.");
  bcont = check(money);
  return bcont;

...因为您不必要地进行了递归调用,我确信这不是您想要的。

因此,请改为执行以下操作:

scont = System.in.readline();

do {
    scont = System.in.readline();
       if (scont.equalsIgnoreCase(yes)) {
         bcont = true;
         return bcont;

       } else if (scont.equalsIgnoreCase(no)) {
        bcont = false;
        return bcont;

      } else {

       System.out.println("Invalid answer.  Please try again.  Would you like to continue playing? (Yes or no?)");
       scont = System.in.readline();

} while (!scont.equalsIgnoreCase(yes) && !scont.equalsIgnoreCase(yes));
于 2013-10-21T02:18:59.963 回答