Question

In: Computer Science

Java Counter Program I can't get my program to add the number of times a number...

Java Counter Program

I can't get my program to add the number of times a number was landed on for my if statements for No12 through No2.

My Code:

import java.util.Scanner;
import java.util.Random;
   import java.lang.*;
  
   public class Dice
   {
      
       public static void main(String[] args)
       {
           Scanner in = new Scanner(System.in);
           int Continue = 1;
           //randomnum = new Random();
          
           do
           {
           RollDice();
          
           System.out.print("Would you like to go again? (Yes = 1, No =2");
           Continue = in.nextInt();
           }while(Continue == 1);  
       }

   public static void RollDice ()
   {
       Scanner in = new Scanner(System.in);
       //int die1;
       //int die2;
       int dieTotal;
       int times;
       int rollCount =1;
       int i,temp=0;
       int rollCounto = 1;
      
           int No12=0;
           int No11=0;
           int No10=0;
           int No9=0;
           int No8=0;
           int No7=0;
           int No6=0;
           int No5=0;
           int No4=0;
           int No3=0;
           int No2=0;
           Random diceRoller = new Random();
           System.out.print("Enter amount of rolls desired");  
       for(i=0; i<rollCounto;i++)
       {
           rollCounto = in.nextInt();
           rollCount = rollCounto;
          
           do
           {
               int die1 = diceRoller.nextInt(6) +1;
               int die2 = diceRoller.nextInt(6) +1;
               dieTotal = (die1 + die2);
               if(dieTotal == 12)
               {
                   No12= No12++;  
               }
               if(dieTotal == 11)
               {
                   No11= No11++;  
               }
               if(dieTotal == 10)
               {
                   No10= No10++;  
               }
               if(dieTotal == 9)
               {
                   No9= No9++;  
               }
               if(dieTotal == 8)
               {
                   No8= No8++;  
               }
               if(dieTotal == 7)
               {
                   No7= No7++;  
               }
               if(dieTotal == 6)
               {
                   No6= No6++;  
               }
               if(dieTotal == 5)
               {
                   No5= No5++;  
               }
               if(dieTotal == 4)
               {
                   No4= No4++;  
               }
               if(dieTotal == 3)
               {
                   No3= No3++;  
               }
               if(dieTotal == 2)
               {
                   No2= No2++;  
               }
               rollCount = (rollCount - 1);
           }while(rollCount >0);
           System.out.println("\n2 | " + No2 + "/36" + "\n3 | " + No3 + "/36" + "\n4 | " + No4 + "/36" + "\n5 | " + No5 + "/36" + "\n6 | " + No6 + "/36" +
           "\n7 | " + No7 + "/36" + "\n8 | " + No8 + "/36" + "\n9 | " + No9 + "/36" + "\n10 | " + No10 + "/36" + "\n11 | " + No11 + "/36" + "\n12 | " + No12 + "/36");
       break;
       }
      
      
   }
   }

Solutions

Expert Solution

//Try this java code

import java.util.Scanner;
import java.util.Random;
import java.lang.*;

public class Dice
{

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int Continue = 1;

        do
        {
            RollDice();
            System.out.print("Would you like to go again? (Yes = 1, No =2): ");
            Continue = in.nextInt();
        }while(Continue == 1);
    }

    public static void RollDice ()
    {
        Scanner in = new Scanner(System.in);
        //int die1;
        //int die2;
        int dieTotal;
        int times;
        int rollCount =1;
        int i,temp=0;
        int rollCounto = 1;

        int No12=0;
        int No11=0;
        int No10=0;
        int No9=0;
        int No8=0;
        int No7=0;
        int No6=0;
        int No5=0;
        int No4=0;
        int No3=0;
        int No2=0;
        Random diceRoller = new Random();
        System.out.print("Enter amount of rolls desired: ");

        for(i=0; i<rollCounto;i++)
        {
            rollCounto = in.nextInt();
            rollCount = rollCounto;

            do
            {
                int die1 = diceRoller.nextInt(6) +1;
                int die2 = diceRoller.nextInt(6) +1;
                dieTotal = (die1 + die2);
                if(dieTotal == 12)
                {
                    No12++;
                }
                if(dieTotal == 11)
                {
                    No11++;
                }
                if(dieTotal == 10)
                {
                    No10++;
                }
                if(dieTotal == 9)
                {
                    No9++;
                }
                if(dieTotal == 8)
                {
                    No8++;
                }
                if(dieTotal == 7)
                {
                    No7++;
                }
                if(dieTotal == 6)
                {
                    No6++;
                }
                if(dieTotal == 5)
                {
                     No5++;
                }
                if(dieTotal == 4)
                {
                    No4++;
                }
                if(dieTotal == 3)
                {
                     No3++;
                }
                if(dieTotal == 2)
                {
                     No2++;
                }
                rollCount = (rollCount - 1);
            }while(rollCount >0);
            System.out.println("\n2 | " + No2 + "/36" + "\n3 | " + No3 + "/36" + "\n4 | " + No4 + "/36" + "\n5 | " + No5 + "/36" + "\n6 | " + No6 + "/36" +
                    "\n7 | " + No7 + "/36" + "\n8 | " + No8 + "/36" + "\n9 | " + No9 + "/36" + "\n10 | " + No10 + "/36" + "\n11 | " + No11 + "/36" + "\n12 | " + No12 + "/36");
            break;
        }


    }
}

