In: Computer Science
for Loop
do Loop
roll = rand.nextInt(6) + 1;
System.out.println(" Roll = " + roll);
ctr++;
Here is a sample output display:
The rolls are:
Roll = 3
Roll = 1
Roll = 4
Roll = 6
Roll = 4
Add a loop structure using the while loop and then add a for loop. The program should include 3 loops that do the same thing. What is the “best” loop structure to use for this problem and why?
Can you please tell me how to connect one class and where i should write in main or other class please
import java.util.Scanner;
import java.util.Random;
// Defines class loop
class MyLoop
{
// Scanner class object created to accept data from
console
Scanner sc = new Scanner(System.in);
// Random class object created
Random rand = new Random();
// Method to accept data till odd number is
entered
// Counts number of time loop iterated and total of
even numbers
void calculateSum()
{
// To store the number
int no;
// To store the loop
iteration
int count = 0;
// To store total
int total = 0;
// Note: While loop is used
because we don't know after how many times
// user will enter the odd
number
// Loops till odd number
while(true)
{
// Increase the
counter by one
count++;
// Accepts a
number
System.out.print("\n Enter a number (An odd number will stop the
loop): ");
no =
sc.nextInt();
// Calculate
total
total +=
no;
// Checks if the
number is odd then stop the loop
if(no % 2 ==
1)
break;
}
// Displays the number of times
loop iterated
System.out.print("\n The number of
iterations: " + count);
// Displays the total
System.out.print("\n The total of
the values: " + total);
}
// Method to display all the numbers ending in 3 from
13 - 93
void displayEndingBy3()
{
// Note: for loop is used because
we know how many times to iterate
// or stopping condition (93) and
starting value 13
// Loops from 13 to 93
for(int c = 13; c <= 93;
c++)
// Checks if
unit digit of the number is 3 then number ending in 3
if(c % 10 ==
3)
// Display the number
System.out.println(c);
}
// Method to display a string entered by the user
backwards.
void showBackward()
{
// Accepts a string
System.out.print("\n Enter a
string: ");
String data = sc.next();
System.out.println("\n Backward
String: ");
// Note: for loop is used because
we know how many times to iterate
// or stopping condition (0) and
starting value length of the string minus one
// Loops from length of the string
-1 (last index position)
// to 0 (first index
position)
for(int c = data.length()-1; c
>= 0; c--)
// Displays the
character at c index position
System.out.print(data.charAt(c));
}
// Method to generate random number between 1 and 6
and displays it
void rollDice()
{
// Counter variable
int ctr = 0;
System.out.println("\n The rolls
are: ");
// Note: The best loop structure is
is for loop
// because we know how many times
to iterate
// Loop 5 times
do
{
// Generates a
random number between 1 and 6
int roll =
rand.nextInt(6) + 1;
// Displays the
number
System.out.println(" Roll = " + roll);
// Increase the
counter by one
ctr++;
}while(ctr < 5); // Checks if
ctr value is less than 5 then continue
}
}// End of class
// Driver class definition
public class LoopDriver
{
// main method definition
public static void main(String []s)
{
// Creates an object of class
MyLoop
MyLoop loop = new MyLoop();
System.out.println("\n ********
Calculate sum till odd number entered ******** ");
loop.calculateSum();
System.out.println("\n ********
Display numer ends in 3 from 13 - 93 ******** ");
loop.displayEndingBy3();
System.out.println("\n ********
Display string backward ******** ");
loop.showBackward();
System.out.println("\n ********
Display rolled dice value ******** ");
loop.rollDice();
}// End of main method
}// End of driver class
Sample Output:
******** Calculate sum till odd number entered ********
Enter a number (An odd number will stop the loop): 12
Enter a number (An odd number will stop the loop): 4
Enter a number (An odd number will stop the loop): 6
Enter a number (An odd number will stop the loop): 5
The number of iterations: 4
The total of the values: 27
******** Display numer ends in 3 from 13 - 93 ********
13
23
33
43
53
63
73
83
93
******** Display string backward ********
Enter a string: This
Backward String:
sihT
******** Display rolled dice value ********
The rolls are:
Roll = 6
Roll = 1
Roll = 2
Roll = 3
Roll = 2