Java: fill in code. import java.util.*; public class Chapter6_FillInTheCode { pu
ID: 3585747 • Letter: J
Question
Java: fill in code.
import java.util.*;
public class Chapter6_FillInTheCode
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
// exercise 21
// write a while loop that generates random integers between 3 and 7 until a 5 is generated
// and prints them all out, excluding 5
Random random = new Random();
int i = random.nextInt( 5 ) + 3; // prime read
// your code goes here
// exercise 22
// write a while loop that takes an integer input from the user, then prompts for additional integers
// and prints all integers that are greater than or equal to the original input until the user enters 20,
// which is not printed
System.out.println("Enter a starting integer > ");
int start = keyboard.nextInt(); // prime read
// your code goes here
// exercise 23
// write a while loop that takes integer values as input from the user and finds the sum of those
// integers until the user types in the value -1 which is not added
System.out.println("Enter an integer value. enter -1 to stop >");
int value = keyboard.nextInt(); // prime read
// your code goes here
// exercise 25
// write a while loop that takes words as input from the user and concatenates them until
// the user types in the word "end" which is not concatenated
String sentence = "";
String word = keyboard.next(); // prime read
// your code goes here
}
}
Explanation / Answer
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Surya
*/
import java.util.*;
public class Chapter6_FillInTheCode {
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
// exercise 21
// write a while loop that generates random integers between 3 and 7 until a 5 is generated
// and prints them all out, excluding 5
Random random = new Random();
int i = random.nextInt( 5 ) + 3; // prime read
System.out.println("Random numbers :");
while(true)//loop runs until random number 5 is generated
{
i = random.nextInt(5)+3;//between 3 and 7
if(i==5)
break;//breaking loop is 5 found
System.out.println(i);
}
// your code goes here
// exercise 22
// write a while loop that takes an integer input from the user, then prompts for additional integers
// and prints all integers that are greater than or equal to the original input until the user enters 20,
// which is not printed
System.out.println("Enter a starting integer > ");
int start = keyboard.nextInt(); // prime read
int n;//to read additional numbers..
while(true)
{
System.out.print("Enter a additional number (20 to stop)");
n = keyboard.nextInt();//reading numbers
if(n==20)//if 20 entered
break;//breaking loop
if(n>=start)//if greater than or equal to original number then print..
System.out.println(n);
}
// your code goes here
// exercise 23
// write a while loop that takes integer values as input from the user and finds the sum of those
// integers until the user types in the value -1 which is not added
int sum=0;//to find sum...
int value;
while(true)
{
System.out.println("Enter an integer value. enter -1 to stop >");
value = keyboard.nextInt();
if(value ==-1)//breaking condition
break;
sum = sum+value;//finding sum..
}
System.out.println("Sum of the numbers :"+sum);
// prime read
// your code goes here
// exercise 25
// write a while loop that takes words as input from the user and concatenates them until
// the user types in the word "end" which is not concatenated
String sentence = "";
String word ;
while(true)
{
System.out.print("Enter word:(enter :end (to stop)):");
word = keyboard.next(); // prime read
if(word.equals("end"))//if end entered
{
break;
}
sentence = sentence+" "+word;//adding words
}// your code goes here
System.out.println("The words that are entered : "+sentence);
}
}
output:-
run:
Random numbers :
7
6
Enter a starting integer >
5
Enter a additional number (20 to stop)2
Enter a additional number (20 to stop)7
7
Enter a additional number (20 to stop)9
9
Enter a additional number (20 to stop)10
10
Enter a additional number (20 to stop)3
Enter a additional number (20 to stop)20
Enter an integer value. enter -1 to stop >
1
Enter an integer value. enter -1 to stop >
2
Enter an integer value. enter -1 to stop >
3
Enter an integer value. enter -1 to stop >
4
Enter an integer value. enter -1 to stop >
5
Enter an integer value. enter -1 to stop >
7
Enter an integer value. enter -1 to stop >
8
Enter an integer value. enter -1 to stop >
-1
Sum of the numbers :30
Enter word:(enter :end (to stop)):hello
Enter word:(enter :end (to stop)):say
Enter word:(enter :end (to stop)):surya
Enter word:(enter :end (to stop)):end
The words that are entered : hello say surya
BUILD SUCCESSFUL (total time: 1 minute 27 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.