mercredi 2 août 2023

How to save the last RANDOM generated number in a loop

**I am trying to make the user go back to the position he was on if he gets higher than 100. So if he was on 97 and rolls a 5 on Dice1 and becomes 102 I want the variable position1 to go back to 97 I am still new so it might just be a stupid logic problem also my variable names are not industry standard so im still working on this **

     Scanner Scan = new Scanner(System.in);
     Random random = new Random();
     int Position1 = 0;
     //int position2 = 0;
    
     
     System.out.println("Type Start");
     String Input = Scan.next();

  
      if (Input.equals("start")){
         
         while(Position1!=100 ) {
            
             int Dice1 = random.nextInt(1,7);
            
 
             if (Dice1 == 6) {
                 Position1= Position1+Dice1;
                 System.out.println(Position1);
                 System.out.println("player 1 turn ");
                 Input = Scan.next();

             }
             else if (Dice1 == 5) {
                 Position1= Position1+Dice1;
                 System.out.println(Position1); 
                 System.out.println("player l turn");
                 Input = Scan.next();

             }
             else if (Dice1 == 4) {
                 Position1= Position1+Dice1;
                 System.out.println(Position1);
                 System.out.println("player 1 turn ");
                 Input = Scan.next
             }
             else if (Dice1 == 5) {
                 Position1= Position1+Dice1;
                 System.out.println(Position1); 
                 System.out.println("player l turn");
                 Input = Scan.next();

             }
             else if (Dice1 == 4) {
                 Position1= Position1+Dice1;
                 System.out.println(Position1);
                 System.out.println("player 1 turn ");
                 Input = Scan.next();

             }
             else if (Dice1 == 3) {
                 Position1= Position1+Dice1;
                 System.out.println(Position1);
                 System.out.println("Player 1 turn");
                 Input = Scan.next();

             }
             else if (Dice1 == 2) {
                 Position1= Position1+Dice1;
                 System.out.println(Position1);
                 System.out.println("player 1 turn");
                 Input = Scan.next();

             }
             else if (Dice1 == 1) {
                 Position1= Position1+Dice1;
                 System.out.println(Position1);
                 System.out.println("player 1 turn ");
                 Input = Scan.next();
             }
             if(Position1>100) {
                Position1 = Position1-Dice1;
             }
             else if(Position1==100) {
                 System.out.println("U Win"); 
                 
             }
                    
        }
        
     
    }

}

}




Aucun commentaire:

Enregistrer un commentaire