Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By ics1010

Thread: Almost finished example need help on last BIT!

  1. #1
    WakeBakeCode is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default 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 class
    Last edited by Fubarable; 08-15-2012 at 01:18 AM.

  2. #2
    WakeBakeCode is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default 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

  3. #3
    ics1010 is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default 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.
    Juukamen likes this.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

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

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

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    WakeBakeCode is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default 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;
    }

  7. #7
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    54
    Rep Power
    0

    Default 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);
    	}
    }
    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 10:32 PM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default 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

  1. Finished the code but don't know how to run this...
    By gomdohri in forum New To Java
    Replies: 5
    Last Post: 09-08-2011, 04:36 PM
  2. help with assignment near finished
    By belfast09 in forum New To Java
    Replies: 11
    Last Post: 06-15-2011, 05:00 AM
  3. Need help almost finished problem thats due
    By tm02943 in forum New To Java
    Replies: 2
    Last Post: 03-22-2011, 09:10 AM
  4. Finished Product: What now?
    By Unome in forum Java Applets
    Replies: 5
    Last Post: 02-11-2009, 11:41 AM
  5. finished paint!
    By diggitydoggz in forum New To Java
    Replies: 3
    Last Post: 01-04-2009, 11:33 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •