Results 1 to 11 of 11
Thread: If statements
- 11-14-2012, 01:51 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 13
- Rep Power
- 0
If statements
I am working on a program that prompts the user to input 3 numbers and it evaluates the input and displays the numbers in nondecending order. My question is I have a couple lines of code that a classmate helped me write but he didn't offer me an explanation as to why this works, can anyone break it down for me?
if (val3 <= val1)
System.out.println(val3 + " " + val1 + " " + val2);
else if (val1 <= val3 && val3 <= val2)
System.out.println(val1 + " " + val3 + " " + val2);
else
System.out.println(val1 + " " + val2 + " " + val3);
here is my complete code:
import java.util.*;
public class Evaluate_3
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
double val1, val2, val3;
double temp;
System.out.print("Enter three numbers with a space seperating each one: ");
val1 = console.nextDouble();
val2 = console.nextDouble();
val3 = console.nextDouble();
if (val1 > val2)
{
temp = val1;
val1 = val2;
val2 = val3;
}
System.out.printf("Your numbers in non decending order: ");
if (val3 <= val1)
System.out.println(val3 + " " + val1 + " " + val2);
else if (val1 <= val3 && val3 <= val2)
System.out.println(val1 + " " + val3 + " " + val2);
else
System.out.println(val1 + " " + val2 + " " + val3);
}
}
- 11-14-2012, 02:08 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: If statements
Please use [code] tags [/code] when posting code, as that's almost unreadable without them.
Does that code actually work?Please do not ask for code as refusal often offends.
- 11-14-2012, 02:26 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 13
- Rep Power
- 0
Re: If statements
it does yes
- 11-14-2012, 02:51 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: If statements
Does it work for val1 > val2?
Try it with the following numbers -> 10, 4, 8.Please do not ask for code as refusal often offends.
- 11-14-2012, 03:09 PM #5
Member
- Join Date
- Nov 2012
- Posts
- 13
- Rep Power
- 0
Re: If statements
Ah good catch no it does not work, in that case it lists 4, 8, 8
- 11-14-2012, 03:32 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: If statements
So this part needs correcting first:
Java Code:if (val1 > val2) { temp = val1; val1 = val2; val2 = val3; }Please do not ask for code as refusal often offends.
- 11-14-2012, 06:10 PM #7
Member
- Join Date
- Nov 2012
- Posts
- 13
- Rep Power
- 0
Re: If statements
Ok, that's kind of the part I was confused on, my counterpart didn't want to explain to me why he just wanted to write it and get it over with. I will read the section on if statements again in my textbook and post the result once it is corrected. Thank you for looking.
- 11-14-2012, 06:38 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Re: If statements
Have a look at the Math.min( ... ) and Math.max( ... ) methods; also note that a+b+c-M-m (where M and m and the maximum and minimum of the three numbers a, b and c) results in the third 'middle' number.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-15-2012, 04:28 AM #9
Member
- Join Date
- Nov 2012
- Posts
- 13
- Rep Power
- 0
Re: If statements
I changed up everything, rewrote my if and else statements and I THINK I have got it so it is right (seems to work with any combination)
Java Code:import java.util.Scanner; public class Evaluate_3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a number: "); double A = input.nextDouble(); System.out.print("Enter a second number: "); double B = input.nextDouble(); System.out.print("Enter a third number: "); double C = input.nextDouble(); System.out.println("The numbers in non decending order are: "); displaySortedNumbers(A, B, C); } public static void displaySortedNumbers(double A, double B, double C) { if ( (A <= B) && (A <= C) ) { System.out.print(A); if (B <= C) System.out.println(" " + B + " " + C); else System.out.println(" " + C + " " + B); } else if ( (B <= A) && (B <= C) ) { System.out.print(B); if (A < C) System.out.println(" " + A + " " + C); else System.out.println(" " + C + " " + A); } else if ( (C <= A) && (C <= B) ) { System.out.print(C); if (A < B) System.out.println(" " + A + " " + B); else System.out.println(" " + B + " " + A); } } }Last edited by Tolls; 11-15-2012 at 09:42 AM. Reason: Correct tags
- 11-15-2012, 07:56 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Re: If statements
grmph ...
kind regards,Java Code:int max= Math.max(A, Math.max(B, C)); int min= Math.min(A, Math.min(B, C)); int mid= A+B+C-max-min;
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-15-2012, 09:41 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
If Statements
By WillHorne in forum New To JavaReplies: 7Last Post: 03-01-2012, 12:47 PM -
If Statements
By katymccl in forum New To JavaReplies: 6Last Post: 02-02-2012, 08:06 AM -
if else statements
By sweetpea123 in forum New To JavaReplies: 4Last Post: 04-12-2010, 07:02 PM -
age: using if statements
By yasmin k in forum New To JavaReplies: 2Last Post: 10-04-2009, 09:50 PM -
Help with if-else statements
By porchrat in forum New To JavaReplies: 4Last Post: 03-23-2009, 04:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks