Write a Java application program that will accept only 2 integers from the user,
ID: 3825273 • Letter: W
Question
Write a Java application program that will accept only 2 integers from the user, and then show those numbers in ascending order, the largest integer, the smallest integer, and the sum of all integers. Please stop your program with a thank-you message if the user enters zero. Please see the sample test below to design your application program properly. This program is like a number game for user to play. You cannot use array for this assignment since we have not talked about array structure yet. That's why we only allow user to give you 2 numbers only. You may declare 4 integers: n1, n2, largest, smallest, and sum. You may use Math.max(n1, n2) to find the largest number, and Math.min(n1, n2) to get the smallest one. You may use if statement to get max and min. Your if statement may look like the following: if (n1 > = n2) {largest = n1; smallest = n2;} else {largest = n2; smallest = n1;} The following is a sample test, which must be your test case #1. You must also do test case #2 and test case #3 with different set of data. Each test case must cover at least two good results plus some invalid input numbers, with integer zero to stop this number game. Welcome to play the number game! Please enter how many integers you would like to play (2 only, 0 to stop) > 2 Please enter 2 integers in any order > 222 111 Your 2 integers in ascending order are: 111 222 The largest integer is 222, the smallest integer is 111, and the sum of all integers is 333 Please enter how many integers you would like to play (2 only, 0 to stop) > 2 Please enter 2 integers in any order > 23 12 Your 2 integers in ascending order are: 12 23 The largest integer is 23, the smallest integer is 12, and the sum of all integers is 35 Please enter how many integers you would like to play (2 only, 0 to stop) > 1 Please enter how many integers you would like to play (2 only, 0 to stop) > 4 Please enter how many integers you would like to play (2 only, 0 to stop) > 99 Please enter how many integers you would like to play (2 only, 0 to stop) > 0 Thank you for playing this number game. Hope to see you again!Explanation / Answer
import java.util.*;
public class HelloWorld{
public static void play(int num){
int num1,num2;
Scanner sc=new Scanner(System.in);
if(num==0){
System.out.println("Thankyou for playing this game .Hope to see you again!");
}
else if(num==2){
System.out.println("please enter 2 integers in any order>");
num1=sc.nextInt();
num2=sc.nextInt();
int great=Math.max(num1,num2);
int small=Math.min(num1,num2);
System.out.println("Your 2 integers in ascending order are: "+small+" "+great);
int sum= num1+num2;
System.out.println("The largest integer is "+great+",the smallest integer is "+small+",and the sum of all integer is "+sum);
System.out.println("PLEASE ENTER HOW MANY INTEGERS YOU WOULD LIKE TO PLAY(2 ONLY, 0 TO STOP)>");
num=sc.nextInt();
play(num);
}
else
{
System.out.println("PLEASE ENTER HOW MANY INTEGERS YOU WOULD LIKE TO PLAY(2 ONLY, 0 TO STOP)>");
num=sc.nextInt();// Take input from user till user enters 2 numbers to play
play(num);
}
}
public static void main(String []args){
System.out.println("WELCOME TO PLAY THE NUMBER GAME");
System.out.println("PLEASE ENTER HOW MANY INTEGERS YOU WOULD LIKE TO PLAY(2 ONLY, 0 TO STOP)>");
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
play(num);// call function play
}
}
OUTPUT:
sh-4.3$ javac HelloWorld.java
sh-4.3$ java -Xmx128M -Xms16M HelloWorld
WELCOME TO PLAY THE NUMBER GAME
PLEASE ENTER HOW MANY INTEGERS YOU WOULD LIKE TO PLAY(2 ONLY, 0 TO STOP)
2
please enter 2 integers in any order>
222 111
Your 2 integers in ascending order are:111 222
The largest integer is 222,the smallest integer is111,and the sum of all integer is333
PLEASE ENTER HOW MANY INTEGERS YOU WOULD LIKE TO PLAY(2 ONLY, 0 TO STOP)
2
please enter 2 integers in any order>
12 23
Your 2 integers in ascending order are:12 23
The largest integer is 23,the smallest integer is12,and the sum of all integer is35
pLEASE ENTER HOW MANY INTEGERS YOU WOULD LIKE TO PLAY(2 ONLY, 0 TO STOP)
1
PLEASE ENTER HOW MANY INTEGERS YOU WOULD LIKE TO PLAY(2 ONLY, 0 TO STOP)
4
PLEASE ENTER HOW MANY INTEGERS YOU WOULD LIKE TO PLAY(2 ONLY, 0 TO STOP)
99
PLEASE ENTER HOW MANY INTEGERS YOU WOULD LIKE TO PLAY(2 ONLY, 0 TO STOP)
0
Thankyou for playing this game .Hope to see you again!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.