a little java help plwase

- English -

Resident Freddy
Joined
Apr 7, 2004
Messages
5,263
What does the following expression test?
if ( (( num % 2) == 0) ? (num >50): (num < 50) )
 

Laddey

FH is my second home
Joined
May 24, 2005
Messages
7,124
Google it! You're the one at uni, you should be telling us!
 

- English -

Resident Freddy
Joined
Apr 7, 2004
Messages
5,263
import java.util.*;
public class partb

{

public static void main(String [] args)

{

int num, num2, biggest;

Scanner kybd = new Scanner(System.in);

System.out.println("Enter your number");
num = kybd.nextInt();

if ( (( num % 2) == 0) ? (num >50): (num < 50) )

System.out.println("Your number is Less than 50");

else

System.out.println("Your number is Greater than 50");

}

}

is this program correct. If you stick a number greater than 50 in it will say its greater than 50 and vice versa. Is that what my previous post meant though?

Just noticed when i put a number in between 40 and 50 it says its greater than 50...doh
 

Gamah

Banned
Joined
Dec 22, 2003
Messages
13,042
import java.util.*;
public class partb

{

public static void main(String [] args)

{

int num, num2, biggest;

Scanner kybd = new Scanner(System.in);

System.out.println("Enter your number");
num = kybd.nextInt();

if (num < 50)

System.out.println("Your number is Less than 50");

else

System.out.println("Your number is Greater than 50");

}

}


Why is you code so over complecated, also why are you checking to see the number is whole..num is an int so cant take 76.23 etc it will just read it as 76.

This code works.

if you need any java advice add my msn (pm me for address) as I dont come here often.
 

Gamah

Banned
Joined
Dec 22, 2003
Messages
13,042
also your formatting is horrible :p

public class PartB not partb

and String[] args, not sure about the course if you get marked for that but java writing convention is important.
 

- English -

Resident Freddy
Joined
Apr 7, 2004
Messages
5,263
hey gamah check pms :)

the question didnt specify a double so just using an int. Its not to hard to change it if they want decimals so. This is just an into to java, its the way ive been taught. 7 weeks ago I didnt know anything so :D

edit; could you explain why putting 40 in prints greater than 50?
 

kirennia

Part of the furniture
Joined
Dec 26, 2003
Messages
3,857
(( num % 2) == 0)

That's checking to see if there are any decimal places.

Gamahs method is probably the best way to do it, if and else statements. you could do
if (num > 50)
{
System.out.println("It's over 50!");
}

else if (num < 50)
{
System.out.println("It's under 50!");
}

else
{
System.out.println("The number is 50!");
}


otherwise if you only use '>' instead of '>=' and the number is actually 50, you'll come up with it saying it's greater or lower when it actually isn't.

And similar to gamah, if you want any help, feel free to add me on msn.
 

Users who are viewing this thread

Top Bottom