I'm trying to write a loop that will run a method, and return the results at least once. the getResults() method returns a string containing either "Throw again" "win" or "lose", and if it returns "throw again" it continues to loop, breaking otherwise.
This code currently sends me into an infinite loop whenever I get the "Throw again". I notice that the loop only runs getResults() once to take the results. However, I'm unsure how to have that loop run the method again then check for the string after each 'Throw again'.
public String playCraps() {
String results;
do {
getResults();
results = getResults();
System.out.println(results "\n*************");
} while (results.contains("Throw again"));
return "\n*********";
}
EDIT -Included methods.
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;
}
CodePudding user response:
You should remove the first getResults() call
CodePudding user response:
I believe the fault might be in getReults(), you might want to use: int rand = (int)(Math.random() * range) min; if (rand == 1){ / return "throw again"; }else if (rand == 2){ // return "win"; }else{ return "lose"; } Blockquote
}
CodePudding user response:
String result = "Result is " decideOutcome(addUpScore());
this is wrong. you should put a variable in what is he adding up when you have no input? I would expect that you would input
String result = "Result is " decideOutcome(addUpScore(dice1.getFaceValue(), dice2.getFaceValue()));
What are you adding up? How does addUpScore know faces dice1 and dice2 have?
Assuming getFaceValue()
returns an int
you can simply:
decideOutcome(dice1.getFaceValue() dice2.getFaceValue())
I imagine the faces are getting rolled with getFaceValue()
so here is a working solution:
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);
}
CodePudding user response:
It looks like your loop logic is just fine! getResult() should be getting run each time through, but I believe it is always returning "Throw again" there must be an issue with getResult();
Edit
Id recommend trying this to debug:
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;
}
Edit
Here some code I got to work.
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";
}
Results:
in getResults
Throw again
*************
in getResults
Throw again
*************
in getResults
Throw again
*************
in getResults
win
*************
Process finished with exit code 0
Edit
It now appears that addUpScore() is the problem.