java - Random Coin Flipper output number of times run -


i attempting write program in java flips imaginary coin , outputs flips , when side has been flipped 3 times, stops , tells number of times flipped. program doesn't seem working

my code below:

 import java.util.*; public class flipperthree {     public static void main(string[] args) {          boolean fin = false;         while(!fin){             system.out.println("welcome flipper!");             int h = 0;             int hcount = 0;             int tcount = 0;             int ocount = 0;             string random;             string[] ht;             boolean done = false;             while(!done){                 for(hcount<3||tcount<3){ht = new string[] {"heads","tails"};                 random r =new random();                 random = ht[r.nextint(ht.length)];                 system.out.println(random);                 }                 if (hcount!=3||tcount!=3){                     if(random == ht[h]){                         hcount++;                         ocount++;                         tcount = 0;                     }else{                         hcount = 0;                         tcount++;                         ocount++;                     }                 }else{                     system.out.println("bingo!that took " + ocount+" flips 3 in row!");                     done = true;                 }             }         }fin = true;     } } 

  1. in general program more complicated necessary. need single loop

  2. you using == compare strings. instead of random == ht[h] should random.equals(ht[h])

  3. instead of looping while either heads or tail counts less 3, want keep looping while both less three. instead of hcount!=3 || tcount!=3 it's hcount!=3 && tcount!=3

.

 public class flipperthree {       public static void main(string[] args) {           system.out.println("welcome flipper!");          int h = 0;          int hcount = 0;          int tcount = 0;          int ocount = 0;          string[] ht = new string[] {"heads","tails"};          random r =new random();           while (hcount < 3 && tcount < 3) {              string random = ht[r.nextint(ht.length)];              system.out.println(random);              if(random.equals(ht[h])){                  hcount++;                  ocount++;                  tcount = 0;              }              else{                  hcount = 0;                  tcount++;                  ocount++;              }          }          system.out.println("bingo!that took " + ocount+" flips 3 in row!");      }  }  $ java flipperthree welcome flipper! tails heads heads tails heads heads tails tails tails bingo!that took 9 flips 3 in row! 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -