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
Re: Need help with Question
If its from the user does this usually mean scanner
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).
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: ")
}
Re: Need help with Question
Re: Need help with Question
Quote:
Originally Posted by
PRIME686
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.
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.
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: " );
}
}
}
Re: Need help with Question
Re: Need help with Question
Quote:
Originally Posted by
PRIME686
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)?
Re: Need help with Question
Quote:
Originally Posted by
PRIME686
Im stuck can you help
Code java:
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));
}
}