write a Java program that contains 2 loops, first loop validates users input and
ID: 3736807 • Letter: W
Question
write a Java program that contains 2 loops, first loop validates users input and should be written with a "While loop" and second loop (stacked loop structures) performs the output based on the number the user inputs and the output should be the number the user input with it's value doubled and tripled, Second loop should be written with a "For loop". Make the user input a number between 0 to 100 ( 0 is to exit the program) (validate input number from to make sure they entered a number 1 through 100, if they enter a number less than zero or more than 100 output an error message and continue to ask the user to input a number until they enter a valid number; 1 to 100; 0 to exit) example: if the user input number "2" then the output will look like this: 2,.. (sequential) 4,... (doubled) 8,...(tripled) then rerun
Explanation / Answer
import java.lang.*;
import java.util.*;
class Loop{
public static void main(String args[]){
Scanner input =new Scanner(System.in);
System.out.print("Enter a number:");
int x = input.nextInt();
while(x!=0){ // 0 to exit condition
if(x>0 && x<101){ // proceed to next loop if input 1-100 Otherwise Error message (except 0)
System.out.print(x+" ");
int y=x;
for(int i=1;i<3;i++){
y = y * x;
System.out.print(y+" ");
}
System.out.println();
}else{
System.out.println("Invalid Input!!, please provide valid input (0-100)"); //Error message
}
System.out.print("Enter a number:");
x = input.nextInt();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.