Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Quiz program with if and else

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Quiz program with if and else

    I'm sure there's an easier way to do this, but I was trying to make a very primitive java program that would move on if the answer was correct and would terminate if it wasn't. It works and gets to the end if the user inputs the answers correctly, but it also does that even if he doesn't answer correctly(besides the first question, which takes two inputs to proceed for some reason). It's in my native language but you should get the gist of it.

    package summaries;
     
    import java.util.Scanner;
     
    public class Akvile {
     
    	public static void main(String[] args) {
     
    		int answer=3, answer2=62 ,answer3=1964;
     
    		Scanner print = new Scanner(System.in);
     
    		System.out.println("Labas.");
    		System.out.println("Kiek bus 2+2/2 ?");
     
    		 int ans1 = print.nextInt();
     
    		if (ans1 == answer) {
    			System.out.println("Teisingai. Sudek Roko ir Akviles metus ir padvigubink. Kiek bus?"); } 
     
    		   int ans2=print.nextInt();
     
    		   if (ans2 == answer2 ){
    			    System.out.println("Teisingai! Liko paskutinis klausimas:"); }
     
    			  System.out.println("Kelintais metais yra gimusi mama?");
     
     
    		      int ans3 = print.nextInt();
     
    		      if (answer3 == 1964){
    		    	  System.out.println("Deja...Bet atsakei tesingai!!! Laimejai zaidima. Stai tavo prizas:");
    		    	  System.out.println(":)"); }
     
     
    		      else{
    		  		System.exit(1); }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Quiz program with if and else

    Check which variables are being used. Their uses appear inconsistent.

    The names of the variables should say what is in the variable.
    What is in answer?
    what is in ans1?

    Should the names be something like correctAnswer and userAnswer?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,473
    My Mood
    Mellow
    Thanks
    87
    Thanked 483 Times in 474 Posts

    Default Re: Quiz program with if and else

    Do you know arrays yet? Multiple variables named, this#, are very painful to deal with, and it's difficult to write methods that can take and manipulate collections of similar things that have unique names, like question#, answer#, etc. Learn arrays or ArrayLists before tackling this project.

Similar Threads

  1. Java Quiz help
    By kingsta in forum Java Theory & Questions
    Replies: 2
    Last Post: October 13th, 2013, 10:18 PM
  2. [SOLVED] Logo quiz
    By big daan1234 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 15th, 2012, 09:57 AM
  3. [SOLVED] Addition Quiz
    By javaneedhelp in forum Loops & Control Statements
    Replies: 5
    Last Post: October 9th, 2011, 12:55 AM
  4. Quiz application
    By JonoF in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: May 10th, 2010, 06:06 AM
  5. math quiz program
    By hope.knykcah in forum Collections and Generics
    Replies: 1
    Last Post: October 23rd, 2009, 09:53 AM