• Post Reply Bookmark Topic Watch Topic
  • New Topic

for loop inside an if statement  RSS feed

 
Grzegorz Skawinski
Greenhorn
Posts: 7
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi Guys,

As you probably noticed, I'm a beginner with Java programming and don't have a spread knowledge with coding.
I have set up an aim to myself which is to program a simple quizz game. But my problem is that I want to add "Scores" for example, after the user gets the answer correct he gets +10 Points and if he gets the incorrect answer he gets -10 Points.
To do this I think I need an if statement and for loop. So my question is, is it possible to join these two to get the output ?
If yes, can someone give me any example or a direction how this can be implemented.

This is my code so far :

 
Bob Holness
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you're asking about the loop but here's a bit more to help you on




You could indeed use a loop then have some sort of question/answer data source. Here's an idea of what it might look like...
 
Shubhendu Pramanik
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all I would like to suggest don't compare String like this "if (question == "Juneau" || question == "juneau"){"

Always compare string like below:

question.equals("Juneau"); // for details you can read  difference between "==" and equals method

Secondly your question is not clear. Do you want to associate score for each user ?? For that you should use object of say "Participant" and add member variables as name, score etc and add score using setter method when he gives correct answer.
 
chamini prashakthi
Ranch Hand
Posts: 127
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here there is an simple problem
you have to use instead of
as an example:



and do the same in question part in the code...


String comparison is a common scenario of using both == and .equals method. Since java.lang.String class override equals method, It return true if two String object contains same content but == will only return true if two references are pointing to the same object.
 
Grzegorz Skawinski
Greenhorn
Posts: 7
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just didn't know that I can use :



Inside an if statement. You made it alot clearer for me, thanks.
Can you explain when to use ".equals" and "==" as I've seen ".equals" before but don't really know when to use it.
 
chamini prashakthi
Ranch Hand
Posts: 127
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java’s String class overrides the equals() method to compare the characters in a string. This means that the comparison between the 2 String objects returns true since they both hold the string. It should now be clear what the difference is between the equals() method and the “==” operator.
 
chamini prashakthi
Ranch Hand
Posts: 127
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use for loop inside the statement.Your expression is correct to add 10 by 10 for each answer...
 
Bob Holness
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using "==" is just comparing the reference. ".equals" actually compares the contents.

The reason why "==" works, in most cases, is due to how strings are stored in the JVM. Strings are pooled to save memory so different instances can actually refer to the same reference internally. You can try running this code...

 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Holness and  chamini prashakthi () welcome to the Ranch

As it says on the SO link posted, you have to override equals() correctly to get it to work; String has a correctly‑overridden equals method, so you can use it with complete confidence. If you click that link, you will find that String also has this, so you can write:-
if (text.equalsIgnoreCase("cAmPBeLL"))...

Minor hazard: what if text might be null and you might suffer a NullPointerException? There are two ways you can handle that:-
  • 1: if (text != null && text.equalsIgnoreCase("cAmPBeLL"))...
  • 2: if ("cAmPBeLL".equalsIgnoreCase(text))...
  • In both cases null is considered not equal. You must use && not & in the first example. Most people consider No 2 better. Most people would also use a more conventional type of caPiTAliSATion.
     
    Knute Snortum
    Sheriff
    Posts: 4279
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bob Holness, thank you for your contributions.  I would like to make you aware that we try not to post complete solutions in the Beginning Java forum (snippets are fine). 
     
    Knute Snortum
    Sheriff
    Posts: 4279
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bob Holness, other moderators felt that what you posted was within the bounds of the Beginning Java charter, so take my post with a grain of salt.  But do keep in mind the "no complete solutions" guideline in the future.
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12563
    49
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    When talking about objects, == checks to see if the variable refer to the same object. It's like comparing two notecards to see if they have the same street address written on them.  If both have "1060 West Addison, Chicago IL", then you know they refer to the same thing.

    However, it's possible that they refer to functionally equivilent things...  So one may have "1600 Pennsylvania Avenue, Washington D.C", and the other may have "10 Downing Stree, London England", but if what you care about is where the leader of a country lives, those two are essentially equal.  A better example may be if the same blueprints are used to build the same house on two different plots of land. They have different addresses, but are otherwise identical - depending on what your definition of "identical" is - and equals lets the person creating the class define what "identical" means for that object type.
     
    Bob Holness
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Knute, no worries.

    I had hoped I wasn't being too complete Was just trying to guide OP down the correct OO path and steer clear of the dreaded procedural one
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!