Question

java question, my questions are in the following codes with comments, just answer next or under...

java question, my questions are in the following codes with comments, just answer next or under it thanks!

theres only few, so no worry.

import java.security.SecureRandom;

public class clsTestRand {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       //int num1, num2, num3, num4, num5, num6;
       int num1 = 0;
       int num2 = 0;
       int num3 = 0;
       int num4 = 0;
       int num5 = 0;
      
      
       SecureRandom rn = new SecureRandom(); /
      
       for (int i= 1; i<=10; i++) // so random numbers between 1 to 10 right?
       {
           int roll =1 +rn.nextInt(6);// add 1 because it does not count 6, count from 0 (this one isn't question, we are just assuming that this is a dice)
      
           switch (roll)
           {
           case 1:
               ++num1; // why do we use ++ ? and what it does?
               break;
           case 2:
           ++num2;
           break;
           case 3:
               ++num3;
               break;
           case 4:
               ++num4;
               break;
           case 5:
               ++num5;
               break;
          
           }//end switch
          
       }//end for loop
      
       System.out.println("1: "+num1); // +num1 using println was equal to printf %d%n, so I guess it's just different way right? so it's like " : " + variable + variable if we want more than 1 variable if we need it inside println?
       System.out.printf("2: %d%n", num2);
       System.out.printf("3: %d%n", num3);
       System.out.printf("4: %d%n", num4);
       System.out.printf("5: %d%n", num5);
      
   }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.security.SecureRandom;

public class clsTestRand {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       //int num1, num2, num3, num4, num5, num6;
       int num1 = 0;
       int num2 = 0;
       int num3 = 0;
       int num4 = 0;
       int num5 = 0;
      
      
       SecureRandom rn = new SecureRandom(); /
      
       for (int i= 1; i<=10; i++) // so random numbers between 1 to 10 right?

//NO its not random number between 1 and 10, It repeats following statements within the block 10 times, so to generate 10 //random numbers and do operations on them
       {
           int roll =1 +rn.nextInt(6);// add 1 because it does not count 6, count from 0 (this one isn't question, we are just assuming that this is a dice)
      
           switch (roll)
           {
           case 1:
               ++num1; // why do we use ++ ? and what it does?

//++num1 is pre increment operator,it increases the value of num1 by 1 and then assigns it.
               break;
           case 2:
           ++num2;
           break;
           case 3:
               ++num3;
               break;
           case 4:
               ++num4;
               break;
           case 5:
               ++num5;
               break;
          
           }//end switch
          
       }//end for loop
      
       System.out.println("1: "+num1); // +num1 using println was equal to printf %d%n, so I guess it's just different way right? so it's like " : " + variable + variable if we want more than 1 variable if we need it inside println?

//println("1: "+num1) is like printf("1: %d",num1)....to print more than one variable in println, use println(" "+num1+" "+num2+" //"+num3)
       System.out.printf("2: %d%n", num2);
       System.out.printf("3: %d%n", num3);
       System.out.printf("4: %d%n", num4);
       System.out.printf("5: %d%n", num5);
      
   }

}

Add a comment
Know the answer?
Add Answer to:
java question, my questions are in the following codes with comments, just answer next or under...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA I need a switch so that users can input one selection then another. These selections...

    JAVA I need a switch so that users can input one selection then another. These selections are for a simple calculator that uses random numbers 1. + 2. - 3. * 4./ 5. RNG. This has to be simple this is a beginners class. Here is what I have import java.util.Scanner; import java.util.Random; import java.util.*; public class You_Michael02Basic_Calculator {    public static void main(String[] args) {        // Generate random numbers        int num1,num2;        num1 =(int)(Math.random()*100+1);...

  • What I need: Create a new program named Reverse4 and declare four integer variables with the...

    What I need: Create a new program named Reverse4 and declare four integer variables with the values 23, 45, 55, and 67. Add a method named Reverse that reverses the positions of four integer variables. The Reverse method should accept the variables as references. Write a Main() method that displays the four variables both before and after reversing them to ensure the method works correctly. What I have: using System; class Reverse4 { public static void reverse(ref int num1, ref...

  • Could someone re-write this code so that it first prompts the user to choose an option...

    Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things. ------ import java.util.*; abstract class CalculatorNumVals { int num1,num2; CalculatorNumVals(int value1,int value2)...

  • Java debugging in eclipse package edu.ilstu; import java.util.Scanner; /** * The following class has four independent...

    Java debugging in eclipse package edu.ilstu; import java.util.Scanner; /** * The following class has four independent debugging * problems. Solve one at a time, uncommenting the next * one only after the previous problem is working correctly. */ public class FindTheErrors { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); /* * Problem 1 Debugging * * This problem is to read in your first name, * last name, and current year and display them in *...

  • question of java , very simple one written under with comment, just reply next or below...

    question of java , very simple one written under with comment, just reply next or below it, thanks public static void main(String[] args) {        // TODO Auto-generated method stub        int i, j;        final int ARRAY_LENGTH = 5;        int[] myArray = new int[ARRAY_LENGTH];               for(i=0; i< myArray.length; i++)        {            myArray[0] += 2;          }               for(j = 0; j       ...

  • #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int...

    #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int num1, num2, result; while(ch == 'Y'){ cout << "Enter first number: "; cin >> num1; while(1){//for handling invalid inputs if(cin.fail()){ cin.clear();//reseting the buffer cin.ignore(numeric_limits<streamsize>::max(),'\n');//empty the buffer cout<<"You have entered wrong input"<<endl; cout << "Enter first number: "; cin >> num1; } if(!cin.fail()) break; } cout << "Enter second number: "; cin >> num2; while(1){ if(cin.fail()){ cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout<<"You have entered wrong input"<<endl; cout <<...

  • I'm kind of new to programming, and I am having trouble figuring out why my program...

    I'm kind of new to programming, and I am having trouble figuring out why my program isn't running. Below is the code that I wrote for practice. I will comment where it says the error is. So the error that I'm getting is "error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' ". I'm not sure why I'm getting this because I added the library <iostream> at the top. Thank you. Code: #include <iostream> using namespace std; class...

  • JAVA. Rolling a die 60,000 times with streams. Modify the code below to capture the time before a...

    JAVA. Rolling a die 60,000 times with streams. Modify the code below to capture the time before and after evaluating the stream pipeline (use package java.time which contains instant and duration). Then calculate the difference between the Instants to determine the total time. Use Instant's static method now to get the current time. To determine the difference between two Instants, use class Duration's static method between, which returns a Duration object containing the time difference. Duration provides methods like toMillis...

  • Please answer both questions thank you. Question 49 (1 point) What is wrong with the following...

    Please answer both questions thank you. Question 49 (1 point) What is wrong with the following code snippet? for (int i 1; i <= 5; i++) { int squared = i + i; System.out.println(i + IT "! squared = + squared); } System.out.println("Last square = " + squared); calculation error variable scope error O nothing incorrect use of for loop Question 47 (1 point) What are the values of num1 and num2 and result after the execution of mystery (num1,...

  • NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------...

    NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------------------------------------------------- Tasks to complete: ---------------------------------------------------------------------------------------------------------------------------------------- given code: ----------------------------------------------------------------------------------------------------------------------------------------------- main.cpp #include <iostream> #include <iomanip> #include "fractionType.h" using namespace std; int main() { fractionType num1(5, 6); fractionType num2; fractionType num3; cout << fixed; cout << showpoint; cout << setprecision(2); cout << "Num1 = " << num1 << endl; cout << "Num2 = " << num2 << endl; cout << "Enter the fraction in the form a / b: "; cin >> num2; cout << endl; cout <<...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT