Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Need help

Started by Fabricant Jan 5, 2001 at 7:06 AM 11 replies 950+ views
Original Post
Fabricant
Fabricant
Hi I wrote this program : class elseif { public static void main(String args[]) { int x=2, y=3, z=4; if(x == 2 && y <= 3) { System.out.println("true!"); } else { System.out.println("false!"); } if(x == 2 || y != 2 && z * z >= 16) { System.out.println("true!"); } else { System.out.println("false!"); } if(y == 3 && !(z == 1)) { System.out.println("true!"); } else { System.out.println("false!"); } if(x != 2 && y > 3) { System.out.println("true!"); } else { System.out.println("false!"); } if(x < y || x > z) { System.out.println("true!"); } else { System.out.println("false!"); } } } > My Question : Why does the code below not work (it shows no answers). It would be a lot easier to write >like I did below. Thanks for help ! class elseif { public static void main(String args[]) { int x=2, y=3, z=4; if(x == 2 && y <= 3) if(x == 2 || y != 2 && z * z >= 16) if(y == 3 && !(z == 1)) if(x != 2 && y > 3) if(x < y || x > z) { System.out.println("true!"); } else { System.out.println("false!"); } } }
-Fabricant
Nytegard
Nytegard
  
if(x == 2 && y <= 3)
if(x == 2 || y != 2 && z * z >= 16)
if(y == 3 && !(z == 1))
if(x != 2 && y > 3)
if(x < y || x > z){
System.out.println("true!");
}
else{
System.out.println("false!");
}



That''s what your code essentially is read as. The first if, x has to equal 2, whereas to even display an answer, x cannot equal 2.
Fabricant
Fabricant
why ?

> int x=2

so x == 2 must be right !?
-Fabricant
c_wraith
c_wraith
I can think of three different ways you might mean... First, you might mean "if all conditionals are true, print true," second, you might mean "if any conditional is true, print true,", or third, which is probably what you want, "for each conditional, print true if it is true."

Here''s a bit of sample code based on how I''d do the third case, which is what I think you mean, based on your first example:

  
public class ElseIf
{
public static void main(String [] args)
{
int x=2, y=3, z=4;

test(x == 2 && y ==3);
test(x == 2 || y != 2 && z * z >= 16);
test(y == 3 && !(z == 1));
test(x != 2 && y > 3);
test(x < y || x > z);
} //main


public static void test(boolean b)
{
if (b) System.out.println("true!");
else System.out.println("false!");
} //test


} //class ElseIf



I''d guess, from your questions, that you''re quite new to java programming, possibly taking a class or doing some tutorials from a book. Either way, I''m probably jumping ahead of what you''re "supposed" to know by including a seperate function to simplify the process.

I''d say more important is Nytegard''s demonstration. It shows how the compiler is interpreting the code you''re entering. It treats each if as nested inside the previous... That is, if the last check was true, check this now. In order for something to print in the code you''ve got, every step except the last one would have to be true. Otherwise, it will never make it to the innermost if...else which contains the System.out.println calls.

I''d suggest checking out some web-based java resources to increase your general knowledge of things like this. Bruce Eckel''s Thinking in Java is a good place to start. It starts slow for those with no experience, but also has a lot of in-depth info that is useful once you get some experience. It is at this URL:

http://gothix.trans4media.com/docs/tij/Contents.html

I also suggest downloading Sun''s Java 2 API specification from java.sun.com, if you haven''t already, as it is pretty good documentation of how to use the basic library functions provided with the language.
Fabricant
Fabricant
thanks for the help. I'm new to Java and still learning. I think Java is a good base to learn C later. Thanks for the help !

Edited by - Fabricant on January 5, 2001 1:12:14 PM
-Fabricant
Fabricant
Fabricant
The sun java site is a bit confusing. I already downloaded the full java 2 1.3 SDK but I can''t find an offline documentation. There''s a 23 megs big html SDK doc but I dont think its the right file.
-Fabricant
GKW
GKW
That 23 meg''er is the sdk doc. You wouldn''t think it would be so large but there it is. Takes for ever and a day to download on a 56k when a new JDK comes out. I usually have it on my gaming machine while I work on the dev machine. What was life like before I had two machines? Very helpful.

I wanrned you! Didn''t I warn you?! That colored chalk was forged by Lucifer himself!

Opere Citato
"... we should have such an empire for liberty as she has never surveyed since the creation ..."Thomas Jefferson
Fabricant
Fabricant
thanks GKW

c_wraith :

is there an offline doc for the link you gave me (http://gothix.trans4media.com/docs/tij/Contents.html)
because I cannot be online all the time.
-Fabricant
c_wraith
c_wraith
I''m sorry... I don''t know of an offline version of that. But, it shouldn''t be too hard to just download and save the first few chapters. As I recall, it isn''t that big a book.
felonius
felonius
quote:
Original post by Fabricant

thanks GKW

c_wraith :

is there an offline doc for the link you gave me (http://gothix.trans4media.com/docs/tij/Contents.html)
because I cannot be online all the time.


Go to the official Thinking in Java homepage and you can get it all: off-line (pdk, thml, word, etc.), printed version and on-line version.

http://www.bruceeckel.com/TIJ2/index.html

Jacob Marner

Jacob Marner, M.Sc.Console Programmer, Deadline Games
Fabricant
Fabricant
Thanks !
-Fabricant
Neophyte
Neophyte
If you''re interested in the JDK-1.2.2 API documentation for offline reading I have put it out here:
http://gothix.trans4media.com/docs/jdk1.2.2-doc.tar.gz

As you can see it''s a Unix-tarball, so if you''re on a Windows-machine (or, heaven forbid, a Mac), let me know, and I''ll put a "regular" zip-version there as well.

Neophyte

- Death awaits you all with nasty, big, pointy teeth. -

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.