Results 1 to 3 of 3

Thread: Loop help

  1. #1
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Loop help

    hey so I am messing around trying to make a text based game to fill my boredom but cannot seem to get past the first loop in my sequence. What am I doing wrong here? Wrong loop maybe? possibly a switch and case statement? I am done messing with it for today so any suggestions would be appreciated so I can rewrite it when I get home from work tomorrow.

    Java Code:
    package com.blue2525989;
    
    import java.math.*;
    import java.util.*;
    
    public class MessingAround2 {
        public MessingAround2 () {
            
        }
        
        public static void main(String [] args) {
            String yes = "yes";
            String no = "no";
            String blue = "blue";
            String green = "green";
            int b = 2015, c = 2016;
            String java = "java";
            String cpp = "c++";
            String exit = "exit";
            
            Scanner scan = new Scanner(System.in);
            System.out.println("-------Welcome to my domain-------");
            System.out.println("Do you want to play a game?");
           
                String a = scan.nextLine();
                if (a == yes) {
                    System.out.println("Good, Here is your first question.");
                    System.out.println("Is the sky blue or green?");
                    String aa = scan.nextLine();
                    if (aa == blue) {
                        System.out.println("Good job, You are smart.");
                        System.out.println("Next Question, are you ready?");
                        System.out.println("Is the year 2015 or 2016?");
                        int ab = scan.nextInt();
                        if (ab == b) {
                            System.out.println("Wrong answer.");
                        } if ( ab == c) {
                            System.out.println("Good job.");
                            System.out.println("Next question, Is this program written in Java"
                                    + "or c++");
                            String ac = scan.nextLine();
                            if (ac == cpp) {
                                System.out.println("Wrong answer.");
                            } if (ac == java) {
                                System.out.println("Excellent! You seem to be pretty smart.");
                                System.out.println("Enter exit to exit program. Good-bye");
                                String ad = scan.nextLine();
                                if (ad == exit) {
                                    System.exit(0);
                                }
                            }
                            
                        }
                    } if (aa == green) {
                    System.out.println("Wrong answer.");
                }
                }else if (a == no) {
                    System.exit(0);
                }
        }
    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Loop help

    Compare String using equals() and not ==.
    Java Code:
    if ( a.equals("yes")) {
    
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: Loop help

    Thankyou I will try that in about 12 hours when i get home from work.

Similar Threads

  1. Simple loop within a loop not working.
    By supremegrandruler in forum New To Java
    Replies: 3
    Last Post: 03-21-2014, 09:27 AM
  2. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  3. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  4. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  5. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 04:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •