Results 1 to 8 of 8
 08142012, 09:51 PM #1Member
 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;
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

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 classLast edited by Fubarable; 08152012 at 12:18 AM.
 08142012, 09:53 PM #2Member
 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
 08142012, 10:14 PM #3Member
 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; }

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.
 08152012, 08:02 AM #5
Re: Almost finished example need help on last BIT!
Also, go through the Forum Rules  particularly the third paragraph.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
 08152012, 08:44 PM #6Member
 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;
}
 08152012, 09:26 PM #7Member
 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; 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); } }
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 higestlowest/src line 4 Java Problem
Last edited by Juukamen; 08152012 at 09:32 PM.
 08162012, 10:11 AM #8Moderator
 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.Please do not ask for code as refusal often offends.
** This space for rent **
Similar Threads

help with assignment near finished
By belfast09 in forum New To JavaReplies: 11Last Post: 06152011, 04:00 AM 
Need help almost finished problem thats due
By tm02943 in forum New To JavaReplies: 2Last Post: 03222011, 08:10 AM 
Finished Product: What now?
By Unome in forum Java AppletsReplies: 5Last Post: 02112009, 10:41 AM 
finished paint!
By diggitydoggz in forum New To JavaReplies: 3Last Post: 01042009, 10:33 AM
Bookmarks