//Output

//If you need any help regarding this solution ............ please leave a comment ...... thanks


Related Solutions

My Java program keeps "running." I know I need to close a "loop" but I can't...
My Java program keeps "running." I know I need to close a "loop" but I can't find it. I'm learning how to code. This is confusing for me. import java.util.Scanner; import java.util.ArrayList; public class SteppingStone4_Loops {    public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String recipeName = ""; ArrayList<String> ingredientList = new ArrayList(); String newIngredient = ""; boolean addMoreIngredients = true; System.out.println("Please enter the recipe name: "); recipeName = scnr.nextLine();    do {    System.out.println("Would you...
I can't get the number of days to print. Here is my code: public static void...
I can't get the number of days to print. Here is my code: public static void main(String[] args) { // Prompt the user to enter year Scanner scanner = new Scanner(System.in); // Prompt the user to enter year System.out.print("Enter full year (e.g., 2016): "); int year = scanner.nextInt(); for(int i = 1; i <= 12; i++) printMonth(year, i); } /** Print the calendar for a month in a year */ static void printMonth(int year, int month) { // Print the...
// I can't get my cpp file to work with my header file and it always...
// I can't get my cpp file to work with my header file and it always says the error comes from compatibility I am using Visual Studio 2015 below is the error only my file path and program name has been erased. The issue comes up in my cpp file with the size_t empty and full functions. // Error (active)       declaration is incompatible with "bool ListType::empty()" (declared at line 12 of ListType.h)         // ListType header file #ifndef...
Please add to this Python, Guess My Number Program. Add code to the program to make...
Please add to this Python, Guess My Number Program. Add code to the program to make it ask the user for his/her name and then greet that user by their name. Please add code comments throughout the rest of the program If possible or where you add in the code to make it ask the name and greet them before the program begins. import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the...
I have tried this multiple times and I can't get the right answer. The 2014 balance...
I have tried this multiple times and I can't get the right answer. The 2014 balance sheet of Jordan’s Golf Shop, Inc., showed long-term debt of $5.2 million, and the 2015 balance sheet showed long-term debt of $5.45 million. The 2015 income statement showed an interest expense of $170,000. The 2014 balance sheet showed $520,000 in the common stock account and $5.5 million in the additional paid-in surplus account. The 2015 balance sheet showed $560,000 and $5.7 million in the...
I am struggling with this assignment. I can't get the program to run when I enter...
I am struggling with this assignment. I can't get the program to run when I enter a number with the $ symbol followed by a number below 10. any help would be greatly appreciated. Create a program named Auction that allows a user to enter an amount bid on an online auction item. Include three overloaded methods that accept an int, double, or string bid. Each method should display the bid and indicate whether it is over the minimum acceptable...
Make a java program of Mickey I have the starter program but I need to add...
Make a java program of Mickey I have the starter program but I need to add eyes and a smile to it. import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.JFrame; public class Mickey extends Canvas { public static void main(String[] args) { JFrame frame = new JFrame("Mickey Mouse"); Canvas canvas = new Mickey(); canvas.setSize(400, 400); canvas.setBackground(Color.white); frame.add(canvas); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { Rectangle bb = new Rectangle(100, 100, 200, 200); mickey(g, bb); } public void...
I'm trying to make this C++ loan calculator but I can't get the program to output...
I'm trying to make this C++ loan calculator but I can't get the program to output what I need. For instance I know the formula is right and when I enter 100000 1.5 20 as my cin variables I should get 482.55 but I get 1543.31. What am I not seeing as the issue? Also this should cout only 2 decimal places right? I'm not allowed to alter the #include and add any fixed setprecision. This is what I have....
Hello! I need to add a method that will display the total number of times the...
Hello! I need to add a method that will display the total number of times the user answered the question correctly, how many time he was right or wrong and the percentage of time they were correct in answering. import java.util.Random; import java.util.Scanner; public class Test { private static int getUserInput() { int n; Scanner scanner = new Scanner(System.in); n = scanner.nextInt(); return n; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String choice = "Yes";...
I get an error 1452 saying it cannot add or update my foreign key. is there...
I get an error 1452 saying it cannot add or update my foreign key. is there a way to fix this
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT