It is possible that not using array to list the positive integer <100??
hi...
i got problem with my code.
i unable to display the largest and smallest integer in range of 1 to 100...
This is the title.
Write a program that reads a list of positive numbers < 100 and displays the largest and the smallest. A 0 (the number zero) should terminate the list. Do not use an array to answer this question. There is at least one number before the 0.
this my code.
[CODE]
Code :
public class Integer3
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// TODO code appli public static void main(String [] args)
{
int min = 0;
int max = 0;
int integer = 0;
max=min=integer;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter a positive integer or 0 to terminate: ");
integer = keyboard.nextInt();
while(integer != 0)
{
if(integer >max )
{
max = integer;
}
if(integer < min )
{
min =integer;
}
if (integer < 1 || integer >100)
{
System.out.println("Please enter again a positive integer");
integer = keyboard.nextInt()
}
}
System.out.println("Smaller integer: " + min);//display the smallest integer
System.out.println("Largest integer: "+max);//display the largest
}
}
}
Re: It is possible that not using array to list the positive integer <100??
Do you have a response to my reply on this other thread?
http://www.javaprogrammingforums.com...html#post63618
Re: It is possible that not using array to list the positive integer <100??
Code :
int max = 0;
int min = 0;
int num = 0;
min=max=num;
There's your problem, don't initialize the variables all as the same number. If you start min as zero then nothing can be less than 0, so you must initialize min as greater than it can possibly be, in this case, 100. take the min=max=num line out completely.