Q: Mini-program: Write a console program that prompts the user for
an integer. As long as the user keeps entering positive integers,
continue prompting for the next integer. Once the user enters an
integer of 0 or less, report the largest integer that was entered
and end the program.
Solution:
import java.util.ArrayList;
import java.util.Scanner;
public class LargestNumber
{
public static void main(String args[])
{
int ending=0;
ArrayList<Integer> list=new ArrayList<Integer>();/* declare arrylist to store the value*/
int value=1;
while(value>0)
{
System.out.println("Please Enter the number");/* promt the user to give the number*/
Scanner in=new Scanner(System.in);
value=in.nextInt();/* store the entered value */
ending =value;
if(value>0){/* check if the value is greater than zero than only it will store*/
list.add(value);
}
}
int max=0;
for(int i=0;i<list.size();i++)
{
if(list.get(i).intValue()>max)
max=list.get(i).intValue();
}
System.out.println("----------------------------------------");
System.out.println("largest number is=");/* print the largest number*/
System.out.println(max);
}
}
Solution:
import java.util.ArrayList;
import java.util.Scanner;
public class LargestNumber
{
public static void main(String args[])
{
int ending=0;
ArrayList<Integer> list=new ArrayList<Integer>();/* declare arrylist to store the value*/
int value=1;
while(value>0)
{
System.out.println("Please Enter the number");/* promt the user to give the number*/
Scanner in=new Scanner(System.in);
value=in.nextInt();/* store the entered value */
ending =value;
if(value>0){/* check if the value is greater than zero than only it will store*/
list.add(value);
}
}
int max=0;
for(int i=0;i<list.size();i++)
{
if(list.get(i).intValue()>max)
max=list.get(i).intValue();
}
System.out.println("----------------------------------------");
System.out.println("largest number is=");/* print the largest number*/
System.out.println(max);
}
}
No comments:
Post a Comment