这是我遇到问题的特定功能。
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 语句。然后,它会调用自己并在此之后接受用户输入。