Add a loop to main in #3. You should continue to get integers from the user unti
ID: 3734989 • Letter: A
Question
Add a loop to main in #3. You should continue to get integers from the user until the user gives you a first number that is greater than or equal to the second.
import java.util.Scanner;
public class L14Number4 {
public static void main(String[] args) {
Scanner keyboard=new Scanner(System.in);
int num1, num2;
System.out.println("Enter the first integer");
num1=keyboard.nextInt();
System.out.println("Enter the second integer");
num2=keyboard.nextInt();
AddRange(num1, num2);
}
}
}
Can anyone help me to answer it using java?
I tried this answer but it does not work. It gave error L14Number4.java:8: error: incompatible types: int cannot be converted to boolean while(1) ^ L14Number4.java:19: error: cannot find symbol AddRange(num1, num2); ^ symbol: method AddRange(int,int) location: class L14Number4 2 errors.
import java.util.Scanner;
public class L14Number4 {
public static void main(String[] args) {
Scanner keyboard=new Scanner(System.in);
int num1, num2;
while(1)
{
System.out.println("Enter the first integer");
num1=keyboard.nextInt();
System.out.println("Enter the second integer");
num2=keyboard.nextInt();
if(num1 >= num2)
break;
AddRange(num1, num2);
}
}
}
Explanation / Answer
L14Number4.java
import java.util.Scanner;
public class L14Number4 {
public static void main(String[] args) {
Scanner keyboard=new Scanner(System.in);
int num1, num2;
int total = 0;
while(true)
{
System.out.println("Enter the first integer");
num1=keyboard.nextInt();
System.out.println("Enter the second integer");
num2=keyboard.nextInt();
total = total + num1+num2;
if(num1 >= num2)
break;
}
System.out.println("Total: "+total);
}
}
Output:
Enter the first integer
4
Enter the second integer
5
Enter the first integer
6
Enter the second integer
5
Total: 20
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.