Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Need help with Question

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with Question

    Write a program to read three numbers from the user and print which number was the largest (first number, second number or third number). The program should also print which of the numbers is the smallest. You may assume that the numbers are not equal. 12 13 14


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Question

    Ok first of all, this should be pretty basic so don't worry.

    Now you need to use java's scanner to read input from the user, meaning you have to first import scanner:

    import java.util.Scanner;

    Then you have to use the scanner to read three inputs from the user. Of course you should prompt the user
    to enter each number separately. Once the number is read, they will be stored as a certain variable in your
    code.

    Finally you need to use an if else statement to set these entered variables as "largest", "medium", or "smallest"
    (make these three variables before hand). And once these variables are labelled as "largest", "medium", or "smallest"
    they can be easily printed with "System.out.println()" in the order you wish.

    If you need an in-depth explanation on any of these things (like the Scanner or if else statements), please post
    another message and I'll try help you with that.

    - Kranti

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Question

    If its from the user does this usually mean scanner

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Question

    Yes, scanner will allow any user to input numbers or letters into command prompt or the console box if you are using Eclipse.

    So create a scanner variable, and use that to read three numbers off from the user, and set these three numbers as variables
    num1, num2, and num3 or something. Then use if else statements to check which of these numbers are bigger, and set the
    biggest one as the variable "largest", the medium one as "medium", and the smallest number as "smallest".

    Then simply "System.out.println(largest, medium, smallest);" to print out the numbers from largest to smallest (or in any fashion
    you wish).

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Question

    import java.util.Scanner;
    public class AS1Q5 {
    public static void main(String args[]) {

    double a, b, c;
    Scanner number = new Scanner(System.in);
    System.out.print("Write a nubmer: " );
    a = number.nextDouble();
    Scanner number2 = new Scanner(System.in);
    System.out.print("Write another number: ");
    b = number.nextDouble();
    Scanner number3 = new Scanner(System.in);
    System.out.print("Write a final number: ");
    c = number.nextDouble();
    if(a > b & c)
    {
    System.out.println("The first number is the largest: ")
    }

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Question

    Im lost after this part

  7. #7
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Need help with Question

    Quote Originally Posted by PRIME686 View Post
    Im lost after this part
    Why are you lost? You are in the right direction. You just need to check again the if statement syntax.
    if(a > b & c)
    Firstly, you should better use the logical operator && instead of the bitwise &. And secondly, this is not the correct way to evaluate if a number is greater than two(or more) others Here is a useful link for logical operators.
    Also, you should always have the logic in a piece paper before trying implement code.

  8. #8
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Question

    import java.util.Scanner;
    public class AS1Q5 {
    public static void main(String[] args) {

    double a, b, c;
    Scanner number = new Scanner(System.in);
    System.out.print("Write a nubmer: ");
    a = number.nextDouble();
    Scanner number2 = new Scanner(System.in);
    System.out.print("Write a second number: ");
    b = number.nextDouble();
    Scanner number3 = new Scanner(System.in);
    System.out.print("Write a third number: ");
    c = number.nextDouble();
    if(a > b + c )
    {
    System.out.println("The first number is the smallest: " );
    System.out.println("The third number is the largest: " );
    } else if (a < b + c ){
    System.out.println("The first number is the largest: " );
    System.out.println("The third number is the smallest: " );
    } else if (b > a + c){
    System.out.println("The second number is the largest: " );
    System.out.println("The third number is the smallest: " );

    }
    }
    }

  9. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Question

    Im stuck can you help

  10. #10
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Need help with Question

    Quote Originally Posted by PRIME686 View Post
    Im stuck can you help
    Where are you stuck?
    if(a > b + c )
    This condition will evaluate to true if a is bigger than the sum of b and c. Is this what you want? Don't you want to a be bigger than b and bigger than c?
    Did you write down to a piece of paper the logic as I suggested you? How would you check if a number is greater than other two (not in Java, if somebody asks you to)?

  11. #11
    Junior Member hackthisred's Avatar
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    1
    Thanked 1 Time in 1 Post

    Cool Re: Need help with Question

    Quote Originally Posted by PRIME686 View Post
    Im stuck can you help
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.Scanner;
     
     
    public class NumberValueComparison {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		//Using generics to declare my list to be of type Double same goes for my ArrayList
    		List<Double> numbers = new ArrayList<Double>();
     
     
    		Scanner keyboard = new Scanner(System.in);
     
    		System.out.println("This program will evaluate the values of 3 numbers: ");
     
    		System.out.println("Please key in the first number to be evaluated");
    		numbers.add(keyboard.nextDouble()) ;
     
    		System.out.println("Please key in the second number to be evaluated");
    		numbers.add(keyboard.nextDouble()) ;
     
    		System.out.println("Please key in the final number to be evaluated");
    		numbers.add(keyboard.nextDouble()) ;
     
    		//Goes through the list of numbers keyed in and orders them smallest to largest
    		//Collections are a cool type to use on Lists for this type remedial logic
    		Collections.sort(numbers);
     
    		//Arrays and Lists start at position 0 not 1 jsyk
    		System.out.println("The greatest number is: "+numbers.get(2));
    		System.out.println("The median number is: " + numbers.get(1));
    		System.out.println("The smallest number is: " + numbers.get(0));
     
    	}
     
    }
    Last edited by hackthisred; April 29th, 2012 at 05:41 PM. Reason: formatting

Tags for this Thread