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??
This question should be posted in the "What is wrong" section.
Can you post the output from when you execute your code that shows the problem?
And explain what is wrong with it.
The trick with searching for a largest/smallest number is to assume the first number is the correct one and assign its value to the variable. As you compare against the other numbers, change it if you find one that is a better answer.
Another way is to initialize the largest variable to the smallest possible value of a number so that any number that you look at will be larger. And the opposite for smallest, start it at the high value so any number will be smaller.
Re: It is possible that not using array to list the positive integer <100??
sorry to sent wrong site...
can wait a minute..due to my netbean got a problem.i need re install back..sorry for that..
Re: It is possible that not using array to list the positive integer <100??
i change to this code.
Code :
Scanner in = new Scanner(System.in);
int biggest =0;
int smallest =100;
int number =1;
System.out.print("Write number at one at time and press the enter\n(O to end the program and display result:)\n");
while(number !=0)
{
number=in.nextInt();
if(number <1)
{
System.out.println("Please enter positive integer");
}else if( number >100)
{
System.out.println("Please enter less than 100 of positive integer");
}
if(number >biggest)
biggest=number;
if(number < smallest)
smallest =number;
}
System.out.println("Biggest:" +biggest+ "Smallest:"+smallest);
}
}
the output:
Write number at one at time and press the enter
( O to end the program and display result: )
123
Please enter less than 100 of positive integer
12
-1
Please enter positive integer
100
2
0
Please enter positive integer
Biggest:123Smallest:-1
The biggest should be 100 and smallest should be 2..
but i can't figure out how to do it..
Re: It is possible that not using array to list the positive integer <100??
What does the code do when it finds a number out of range and prints a message? What does it do next?
Do you know about the continue statement? That will help you change how the code executes.
Re: It is possible that not using array to list the positive integer <100??
skip to next iteration..
but just now i put continue inside the while...it seem appear same things..
Re: It is possible that not using array to list the positive integer <100??
Post the new code and the console from when you execute it.
Re: It is possible that not using array to list the positive integer <100??
Code :
while(number !=0)
{
number=in.nextInt();
if(number <1)
{
System.out.println("Please enter positive integer");
}else if( number >100)
{
System.out.println("Please enter less than 100 of positive integer");
}
if(number >biggest)
biggest=number;
if(number < smallest)
smallest =number;
continue;
}
System.out.println("Biggest:" +biggest+ "Smallest:"+smallest);
}
i put inside it...
Does this " Write a program that reads a list of positive numbers < 100" mean print out the 100 integer??
Re: It is possible that not using array to list the positive integer <100??
What do you want the program to do when it finds an invalid input and prints out a message?
Your code then tests the number.
continue at the end of a loop is redundant. The loop will normally go to the top there without the continue.