How can I create a java program using only if / else to order 5 numbers in ascending order. I can not use for processes or array. Numbers are entered by the user My code...
public static void main(String[] args) {
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
int lugar1=0;
int lugar2=0;
int lugar3=0;
int lugar4=0;
int lugar5=0;
Scanner sc = new Scanner (System.in);
System.out.println("Enter first number");
a = sc.nextInt();
System.out.println("Enter second number");
b = sc.nextInt();
System.out.println("Enter third number");
c = sc.nextInt();
System.out.println("Enter fourth number");
d = sc.nextInt();
System.out.println("Enter fifth number");
e = sc.nextInt();
if(a>b&&a>c&&a>d&&a>e)
{
lugar1=a;
}
else {
if (b>c&&b>d&&b>e&&b>a)
{
lugar1=b;
}
else {
if (c>d&&c>e&&c>a&&c>b)
{
lugar1=c;
}
else {
if (d>e&&d>a&&d>b&&d>c)
{
lugar1=d;
}
else
{
lugar1=e;
}
}
}
}
if(a<b&&a<c&&a<d&&a<e)
{
lugar5=a;
}
else {
if (b<c&&b<d&&b<e&&b<a)
{
lugar5=b;
}
else {
if (c<d&&c<e&&c<a&&c<b)
{
lugar5=c;
}
else {
if (d<e&&d<a&&d<b&&d<c)
{
lugar5=d;
}
else
{
lugar5=e;
}
}
}
}
System.out.println(lugar1);
System.out.println(lugar5);
}
I get not do that walk again, I have only 1 and 5
Your constraints do not exclude using Lists. See How to sort a ArrayList in Java?
If you can't use Lists either, I suggest trying to use Math.min, http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html , which would allow you to determine which of them is the next minimum...
Create a function to print the next smallest.
private void printNextSmallest() {
int min = Math.min(Math.min(Math.min(a,b), Math.min(c,d)),e);
if (min == Integer.MAX) {return;}
else if (a == min) { System.out.println(a + " "); printNextSmallest(Integer.MAX, b, c, d,e);}
//..
else if (e == min) { System.out.println(e + " "); printNextSmallest(a, b, c, d, Integer.MAX);
}
OK. I bit the bullet and typed it all. No loops, no arrays, no nothing.
The basic idea is to keep sorted order.
import java.util.Scanner;
public class Sort5 {
static int a1 = Integer.MIN_VALUE, a2 = Integer.MIN_VALUE, a3 = Integer.MIN_VALUE, a4 = Integer.MIN_VALUE, a5 = Integer.MIN_VALUE;
public static void placeNext(int aNew) {
if (aNew > a5) {
a1 = a2;
a2 = a3;
a3 = a4;
a4 = a5;
a5 = aNew;
} else {
if (aNew > a4) {
a1 = a2;
a2 = a3;
a3 = a4;
a4 = aNew;
} else {
if (aNew > a3) {
a1 = a2;
a2 = a3;
a3 = aNew;
} else {
if (aNew > a2) {
a1 = a2;
a2 = aNew;
} else {
a1 = aNew;
}
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
placeNext(sc.nextInt());
placeNext(sc.nextInt());
placeNext(sc.nextInt());
placeNext(sc.nextInt());
placeNext(sc.nextInt());
System.out.println("" + a1 + " | " + a2 + " | " + a3 + " | " + a4 + " | " + a5);
}
}
Unless I made a typo somewhere should work for any 5 int
s.
Use these methods:
public int getSmallestInt(int... ints) {
int smallest = Integer.MAX_VALUE;
for (int i : ints) {
smallest = Math.min(smallest, i);
}
return smallest;
}
public int getLargestInt(int... ints) {
int largest = Integer.MAX_VALUE;
for (int i : ints) {
largest = Max.max(largest, i);
}
return largest;
}
or to sort:
public static void main(String[] args) {
List<Integer> ints = new Test().sort(5, 1, 2);
System.out.println(ints.get(0)); //-> 1
}
public List<Integer> sort(Integer... ints) {
List<Integer> l = Arrays.asList(ints);
Collections.sort(l);
return l;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.