mercredi 26 août 2015

Why doesnt this while loop stop after roll1 and roll2?

I've created a simple dice roll program. But the while loop will not stop when roll1 and roll2 are equal. And the total is not adding up. The programs runs infinite times, I have to stop it. Please help.

Output:

Roll #1: 1
Roll #2: 2
Total is : 3
Roll #1: 4
Roll #2: 1
Total is : 3
Roll #1: 4
Roll #2: 4
Total is : 3
Roll #1: 2
Roll #2: 5
Total is : 3
Roll #1: 4
Roll #2: 4
Total is : 3
Roll #1: 0
Roll #2: 2
Total is : 3
Roll #1: 4
Roll #2: 3
Total is : 3

Source Code:

import java.util.Scanner;
import java.util.Random;

public class App1
{
    public static void main( String[] args )
    {
        Random r = new Random();

        int roll1 = 1+ r.nextInt(6);
        int roll2 = 1+ r.nextInt(6);
        int total = roll1 + roll2;

        System.out.println("Heres comes the dice!");
        System.out.println();


        while ( roll1 != roll2 )
        {
            System.out.println("Roll #1: "  + roll1);
            System.out.println("Roll #2: "  + roll2);
            System.out.println("Total is : " + total );

        }
        System.out.println("Roll #1: "  + roll1);
        System.out.println("Roll #2: "  + roll2);
        System.out.println("Total is : " + total );


    }
 }




Aucun commentaire:

Enregistrer un commentaire