java程序中的一个方法首先跳过用户输入,然后在调用自身时请求用户输入
下面是我遇到麻烦的特定函数java程序中的一个方法首先跳过用户输入,然后在调用自身时请求用户输入,java,if-statement,recursion,input,java.util.scanner,Java,If Statement,Recursion,Input,Java.util.scanner,下面是我遇到麻烦的特定函数 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;
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语句。然后,它将调用自身,并在之后接受用户输入。因此,在代码的这一部分:
do{
System.out.println("\nRound " + roundnum + ":");
System.out.println("-------\n");
money = round(money);
cont = check(money);
}while(cont == true);
round()
和check()
都是使用类中声明为字段的Scanner
对象的函数round()
使用nextLine()
,而check()
使用nextLine()
发生的情况是,当round()
解析猜测时,nextInt()
不会解析数字输入后的任何尾随空格(如按Enter键的换行符)。因此,当您调用check()
时,仍然有一行代码供它读入,因此当您点击这一行时
scont = in.nextLine();
它改为在那一行中读取,因为它显然不等于“yes”
或“no”
,所以它转到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));
我最近在使用扫描器时遇到了类似的问题,正如丹尼斯所解释的,我很确定这与以前在执行中使用扫描器有关。这里有一个我发布的关于它的问题的链接
看看这里给出的一些答案。有一个特别的例子直观地解释了扫描仪是如何工作的,这是我从中学到的很多东西 你到底有什么问题?这个方法应该是递归方法吗?在这个语句money=round(money)之后,钱的价值是多少;?还要在.nextLine()中的这一行scont=in之后打印scont的值;。佩斯基莱特问了什么。你是说它忽略了用户是否说是继续?您自己的逻辑指定,如果用户没有说“是”或“否”,它将完全按照您所描述的内容执行。跳转到else并调用自身。@Radiodef我认为OP看到的是,在用户有机会输入yes/no之前,它通过if/else块并调用自身。我还不会说谢谢,但您的回答似乎非常符合逻辑。我会设法解决的。我只是觉得有趣,我的老师从来没有提到过这样一个可能发生的问题。这也是我第一次遇到这样的问题。顺便说一句,Cem说递归调用不是解决问题的方法,这是正确的,但你可以稍后再担心。哇,非常感谢!在主函数中的
money=round(money)
之后,我简单地将放在.nextLine()中代码>。这是一次完美的修复。我将不得不向老师提出这个复杂问题。我在另一篇帖子中提到了这段代码,询问同一个函数中的不同问题,我想有人试图提到你刚才向我解释的同一个问题。然而,它们没有意义。你的解释很清楚。再次感谢=如果不考虑该函数中的递归调用,我想这不是正确的方法。然而,直到现在,我还没有找到使用递归函数的方法。我以前多次使用do…while循环来检查有效数据和输入。我意识到我可以让它自己调用,以防在制作这个程序时出错。do…while
是正确的方法。简短的解释是,如果进行递归调用,每次递归调用都需要额外的内存,因此反复提供不正确的输入可能会导致程序内存不足。使用do…而本身并不需要每次迭代都需要更多的内存,因此用户可以随心所欲地提供不正确的输入;你的程序不会耗尽内存。