Reputation: 31
im trying to code a java program that gets 5 numbers from user and determines which is the largest and which is the smallest, i got a problem with the logic , i cant quiet figure how to compare all 5 input numbers with one another and determine the largest and smallest :
import java.util.Scanner;
public class determine {
public static void main (String [] args) {
Scanner scan=new Scanner(System.in);
System.out.println("enter 5 numbers : ");
for(int i=1; i<=5; i++){
int num=scan.nextInt();
if (num is bigger than the rest)
System.out.println(num+" is the largest");
if (num is smaller than rest )
System.out.println(num+" is the smallest");
}
}
}
i sorted out the problem , is there any other way around for comparing the numbers rather than setting a value for min and max ?
import java.util.Scanner;
public class determine {
public static void main (String [] args) {
int max=-9999;
int min=9999;
Scanner scan=new Scanner(System.in);
System.out.println("enter 5 numbers : ");
for(int i=1; i<=5; i++){
int num=scan.nextInt();
if (num>max){max=num;}
if(num<min){min=num;}
}
System.out.println(max+"largest");
System.out.println(min+"smallest");
}
}
Upvotes: -2
Views: 11130
Reputation: 8497
Here's the general idea:
package max;
public class Max {
//assumes integers
//wrong output for min
public static void main(String[] args) {
int[] numbers = {99, 9, 502, 7, 3};
int indexOfMin = 0;
int indexOfMax = 0;
int currentNumber = numbers[0];
int min = numbers[0];
int max = numbers[0];
for (int i = 0; i < numbers.length; i++) {
currentNumber = numbers[i];
if (currentNumber >= max) {
max = currentNumber;
indexOfMax = i;
}
System.out.println("position " + i + " has a value of " + currentNumber);
System.out.println("current max of " + max + " is at position " + indexOfMax);
}
System.out.println("max value of " + max + " is at position " + indexOfMax);
System.out.println("min value of " + min + " is at position " + indexOfMin); //whoops, wrong output
}
}
I'll leave the user input/output to you. As food for thought, after you do this, think about how to sort the array. Don't just look up the answer, try to sort it on your own. When posting homework, make it clear what you're doing and what you're asking for.
Upvotes: 0
Reputation: 1
Create an array
int[] numbers = new int[5];
for(int i=0; i<5; i++){
numbers[i]=scan.nextInt();
}
Then sort the array;
int max=numbers[0];
for (int i=0; i<5; i++){
if (max<num[i]){
max=num[i]
}
}
System.out.println(max+" is the largest");
Upvotes: 0
Reputation: 16067
Just initialize min and max values. Then at each iteration, you'll have to make some tests on the number entered by the user and update accordingly min and max. I left the things to complete with ???.
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
System.out.println("enter 5 numbers : ");
for(int i=1; i<=5; i++){
int num=scan.nextInt();
if (?????)
max = ????;
else if (?????)
min = ????;
}
System.out.println(max+" is the largest");
System.out.println(min+" is the smallest");
Upvotes: 0
Reputation: 3822
Just use an extra variable max
in which you store the largest number you have seen so far. And for each new number you just check if the new number is larger than max
, then you assign it to max
. At the end you will have your maximum in max
;-)
Upvotes: 1