Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


[java] Need help


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 Fabricant   Members   -  Reputation: 122

Like
Likes
Like

Posted 05 January 2001 - 01:06 AM

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!"); } } }

Sponsor:

#2 Nytegard   Members   -  Reputation: 823

Like
Likes
Like

Posted 05 January 2001 - 03:07 AM

  
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.

#3 Fabricant   Members   -  Reputation: 122

Like
Likes
Like

Posted 05 January 2001 - 03:26 AM

why ?

> int x=2

so x == 2 must be right !?

#4 c_wraith   Members   -  Reputation: 122

Like
Likes
Like

Posted 05 January 2001 - 03:30 AM

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.

#5 Fabricant   Members   -  Reputation: 122

Like
Likes
Like

Posted 05 January 2001 - 06:09 AM

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

#6 Fabricant   Members   -  Reputation: 122

Like
Likes
Like

Posted 05 January 2001 - 07:14 AM

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.

#7 GKW   Members   -  Reputation: 200

Like
Likes
Like

Posted 05 January 2001 - 07:22 AM

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

#8 Fabricant   Members   -  Reputation: 122

Like
Likes
Like

Posted 05 January 2001 - 07:31 AM

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.

#9 c_wraith   Members   -  Reputation: 122

Like
Likes
Like

Posted 05 January 2001 - 08:43 AM

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.

#10 felonius   Members   -  Reputation: 122

Like
Likes
Like

Posted 06 January 2001 - 12:43 AM

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



#11 Fabricant   Members   -  Reputation: 122

Like
Likes
Like

Posted 06 January 2001 - 05:33 AM

Thanks !

#12 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 06 January 2001 - 07:02 AM

"I think Java is a good base to learn C later."

I _don''t_ think Java is a good base for learning C. They are different. While they look alike on the surface, they can behave quite differently.

C is meant to give you a lot of power (access to the machine).

Java is meant to be safe and portable.


#13 Neophyte   Members   -  Reputation: 544

Like
Likes
Like

Posted 07 January 2001 - 05:26 AM

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. -




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS