创建一个 while 循环,它从一个方法中随机获取 return

Creating a while loop which takes a random return from a method

我正在尝试编写一个循环,该循环将 运行 一个方法,并且 return 至少一次结果。 getResults() 方法 return 是一个包含“Throw again”、“win”或“lose”的字符串,如果它 return 是“throw again”,它会继续循环,否则会中断。

每当我收到“Throw again”时,此代码当前会使我进入无限循环。我注意到循环只有 运行s getResults() 一次来获取结果。但是,我不确定如何再次循环 运行 方法然后在每个 'Throw again'.

之后检查字符串
public String playCraps() {
        String results;
        do {
            getResults();
            results = getResults();
            System.out.println(results + "\n*************");
        } while (results.contains("Throw again"));
        return "\n*********";
    }

编辑 getResults() 方法。

public String getResults() {
        String die1 = "Dice 1 is " + dice1.getFaceValue();
        String die2 = "Dice 2 is " + dice2.getFaceValue();
        String total = "Total is: " + addUpScore();
        String result = "Result is " + decideOutcome(addUpScore());
        String message = die1 +"\n" +die2+"\n"+total+"\n"+result;
        return message;
    }

编辑 2 - 1st returns 每次按 运行 时我想要的输出(getResults() 的输出是随机的)。第二个表明无论我通过循环或指令调用多少次,getResults() 都不会改变。

public String playCraps() {
        return getResults();
    }
public String playCraps() {
        System.out.println(getResults());
        getResults();
        System.out.println(getResults());
        getResults();
        String message;
        message = getResults();
        return message;
    }

看来你的循环逻辑没问题! getResult() 应该每次都得到 运行,但我相信它总是返回“再次抛出”getResult();

一定有问题

编辑

我建议尝试这个来调试:

public String getResults() {
    String die1 = "Dice 1 is " + dice1.getFaceValue();
    System.out.println(die1);
    String die2 = "Dice 2 is " + dice2.getFaceValue();
    System.out.println(die2);
    String total = "Total is: " + addUpScore();
    System.out.println(total);
    String result = "Result is " + decideOutcome(addUpScore());
    System.out.println(result);
    String message = die1 +"\n" +die2+"\n"+total+"\n"+result;
    System.out.println(message);
    return message;
}

编辑

这是我开始工作的一些代码。

public String playCraps() 
{
  String results;
  System.out.println("");
  int counter = 0;
  do 
  {
    counter++;
    results = getResults(counter);
    System.out.println(results + "\n*************");
  } while (results.contains("Throw again"));
  return "\n*********";
}

public String getResults(int counter)
{
    System.out.println("in getResults");

  if(counter > 3)
  {
    return "win";
  }

  return "Throw again";
}

结果:

in getResults
Throw again
*************
in getResults
Throw again
*************
in getResults
Throw again
*************
in getResults
win
*************

Process finished with exit code 0

编辑

现在看来问题出在 addUpScore() 上。

我认为错误可能出在 getReults(),您可能需要使用:

int rand = (int)(Math.random() * range) + min;
if(rand == 1) {
    return "throw again";
} else if(rand == 2){
    return "win";
} else { 
    return "lose"; 
}
String result = "Result is " + decideOutcome(addUpScore());

这是错误的。当你没有输入时,你应该在他加起来的内容中加入一个变量?我希望你会输入

 String result = "Result is " + decideOutcome(addUpScore(dice1.getFaceValue(), dice2.getFaceValue()));

这是一个完整的工作示例:

import java.util.Random;

public class MyClass {
    public static void main(String args[]) {
        System.out.println(playCraps());
    }
    
    public static String playCraps() {
        String results;
        do {
            results = getResults();
            System.out.println(results + "\n*************");
        } while (results.contains("Throw again"));
        return "\n*********";
    }
    
    public static String getResults(){
        Random rand = new Random();
        int max = 6;
        int min = 1;
        int dice1Value = rand.nextInt((max+1) - min) + min;
        int dice2Value = rand.nextInt((max+1) - min) + min;
        int score = dice1Value + dice2Value;

        String die1 = "Dice 1 is: " + dice1Value;        
        String die2 = "Dice 2 is: " + dice2Value;
        String total = "Total is: " + score;
        return "Result is:"+ "\n" + die1 +"\n" +die2+"\n"+total+"\n" + decideOutcome(score);
    }
    
    public static String decideOutcome(int score){
        String message = new String();
        if(score == 7 || score == 11) {
            message = "You win";
        }
        else if(score == 2 || score == 3 || score == 12) {
            message = "You lose";
        }
        else {
            message = "Throw again";
        }
        return message;
    }
    
}

第一个输出:

Result is:
Dice 1 is: 5
Dice 2 is: 5
Total is: 10
Throw again
*************
Result is:
Dice 1 is: 4
Dice 2 is: 1
Total is: 5
Throw again
*************
Result is:
Dice 1 is: 3
Dice 2 is: 6
Total is: 9
Throw again
*************
Result is:
Dice 1 is: 5
Dice 2 is: 6
Total is: 11
You win
*************

*********

第二个输出:

Result is:
Dice 1 is: 5
Dice 2 is: 5
Total is: 10
Throw again
*************
Result is:
Dice 1 is: 4
Dice 2 is: 5
Total is: 9
Throw again
*************
Result is:
Dice 1 is: 4
Dice 2 is: 5
Total is: 9
Throw again
*************
Result is:
Dice 1 is: 3
Dice 2 is: 6
Total is: 9
Throw again
*************
Result is:
Dice 1 is: 4
Dice 2 is: 4
Total is: 8
Throw again
*************
Result is:
Dice 1 is: 1
Dice 2 is: 2
Total is: 3
You lose
*************

*********