Search code examples
javaloopsswitch-statementdice

Problems with my java loop


I want to make a dice which keeps getting thrown until it hits 6: When it hits 1, it pops up with 1 and an one eyed dice and throws again,.. just with random numbers (1-6 obviously) until it hits 6. When it hits 6 it is supposed to stop.

Now I had this switch here which showed the proper number when hit, but I'm having troubles getting this switch working properly. Or it hits all numbers BUT six and keeps generating numbers, or it keeps throwing the same numbers.

Can anyone lend me a hand?

Very appreciated


public static void main(String[] args) {

    // asking what symbol to use to print the eye(s) of the dice
    System.out.print("choose symbol to use for eyes: ");

    char ch;
    Scanner sc = new Scanner(System.in);

    ch = sc.findInLine(".").charAt(0);
    int dice = (int)(6*Math.random()) + 1;


    do{
        switch(dice % 6){
            case 0: System.out.println("1");
                    System.out.println(ch);
            break;
            case 1: System.out.println("2");
                    System.out.println(ch  + "\n\n " + ch);
            break;
            case 2: System.out.println("3");
                    System.out.println(ch + "\n " + ch + "\n  " + ch);
            break;
            case 3: System.out.println("4");
                    System.out.println(ch + " " + ch + "\n" + ch + " " + ch);
            break;
            case 4: System.out.println("5");
                    System.out.println(ch + " " + ch + "\n" + " " + ch + " \n"+ ch + " " + ch);
            break;
        }
    }
    while(dice < 6);
      //  Else{ System.out.println("6");
       //         System.out.println(ch + " " + ch + "\n" + ch + " " + ch + "\n" + ch +
        //        " " + ch);
       }
    }

}

Solution

  • you generating your random number outside of the loop, so it will be same for ever here is the working version:

    public static void main(String[] args) {
    
        // asking what symbol to use to print the eye(s) of the dice
        System.out.print("choose symbol to use for eyes: ");
    
        char ch;
        Scanner sc = new Scanner(System.in);
    
        ch = sc.findInLine(".").charAt(0);
        int dice = (int)(6*Math.random()) + 1;
    
    
        do{
            switch(dice % 6){
                case 0: System.out.println("1");
                        System.out.println(ch);
                break;
                case 1: System.out.println("2");
                        System.out.println(ch  + "\n\n " + ch);
                break;
                case 2: System.out.println("3");
                        System.out.println(ch + "\n " + ch + "\n  " + ch);
                break;
                case 3: System.out.println("4");
                        System.out.println(ch + " " + ch + "\n" + ch + " " + ch);
                break;
                case 4: System.out.println("5");
                        System.out.println(ch + " " + ch + "\n" + " " + ch + " \n"+ ch + " " + ch);
                break;
            }
            dice = (int)(6*Math.random()) + 1;
        }
        while(dice < 6);
        System.out.println("6");
           //         System.out.println(ch + " " + ch + "\n" + ch + " " + ch + "\n" + ch +
            //        " " + ch);
     }
    

    here is the online working version.