[java] Need help
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
|
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.
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:
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.
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:
|
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.
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
Edited by - Fabricant on January 5, 2001 1:12:14 PM
-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
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
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
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.
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
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.
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement