java - Displaying Results After a Loop -
my class learned loops , random number generators, wanted try creating game guessed sum of 2 dice. @ end of each game, want show of user's guesses , line displays value was. issue displaying values user inputted @ end. code far:
import java.util.random; import java.util.scanner; public class dice2 { public static void main (string[] args) { scanner kb = new scanner(system.in); random rn = new random(); int roll1= rn.nextint(6)+1; int roll2= rn.nextint(6)+1; int sum = roll1 +roll2; boolean retry = false; system.out.print("guess number: "); int guess = kb.nextint(); kb.nextline(); if (guess!=sum) { retry= true; while (retry) { system.out.print("wrong, enter again: "); int newguess = kb.nextint(); retry = true; if(newguess==sum) { retry = false; system.out.printf("you guessed: %4d%4d\n", guess,newguess); system.out.println("the number was: " + sum); } else continue; } } else system.out.println("the number " + sum); } }
the problem i'm getting when trying show of numbers user guessed. want display guesses user had, why used while loop. i'm not sure how go displaying inputs user guessed. example if correct answer 8 , user guessed 2,3,4,5,6,8 want able display following:
you guessed: 2,3,4,5,6,8 number :8
i understand after each iteration of while loop overwriting newguess variable, not sure in order display numbers user guessed. or tips appreciated.
if want string user's guesses, use stringbuilder
.
stringbuilder builder = new stringbuilder(); builder.append(5); builder.append(","); builder.append(4); builder.append(","); builder.append(9); system.out.println(builder.tostring());
create 1 before while loop, append each time through loop.
Comments
Post a Comment