java - Random number keeps giving the number 1 -


since added while loop little rock,paper , scissors game keeps giving me number 1.

package steenpapierschaar1;  import java.util.scanner;  /**  *  * @author t  */ public class steenpapierschaar1 {      /**      * @param args command line arguments      */     public static void main(string[] args) {          int rock = 1 ;   // waarde van rock         int paper = 2;  // waarde van paper         int scissor = 3;// waarde van scissor         int ai = 1 + (int) (math.random() *3);// maakt int ai een random getal nog niet af moet een range in komen         scanner keyboard = new scanner (system.in);// naam scanner input         boolean playing = true;         string answer, answer2 = null;           while(playing = true){             system.out.println("choose 1 rock, 2 paper , 3 scissor"); // string met keuze voor user             int userchoice = keyboard.nextint();// waarde van userchoice              if (ai == 1 && userchoice == 2 || ai == 2 && userchoice == 3 || ai == 3 && userchoice == 1) { // als de speler elke keer 1x hoger heeft wint hij de ronde                 system.out.println("you win");// outprint met dat je gewonnen hebt en wat de computer doet                 if (ai == 1)                     system.out.println("the computer did rock");                 if (ai == 2)                     system.out.println("the computer did paper");                 if (ai == 3)                     system.out.println("the computer did scissors");               }              else if (ai == 1 && userchoice == 1 || ai == 2 && userchoice == 2 || ai == 3 && userchoice == 3) {                 system.out.println("draw");                 if (ai == 1)                     system.out.println("the computer did rock");                 if (ai == 2)                     system.out.println("the computer did paper");                 if (ai == 3)                     system.out.println("the computer did scissors");             }              else if (ai == 1 && userchoice == 3 || ai == 2 && userchoice == 1 || ai == 3 && userchoice == 2){                 system.out.println("you lose");                 if (ai == 1)                     system.out.println("the computer did rock");                 if (ai == 2)                     system.out.println("the computer did paper");                 if (ai == 3)                     system.out.println("the computer did scissors");             }                        }             } } 

there few problems in code. have few unused variables

int rock = 1 ;  // waarde van rock int paper = 2;  // waarde van paper int scissor = 3;// waarde van scissor 

which should

final int rock = 1; // waarde van rock final int paper = 2; // waarde van paper final int scissors = 3;// waarde van scissor 

and used

if (ai == rock) ... 

other problem while (playing == true) notice = assignment operator == comparing operator. in loop assigning true playing evaluated true.

to avoid such problems mistake write = instead of == use

while (playing){  } 

btw should ask after each play if user want continue/quit game. can checking values -1 can represent "quit".


another problem never reassign value of ai inside loop value can't change , same @ start of program. maybe move

int ai = 1 + (int) (math.random() * 3); 

at start of while loop let ai new value.

btw2 instead of math.random()*3 kind of cryptic can use random class , nextint(n) method random integer in range [0; n)

  • 0 (including)
  • n (excluding).

which can interpreted [0; n-1]. code

random r = new random(); while(..){     int ai = r.nextint(3) + 1; // +1 because want move range [0; 2] [1; 3]     ... } 

it looks invoking

if (ai == 1)     system.out.println("the computer did rock"); if (ai == 2)     system.out.println("the computer did paper"); if (ai == 3)     system.out.println("the computer did scissors"); 

in each of win/draw/lose conditions. in case can move out of each of these blocks

if (win conditions){     print win }else if (draw condition){     print draw }else if (lose condition){     print lose } <move code drawing computer hand here> 

draw condition looks overly complex. looks draw achieved when ai , userchoice equal simple

if (ai == userchoice) 

could replace

if (ai == 1 && userchoice == 1 || ai == 2 && userchoice == 2 || ai == 3 && userchoice == 3) 

you can simplify other conditions using modulo % operator.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -