# Almost finished example need help on last BIT!

• 08-14-2012, 10:51 PM
WakeBakeCode
Almost finished example need help on last BIT!
import java.util.Scanner;

public class twoPointSeventeen {
public static void main (String[] args) {

Scanner input = new Scanner(System.in);
int x; //variables all of type int
int y;
int z;
int sum;
int avg;
int product;
int smallest = 0;
int largest = 0;

x = input.nextInt(); //variable x takes value entered

y = input.nextInt(); //variable y takes next int entered

z = input.nextInt(); //variable y takes next int entered

sum = x+y+z; //calc sum of x and y
avg = (x+y+z)/3; //calc average of 3 integers; add them together then divide by amount of numbers(3)
product = x*y*z; //calc multiplication of x and y
-----------------------------------------------
if (x < y)
if (x < z)
x = smallest;
if (y < x)
if (y < z)
y = smallest;

if (z < x)
if (z < y)
z = smallest;

-----------------------------------------------
/*if (x < y && x < z){
x = smallest;
}
else if (y < x && y < z){
y = smallest;
}
else (z < x && z < y){
z = smallest;
}

if (x > y && x > z){
x = largest;
}
else if (y > x && y > z){
y = largest;
}
else (z > x && z > y)
z = largest; */
---------------------------------------------
System.out.printf("%s %d %s %d %s %d %s %d %s %d", "sum is", sum, "Average is", avg, "product is", product, "smallest value is", smallest, "largest value is", largest);

} //end of method
} //end of class
• 08-14-2012, 10:53 PM
WakeBakeCode
Re: Almost finished example need help on last BIT!
--------------
code withing the line is the last bit of code I need to complete, I've gone through if statements, but can't make out the efficient way once 3 numbers are involved rather than 2
if anybody could help me I'd be grateful, I need to calculate to smallest and largest numbers out of the variables x,y, z
• 08-14-2012, 11:14 PM
ics1010
Re: Almost finished example need help on last BIT!
Assign any as largest and smallest then just compare against the other 2 as in...
Code:

```largest = smallest = x; if (y < smallest) {smallest = y; } if (y > largest) {largest = y; } if(z < smallest) {smallest = z; } if(z > largest) {largest = z; }```
You're not saving anything trying to compare 2 at once, so just go with the simple logic.
• 08-15-2012, 01:19 AM
Fubarable
Re: Almost finished example need help on last BIT!
Please don't delete your question as this will not help future seekers of answers for the same problem.
• 08-15-2012, 09:02 AM
DarrylBurke
Re: Almost finished example need help on last BIT!
Also, go through the Forum Rules -- particularly the third paragraph.

db
• 08-15-2012, 09:44 PM
WakeBakeCode
Re: Almost finished example need help on last BIT!
Apologies for trying to delete my coding problem, I'm new to the forums. Thanks for the input ics1010, I done mine slightly different to yours so it's interesting to see an alternative approach. Your solution looks a lot more professional then mine, I still seem to be making java more complicated then it is :S

My solution
if (x < y && x < z){
smallest = x;
}
else if (y < x && y < z){
smallest = y;
}
else {
smallest = z;
}

if (x > y && x > z){
largest = x;
}
else if (y > x && y > z){
largest = y;
}
else{
largest = z;
}
• 08-15-2012, 10:26 PM
Juukamen
Re: Almost finished example need help on last BIT!
dude, learn to use the CODE tags =)

anyhow, here is my solution and by the help of ics1010..

Code:

```import java.util.Scanner; public class HighLow {         public static void main(String[] args) {                 Scanner input = new Scanner(System.in);                 int x, y, z ,sum, avg, product, smallest, largest;                 System.out.println("Please Enter your first Integer: "); //prompt                 x = input.nextInt(); //variable x takes value entered                 System.out.println("Please Enter your second Integer: "); //prompt                 y = input.nextInt(); //variable y takes next int entered                 System.out.println("Please Enter your third Integer: "); //prompt                 z = input.nextInt(); //variable y takes next int entered                 sum = x+y+z; //calc sum of x and y                 avg = (x+y+z)/3; //calc average of 3 integers; add them together then divide by amount of numbers(3)                 product = x*y*z; //calc multiplication of x and y                                 largest = smallest = x; // largest and smallest = value of x                 if (y < smallest ) { smallest = y; } // comparing y to x                 if (y > largest ) { largest = y; } // comparing z to x                 if (z < smallest ) { smallest = z; } // same as above, just with z                 if (z > largest ) { largest = z; } // same as above, just with z                 System.out.printf("sum is "+sum+"\nAverage is "+avg+"\nproduct is "+product+"\nsmallest value is "+smallest+"\nlargest value is "+largest);         } }```
Why the code make more lines than it should, got no idea.
Anyhow, here is something interessting, my Ecplpse tells me
Code:

```Description        Resource        Path        Location        Type Resource leak: 'input' is never closed        HighLow.java        /Java Forums - 1 higest-lowest/src        line 4        Java Problem```
That is news to me, where to read about those warrnings ?
• 08-16-2012, 11:11 AM
Tolls
Re: Almost finished example need help on last BIT!
Scanner can be used with any sort of input stream, so the compiler is warning you that you haven't closed the Scanner, and consequently the underlying stream.
In this case, since it's the System.in, it's not a problem, but if you were using it to read a File then it leave the file open.