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 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2013
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Angry problem with a code.

    I'm attempting to to make a "chose your way" type of game, but I have 2 problems. At the beginning you get a choice of 3 options, and from one of those beginning choices, you get another option. And the way I wrote it, java can't tell which option you pick the beginning or the one after. Please help. Anyway here's the code:

    PHP Code:
    import java.util.Scanner;
    public class 
    test4 
    {
        public static 
    void main(String[] args
        {
                
    Scanner in =new Scanner(System.in);
                
    int a;
                
    int b;
                
    int c;
                
    int d;
                
    int A;
                
    int B;
                
    int C;
                
    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.");
                
    a=in.nextInt();
                    if (
    == 1)
                        
    System.out.println("you start to run toward the mountian, and to climb to it's height but you hit a fork in the road.");
                        
    System.out.println("path 1 is to go to the high ground, and path 2 is to start going down mountianpick your way...");
                            
    A=in.nextInt();
                                if ( 
    A== 1)
                                    
    System.out.println("You keep walking up the path, heighier and heighier.  But then you slip and fall, all the way down the mountian to your death.  Please try agian");
                                        if (
    == 2);
                                            
    System.out.println("you follow the path, going down the mountian, and at the bottom of the mountian you run into the troll, who kiddnapped the princess.");
                    
                    else if (
    == 2)
                        
    System.out.println("You start to run through the vally, running through tall grass, and tree.  But then a river forms, and path splits and now theres two way to go around.");
                        
    System.out.println("Path 1 is to the left, and path 2 is to the right.  Pick your way...");
                            
    B=in.nextInt();
                                if (
    == 1)
                                    
    System.out.println("As you run through the marsh. you get bit by a water snake and die. Please try agian");
                                        if (
    == 2)
                                            
    System.out.println("You run through the marsh and at the end of the path, you run into the troll that kiddnapped the princess.");
                                
                else
                    
    System.out.println("You start to run toward the forest, in the forest a dead tree has fallen over and cleared another path.");
                    
    System.out.println("Path 1 is the freshly made path from the tree, or you can keep following path 2");
                        
    C=in.nextInt();
                            if (
    == 1)
                                
    System.out.println("As you run through the new path it leads you directly into a cave.  As you enter that cave you get attacked by a pack of wolfs and die.  Please try agian");
                                    if (
    == 2)
                                        
    System.out.println("You follow the path till your out of the forest and waiting there is the troll that kiddnapped the princess.");
                                        
                    
                    
        
        }


    Last edited by VIPStephan; 01-07-2014 at 09:40 PM. Reason: added code BB tags

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum.

    BTW, when posting here please help us to help you by making it easier to view, copy, modify, test and debug your scripts by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,642
    Thanks
    6
    Thanked 1,005 Times in 978 Posts
    Moved accordingly.

  • #4
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,728
    Thanks
    41
    Thanked 191 Times in 190 Posts
    your first choice is 'a' (the one that is not getting 'remembered' ... I see no reason why it would not retain the value of 'a'... but I also do not see you calling 'a' anywhere- how do you know it is not retaining the value?

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

  • #5
    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
    There's no need for most of these variables. All you need to know is the first and second chosen values since you only go two steps in depth.
    For this reason, you don't even need the two variables. Instead, simply use a switch:
    PHP Code:
    switch (in.nextInt())
    {
        case 
    1:
            
    System.out.println("Some text here");
            switch (
    in.nextInt())
            {
                case 
    1:
                    
    System.out.println("Result of 1, 1");
                    break;
                case 
    2:
                    
    System.out.println("Result of 1, 2");
                    break;
            }
            break;
        case 
    2//. . .

    Of course, this handles nothing for errors, but I'd ignore that for now.
    I'd suggest splitting some separate methods out for these.

    As your skills improve in the language, even with a cli interface using collections of collections you'll be able to embed infinite depth.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    perrinormal (01-08-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
    •