java - Dice program longest streak not resetting -
here output following code:
roll>>4 (1)higher or(2)lower?>> 1 current: 1 longest: 1 roll>>7 (1)higher or(2)lower?>> 2 streak ended. play again? 1-yes 2-no?>> 1 longest: 2 roll>>4 (1)higher or(2)lower?>>
when game ends , hit "1" play again, longest streak continues left off. how make resets every time play new game?
here code:
package diceman; import java.util.*; public class diceman2 { public static void main(string[] args) { scanner in = new scanner(system.in); random gen = new random(); int current = 1; int longest = 0; while (true) { // generates random number int random1 = gen.nextint((6) + 1); int random2 = gen.nextint((6) + 1); int random = (random1 + random2); system.out.println("roll>>" + (random)); // user plays game system.out.println("(1)higher or(2)lower?>>"); int input = in.nextint(); if (input == 1 ^ input > random) { system.out.println("current: " + current++); } else if (input == 2 ^ input < random) { system.out.println("current: " + current++); } else { system.out.println("your streak ended."); system.out.println("play again? 1-yes 2-no?>>"); int playagain = in.nextint(); if (playagain == 2) { break; } } // longest streak longest++; system.out.println("longest: " + longest); } } }
package diceman; import java.util.*; public class diceman2 { private static int doroll(random gen) { // random.nextint(6) produce number between 0..5. +1 1..6. int random1 = gen.nextint(6) + 1; int random2 = gen.nextint(6) + 1; int random = (random1 + random2); return random; } public static void main(string[] args) { scanner in = new scanner(system.in); random gen = new random(); int current = 0; int longest = 0; // generate initial number check against int previousvalue = doroll(gen); while (true) { system.out.println("roll>>" + previousvalue); // user plays game system.out.println("(1)higher or(2)lower?>>"); int input = in.nextint(); // generate next number compare previous int newvalue = doroll(gen); // if user selects higher , new value higher, continue. if (input == 1 && newvalue > previousvalue) { current++; system.out.println("current: " + current); // if user selects lower , new value lower, continue. } else if (input == 2 && newvalue < previousvalue) { current++; system.out.println("current: " + current); // guessed wrong (or maybe new value equal). } else { system.out.println("next value was: " + newvalue); system.out.println("your streak ended."); system.out.println("play again? 1-yes 2-no?>>"); int playagain = in.nextint(); // reset current streak zero, if playing again. if (playagain == 1) { current = 0; } else if (playagain == 2) { break; } // need handling if press 3,4,5,6 etc } // if current streak longer longest, update. if (current > longest) { longest = current; system.out.println("longest: " + longest); } // assign new value previous value, chekcing next time thro loop previousvalue = newvalue; } // close input reader free resources. in.close(); } }
Comments
Post a Comment