# Thread: Almost finished example need help on last BIT!

1. Member Join Date
Aug 2012
Posts
3
Rep Power
0

## 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
Last edited by Fubarable; 08-15-2012 at 12:18 AM.  Reply With Quote

2. Member Join Date
Aug 2012
Posts
3
Rep Power
0

## 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  Reply With Quote

3. Member Join Date
Aug 2012
Posts
10
Rep Power
0

## Re: Almost finished example need help on last BIT!

Assign any as largest and smallest then just compare against the other 2 as in...
Java 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.  Reply With Quote

4. ## 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.  Reply With Quote

5. ## Re: Almost finished example need help on last BIT!

Also, go through the Forum Rules -- particularly the third paragraph.

db  Reply With Quote

6. Member Join Date
Aug 2012
Posts
3
Rep Power
0

## 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;
}  Reply With Quote

7. Member Join Date
Oct 2011
Location
Tromsø
Posts
58
Rep Power
0

## 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..

Java 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;

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

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
Java 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 ?
Last edited by Juukamen; 08-15-2012 at 09:32 PM.  Reply With Quote

8. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## 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.  Reply With Quote

arithmetic, basic, coding, examples, java 