Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    A problem in my code.

    In the code, after the beginning sequence there is a huge case statement around the whole code, and java can't read the 2nd or 3rd case statement but can read the first. I have no idea why it's not working, anything will help.

    Code:
    Attached Files Attached Files

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Run that through a code formatter and repost it. Its too difficult to read without proper indentation.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,740
    Thanks
    41
    Thanked 191 Times in 190 Posts
    I cleaned it up some, removed a lot of the text because it is not needed to see why the code isn't working... As Fou-Lu said, clean code makes for more readability and troubleshooting, the answer becomes evident when you clean it all up. The first switch only handles 1... you never account for 2 or 3 in the first switch (ie as of now it is not even needed unless you want to validate they pressed 1)... See comments in red

    package game;


    import java.util.Random;
    import java.util.Scanner;


    public class game
    {
    private static Scanner input;
    private static Scanner kb;
    public static void main(String[] args)
    {
    kb = new Scanner(System.in)
    input = new Scanner(System.in);

    int one;
    System.out.println("you are a knight of the kingdom of air, and your on a mission to save the princess, and deliver her to one of the three kingdoms.");
    System.out.println("the kingdom of ice, the kingdom of fire, or the kingdom of earth.");
    System.out.println("to get the princess, there are three routes to take from the kingdom to her.");
    System.out.println("path 1, is to go up through the mountian, path 2, is to go through the vally, and path 3 is to go through the forest");
    System.out.println("So... which way? only use the number of your pick.");
    one = (kb.nextByte()); // right here- this will only ever work if they pick 1... numbers 2 & 3 are never evaluated
    switch (one)
    {
    case 1:
    System.out.println
    System.out.println
    switch (kb.nextInt())
    {
    case 4:
    System.out.println
    break;
    case 5:
    System.out.println
    System.out.println
    String a = input.nextLine();
    String j = "no";
    if (a.equalsIgnoreCase(j))
    System.out.println // no need for brac {} you are using a one line statement for your if
    else
    System.out.println // same here no need for braces {}

    int random;
    Random number = new Random();
    for(int counter=1; counter<=1{
    random = number.nextInt(20);

    String[] question = new String[20];
    **************************** here- this entire section can be moved to happen before the initial switch statement **********************
    question[0]=
    .............
    question[19]=

    String[] answer = new String[20];

    answer[0]=
    ............
    answer[19]=
    **************************************************************************************************** ********************
    String b = (answer[random]);

    System.out.println(question[random]);
    String x = input.nextLine();


    if(x.equalsIgnoreCase(b)){
    System.out.println("congrats! you aswered the riddle correctly.");
    break;
    }
    else{
    System.out.println
    break;
    }
    } // end for loop
    break;

    case 2:
    System.out.println
    System.out.println
    switch (kb.nextInt())
    {
    case 5:
    System.out.println
    break;
    case 4:
    System.out.println
    System.out.println
    System.out.println
    System.out.println
    System.out.println
    System.out.println
    Scanner in =new Scanner(System.in);
    String c = in.nextLine();
    String d = "no";
    if (c.equalsIgnoreCase(d))
    System.out.println // again no need for {}
    else
    System.out.println // or here


    int random1;
    Random numberone = new Random();
    for(int counter=1; counter<=1{
    random1 = numberone.nextInt(20);

    // I didn't move it out above because I didn't realize you were going to do it again twice, but move the string[] for question
    // and answer to before you ever drop into the switch... rule of thumb for coding is if you find your self typing it more than
    // twice it can be done more efficiently


    String b = (answer[random1]);
    System.out.println(question[random1]);
    String y = in.nextLine();

    if(y.equalsIgnoreCase(b)){
    System.out.println("congrats! you aswered the riddle correctly.");
    break;
    }

    else{
    System.out.println("sorry, you did not answer the riddle correctly. Please Try agian.");
    break;
    }

    // worth noting in your previous if/else statements similar to this one, you did not have breaks
    // did you mean to add them?

    }
    break;
    case 3:
    // removed the code here because it's not relevant to figuring out the problem
    // also worth noting is that you do not have a break statement for case 3


    }
    }
    }
    }
    }

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • Users who have thanked alykins for this post:

    perrinormal (01-20-2014)


  •  

    Tags for this Thread

    Posting Permissions

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