我正在嘗試撰寫一個回圈來運行一個方法,并至少回傳一次結果。getResults() 方法回傳一個包含“再次拋出”、“獲勝”或“失敗”的字串,如果它回傳“再次拋出”,它將繼續回圈,否則中斷。
每當我收到“再次拋出”時,此代碼當前都會將我發送到無限回圈中。我注意到回圈只運行一次 getResults() 來獲取結果。但是,我不確定如何讓該回圈再次運行該方法,然后在每次“再次拋出”后檢查字串。
public String playCraps() {
String results;
do {
getResults();
results = getResults();
System.out.println(results "\n*************");
} while (results.contains("Throw again"));
return "\n*********";
}
編輯 - 包含的方法。
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;
}
public 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;
}
uj5u.com熱心網友回復:
您應該洗掉第一個 getResults() 呼叫
uj5u.com熱心網友回復:
我相信錯誤可能出在 getReults() 中,您可能需要使用:int rand = (int)(Math.random() * range) min; if (rand == 1){ / return "再次拋出"; }else if (rand == 2){ // 回傳 "win"; }else{ 回傳“失敗”;塊參考
}
uj5u.com熱心網友回復:
String result = "Result is " decideOutcome(addUpScore());
這是錯誤的。當你沒有輸入時,你應該在他加起來的時候放一個變數嗎?我希望你會輸入
String result = "Result is " decideOutcome(addUpScore(dice1.getFaceValue(), dice2.getFaceValue()));
你在加什么?addUpScore 如何知道骰子 1 和骰子 2 有哪些面孔?
假設getFaceValue()回傳一個int你可以簡單地:
decideOutcome(dice1.getFaceValue() dice2.getFaceValue())
我想這些面孔正在滾動,getFaceValue()所以這是一個有效的解決方案:
public String getResults() {
int dice1Value = dice1.getFaceValue()
int dice2Value = dice2.getFaceValue()
int total = dice1Value dice2Value
String die1 = "Dice 1 is " dice1Value.toString();
String die2 = "Dice 2 is " dice2Value.toString();
String total = "Total is: " total.toString();
String result = "Result is " decideOutcome(total);
}
uj5u.com熱心網友回復:
看起來你的回圈邏輯很好!getResult() 應該每次都運行,但我相信它總是回傳“再次拋出” getResult() 一定有問題;
編輯
Id 建議嘗試使用此方法進行除錯:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/360116.html
