Some java stuff again.
Page 1 of 1
farne




Posts: 3725
Location: Serbia
PostPosted: Wed, 9th Nov 2011 22:06    Post subject: Some java stuff again.
Basically I want to make this program specify the lowest to highest number that I specify.

Code:
import java.util.Scanner;
public class lowtohigh {

public static void main(String[] args) {
System.out.println("Enter the three numbers: ");
Scanner input = new Scanner(System.in);

int unit1 = input.nextInt();
int unit2 = input.nextInt();
int unit3 = input.nextInt();

if (unit1 > unit2 && unit1 > unit3 && unit2 > unit3)
System.out.println(unit1 + " " + unit2 + " " + unit3);
if (unit1 > unit2 && unit1 > unit3 && unit3 > unit2)
System.out.println(unit1 + " " + unit3 + " " + unit2);

if (unit2 > unit1 && unit2 > unit3 && unit1 > unit3)
System.out.println(unit2 + " " + unit1 + " " + lunit3);
if (unit2 > unit1 && unit2 > unit3 && unit3 > unit1)
System.out.println(unit2 + " " + unit3 + " " + unit1);

if (unit3 > unit1 && unit3 > unit2 && unit1 > unit2)
System.out.println(unit3 + " " + unit1 + " " + unit2);
if (unit3 > unit1 && unit3 > unit2 && unit2 > unit1)
System.out.println(unit3 + " " + unit2 + " " + unit1);


    }
}


What I'm wondering is, is there any way to tidy up the code a bit? The lower part of the code in particular, it's awfully spamtastic Very Happy
Back to top
WhiteBarbarian




Posts: 6003
Location: Russia
PostPosted: Wed, 9th Nov 2011 22:40    Post subject:
Store numbers in array, sort it and output the content.


Back to top
garus
VIP Member



Posts: 34200

PostPosted: Wed, 9th Nov 2011 23:02    Post subject:
snip


Last edited by garus on Tue, 27th Aug 2024 21:36; edited 1 time in total
Back to top
WhiteBarbarian




Posts: 6003
Location: Russia
PostPosted: Wed, 9th Nov 2011 23:24    Post subject:
That copypasta is very quality Indian code, very good if you are payed per byte Cool Face

wizarD., with arrays it should be something like this

Code:
        Integer[] numbers = new Integer[3];
     
        numbers[0] = input.nextInt();
        numbers[1] = input.nextInt();
        numbers[2] = input.nextInt();
       
        Arrays.sort(numbers);
       
        System.out.println("numbers[] = " + Arrays.toString(numbers) );


Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: Wed, 9th Nov 2011 23:32    Post subject:
Code:
Integer[] numbers = new Integer[3];
     
for (int i = 0; i < 3; i++)
  numbers[i] = input.nextInt();

Arrays.sort(numbers);
       
System.out.println("numbers[] = " + Arrays.toString(numbers) );


You could've put it into a loop wb!

--
or
Code:

a = input.nextInt();
b = input.nextInt();
c = input.nextInt();

min = b<a?(b<c?b:c):c<a?c:a; //checking for the actual minimum



Should do the trick Smile

Basically the last part (where it checks for minimum) are the following ifs:
Code:

if (b < a){
  if (b < c)
    min = b;
  else
    min = c;
} else {
  if (c < a){
    min = c;
  } else {
    min = a;
  }
}



And sometimes it is just much easier to assume that one number is already the minimum and then you can leave out one pair of checks Smile


Last edited by PumpAction on Wed, 9th Nov 2011 23:41; edited 2 times in total
Back to top
WhiteBarbarian




Posts: 6003
Location: Russia
PostPosted: Wed, 9th Nov 2011 23:35    Post subject:
I wanted to keep it as simple as possible as wizarD. is still in earliest stages of leaning Java Smile


Back to top
PumpAction
[Schmadmin]



Posts: 26759

PostPosted: Wed, 9th Nov 2011 23:40    Post subject:
Hm... you don't think they already handled loops? Sad


=> NFOrce GIF plugin <= - Ryzen 3800X, 16GB DDR4-3200, Sapphire 5700XT Pulse
Back to top
Werelds
Special Little Man



Posts: 15098
Location: 0100111001001100
PostPosted: Thu, 10th Nov 2011 20:14    Post subject:
The cruel thing to do would've been to hand him quicksort, like we'd use Wink

He'd be like Mind Is Full Of Fuck
Back to top
BearishSun




Posts: 4484

PostPosted: Thu, 10th Nov 2011 20:30    Post subject:
Quote:
import java.util.Scanner;
public class lowtohigh {
public static void main(String[] args) {

System.out.println("Enter the three numbers from lowest to highest: ");
Scanner input = new Scanner(System.in);

int unit1 = input.nextInt();
int unit2 = input.nextInt();
int unit3 = input.nextInt();

System.out.println(unit1 + " " + unit2 + " " + unit3);
}
}


Simple!
Back to top
WhiteBarbarian




Posts: 6003
Location: Russia
PostPosted: Thu, 10th Nov 2011 20:48    Post subject:
@BearishSun:


Back to top
garus
VIP Member



Posts: 34200

PostPosted: Thu, 10th Nov 2011 21:00    Post subject:
snip
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group