PDA

View Full Version : Help: Basic java stuff



Cartmann
02-20-2010, 08:20 PM
Hi!, I'm going througe the big javabook, and can't get one of my assignments to compile! =0


public class Age
{
public static void main(String[] arg)
{
int age;
age = 15;

if (age >= 18)
System.out.println("Your an adult.");
else
System.out.println("Your not an adult.");
if (18< age >=15) //Issue
System.out.println("But you may fuck, hell yeah!");
else
System.out.println("Dam dam dammm no womans 4 you my little amish mate");

System.out.println("You are " + age + " years old.");
}
}
Would be glad if someone could tell me how to make my 'age' go between these two bars? thanks!

Sex
02-20-2010, 08:26 PM
Maybe do 18 < age && 15 <= age?

senrath
02-20-2010, 08:28 PM
Yeah. You can't combine two boolean expressions like that. You have to do (age < 18 && age >= 15)

Diddy Kong
02-20-2010, 08:38 PM
Be sure to use braces on all your if and if-else statements

Cartmann
02-20-2010, 08:46 PM
Thanks, It's working just fine now :)

BTW, I've tried making some decent grammar in one of my other assignments, well, let's just say it didn't work out as intended!:


public class eurodollar
{
public static void main(String[] arg)
{
double commission, $, euro, result;
String dollars;
{
$ = 1234;
dollars = ("dollars");
if($=1)
dollars = ("dollar");
if($>1)
dollars= ("cents");
if($=0.01)
dollars= ("cent");

euro = $*0.74;
commission = euro*0.02;
if (commission <0.5)
commission = 0.5;
result = euro-commission;
System.out.println($+" "+dollars+" is equald to "+result+" euro, with the commission of "+commission);

}
}
}

i luffs yeww
02-20-2010, 08:47 PM
Be sure to use braces on all your if and if-else statements

If there's only one function, it's redundant, amirite?

So technically they aren't needed here, but they'd help.

senrath
02-20-2010, 08:53 PM
Thanks, It's working just fine now :)

BTW, I've tried making some decent grammar in one of my other assignments, well, let's just say it didn't work out as intended!:

You need to use ==, not just =, when you're comparing things.


If there's only one function, it's redundant, amirite?

So technically they aren't needed here, but they'd help.

Yes, but it's better to get into the habit of always doing it. It helps improve readability, and if you always put the braces in, you don't have to worry about forgetting them when you need to use them.

i luffs yeww
02-20-2010, 09:01 PM
Yes, but it's better to get into the habit of always doing it. It helps improve readability, and if you always put the braces in, you don't have to worry about forgetting them when you need to use them.

:p I meant to add that, but somehow I ended at "it'd help." x]

Cartmann
02-20-2010, 09:04 PM
Ofc senrath, it's stated in the javabook just above the assignment itself >.< Think I'll might head towards bed instead :P
But thanks for your assistance guys!

- And what do you mean about "Braces"?

senrath
02-20-2010, 09:07 PM
Ofc senrath, it's stated in the javabook just above the assignment itself >.< Think I'll might head towards bed instead :P
But thanks for your assistance guys!

- And what do you mean about "Braces"?

Instead of

if(booleanvalue)
doStuff();

do


if(booleanvalue)
{
doStuff();
}

Diddy Kong
02-20-2010, 09:10 PM
If there's only one function, it's redundant, amirite?

So technically they aren't needed here, but they'd help.

This is Java syntax; Go learn it before you post back on this thread.

i luffs yeww
02-20-2010, 09:21 PM
This is Java syntax; Go learn it before you post back on this thread.

I know this is Java.. :/ Try it, brah. :p It'll work.

void_hatred
02-20-2010, 09:23 PM
what i luffs yeww said is only partly true, you only require braces for the scope if zero to one expressions are in that scope, ie.



if (a) b;
if (a) { b; }


are the same, however in some cases braces are needed, ie.



if (a)
System.out.writeln("ohai");

// and

if (a) { }
System.out.writeln("ohai");


are different things, the first examples has no braces thus executes the next following expression

in the former example "ohai" would only be displayed if a had a truthy value, in contrast, "ohai" would always be displayed to the standard output stream in the latter example regardless of a's value

to make them the same you could do an "empty" expression, ie.



if (a) ;


that would be perfectly valid as a "empty" statement as the semicolon denotes the end of an expression (or function call etc)

hope that helped and I haven't made a fool of myself haha :)

void_hatred

i luffs yeww
02-20-2010, 09:32 PM
what i luff yeww said is only partly true, you only require braces for the scope if zero to one expressions are in that scope, ie.



if (a) b;
if (a) { b; }


are the same, however in some cases braces are needed, ie.



if (a)
System.out.writeln("ohai");

// and

if (a) { }
System.out.writeln("ohai");


are different things, the first examples has no braces thus executes the next following expression

in the former example "ohai" would only be displayed if a had a truthy value, in contrast, "ohai" would always be displayed to the standard output stream in the latter example regardless of a's value

to make them the same you could do an "empty" expression, ie.



if (a) ;


that would be perfectly valid as a "empty" statement as the semicolon denotes the end of an expression (or function call etc)

hope that helped and I haven't made a fool of myself haha :)

void_hatred

:p Why use PHP tags instead of Java? ;)


if (a) System.out.println("wat"); /*Also, void, why writeln is in SCAR, not Java, right? Sorry, I dunno. I don't really like Java.*/


Is what I said would work, and it will.


if (a) System.out.println("wat"); if (b) /*this is an example of what wouldn't work if you're wanting a && b, which is different from what I said*/ System.out.println("lolwut");


Hopefully that makes sense. I said that if it's only one function inside the if statement, then the curly brackets aren't necessary. But


if (a)
System.out.println("wat");
if (b)
System.out.println("lolwut");
/*Assuming that you only want to print "lolwut" if both a and b are true*/


is entirely different. ;) The above snippet would require nesting.