0

Sorry to disturb you but i have been making a quiz app that counts the score.But when it displays my score it shows 0.

Is there something wrong with my variables?.

Is there a way to fix it?

Here is the code:

package quiz;
 import java.util.Scanner;

 public class quiz {

public static void main(String[] args) {
    //–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
    //User input
     Scanner in = new Scanner(System.in);
    //User input
    //–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
//SCORE
int score = 0;
//SCORE
//–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
//Yes or No
String yn;

     //Yes or No
     //––––––––––––––––––––––––––––––––––––––––––––––––––––
     String q1 = "Is the hottest chili on earth called the Carolina      Reaper(y/n)?";
String q2 = "Was beethoven the father of mosart(y/n)?";
String q3 = "Is the worlds best sellng album the Thriller(y/n)?";
String q4 = "Was Steve Jobs the maker of microsoft(y/n)?";
String q5 = "Did Steve jobs Die(y/n)?";
String q6 = "Was the first Iphone the Iphone 2(y/n)?";
String q7 = "Was  Rich Miner the only developer of android (y/n)?";
String q8 = "Is the Goliath  tarantula the  biggest spider on earth(y/n)?";
String q9 = "Is Octopus wolfi the biggest octopus on earth(y/n)?";
String q10 = "Adolph Blaine Charles David Earl Frederick Gerald Hubert Irvin John Kenneth Lloyd Martin Nero Oliver Paul Quincy Randolph Sherman Thomas Uncas Victor William Xerxes Yancy Wolfeschlegelsteinhausenbergerdorff Broke the record for the most confusing name(y/n)?";
   //QUESTIONS


System.out.println(q1);
yn = in.nextLine();
if (yn == "y"){
    score++;
}
System.out.println(q2);
yn = in.nextLine();
if (yn == "y"){
    score++;
}
System.out.println(q3);
yn = in.nextLine();
if (yn == "y"){
    score++;
}
System.out.println(q4);
yn = in.nextLine();
if (yn == "y"){
    score++;
}
System.out.println(q5);
yn = in.nextLine();
if (yn == "y"){
    score++;
}
System.out.println(q6);
yn = in.nextLine();
if (yn == "y"){
    score++;
} System.out.println(q7);
yn = in.nextLine();
if (yn == "y"){
    score++;
} System.out.println(q8);
yn = in.nextLine();
if (yn == "y"){
    score++;
} System.out.println(q9);
yn = in.nextLine();
if (yn == "y"){
    score++;
} System.out.println(q10);
yn = in.nextLine();
if (yn == "y"){
    score++;
}

System.out.println("Awesome, you scored "+ score +"/10");     
}

  }

Thanks!

Sorry if my code is sloppy i am a beginner.

  • 1
    Far too broad. Read [ask] and learn how to debug your code. – Amit Apr 09 '16 at 17:02
  • You can use your IDE to fix your formatting with one hot key. It will also guess you shouldn't use `==` to compare the contents of Strings. – Peter Lawrey Apr 09 '16 at 17:29

1 Answers1

0

compare String with equals() instead of == so like:

if( "y".equals(yn)) {
Dimitrios Begnis
  • 823
  • 6
  • 16