(Java)代码在两个之后停止



在我制作的一款游戏中,我有4扇门,都带有if和else if。但密码只允许进入前两扇门,我不知道为什么?

编辑:非常抱歉!我问错了问题。下面是改写后的:

i1为输入。i1==1i1==2起作用,但i1==3i1==4导致软件中断,问题是什么?

完整代码如下:

/**
 * Write a description of class GameE02 here.
 * 
 * @Aakarsh
 * @1.0 (18 October 2015)
  */
 import java.util.*;

 //System.out.print('f'); clear everything.

 public class GameE02
 {
 public static void main(String [] args) throws InterruptedException{
   Scanner reader = new Scanner(System.in);

   int i1, i2, i3, i4, i5, i6, i7, i8, i9;
   int o1, o2, o3, o4, o5, o6, o7, o8, o9;
   String s1, s2, s3, s4, s5, s6, s7, s8, s9;
   boolean loop = false;
   boolean thugloop = false; //thug hop scotch loop
   int counter = 1; //staircase counter
   int counter2 = 1; //thug hopscotch counter
   //somewhere, add in a never ending loop cycle (game over basically). Secret code required to end the game.

   System.out.println("Do you want to play a game? (y = yes, n = no)");
   s1 = reader.next();

   System.out.println("You walk into a room, there are 5 doors on the left one room on the right, you look at the room; do you wish to enter?");
   s2 = reader.next();

   //Begin the stairchase choice - Step 1 of the game.


   if(s2.equals("y")){
       Thread.sleep(1000);
       System.out.println("You have entered the room, you see a spiral staircase going up or chance to exit; you look at the staircase, do you wish to enter?");
       s3 = reader.next();
       Thread.sleep(1000);
       loop = true;
        System.out.println("You walk up the stairs, but you shockingly see yourself back in the same room, same position, climb again?");
       s3 = reader.next();  //begin the staircase loop
       while(loop==true){

       if(s3.equals("y")){
            System.out.println("You walk up the stairs, but you shockingly see yourself back in the same room, same position, climb again?");
       s3 = reader.next();
      counter++;
        }else if(s3.equals("n")){
            System.out.println("You have chosen not to climb the staircase.");
            loop=false;
            break;
        }

    }  //loop ends here.

    Thread.sleep(1000);
    System.out.println("You are frightened and you walk back and see six doors, pick one (1, 2, 3, 4, 5, 6)");
    //pun - pick one
    s5=reader.next();
    if(s5.equals("1")){
        System.out.println("Good, you understood the pun; you may still have a chance at surviving.");
        Thread.sleep(1000);

        //add in the rest of the code here.

    }else{
        System.out.println("Damn, you really suck at reading - what are you, an AP Computer Science student? I said: 'six doors.. pick ONE...");
        Thread.sleep(3950);
        System.out.println("Since you lost that, I need to put you in the maze, sorry!");
    }

    }  if(s2.equals("n")){
        counter=0;
       Thread.sleep(1000); 
        System.out.println("You see four doors, which one do you pick? (1, 2, 3, 4)");
        i1 = reader.nextInt(); //door choice number. 
        if(i1 == 1){
            Thread.sleep(500);
            System.out.println("You go inside a dungeon, and meet a skeleton there; ");
            Thread.sleep(1000);
            System.out.println("You  talk to the skeleton; Press 'y' if you want to play thug hop-scotch, or 'n' if you want to fight.");
            s4=reader.next();
            if(s4.equals("y")){
                Thread.sleep(1000);
                System.out.println("Enter some 5 (after this) from 1 to maximum-17 (inclusive) individually. Repeats are allowed.");
                i4=reader.nextInt();
                i5=reader.nextInt();
                i6=reader.nextInt();
                i7=reader.nextInt();
                i8=reader.nextInt();
                int sumhop = i4 + i5 + i6 + i7 + i8;
                System.out.println("Through a complex algorithm your sequence of numbers read " + sumhop);
                Thread.sleep(3700);
                System.out.println("Fortunately (or unforunately) the thug you were playing hopscotch against was confident so he had this program generate him a random number.");
                Thread.sleep(2500);
                int randomhop = 3 + (int)(Math.random()*84); //thug hop scotch random.
                System.out.println("His random number generated was: " + randomhop);
                Thread.sleep(1000);
                if(sumhop < randomhop){
                System.out.println("Seriously? READ CAREFULLY - When asking the question, I told you: 'Enter 5 SUM-numbers-MAXIMUM'.");
                Thread.sleep(3000);
                System.out.println("You should have done 17*5  = 85 to outbeat the thug");
                Thread.sleep(3000);
                System.out.println("Anyway, time to do battle with the thug skeleton!");
                counter2=0;

            } else if(sumhop > randomhop){
                System.out.println("Woah, you beat the thug; but now the thug is angry and he won't rest until he beats you.");
                Thread.sleep(2000);
                thugloop = true;
                  System.out.println("Pick your numbers again");
                  o4=reader.nextInt();
                o5=reader.nextInt();
                o6=reader.nextInt();
                o7=reader.nextInt();
                o8=reader.nextInt();
                int sumhop2 = o4+o5+o6+o7+o8;
                  int angrythug = 3 + (int)(Math.random()*85); 
                while(thugloop == true){

                  if(sumhop2>angrythug){
                      counter2++;
                      Thread.sleep(1000);
                      System.out.println("The thug picked: " + angrythug + " .Woah, you are really good at this!");
                      Thread.sleep(1500);
                       System.out.println("But the thug is getting angrier. Pick again!");
                         o4=reader.nextInt();
                o5=reader.nextInt();
                o6=reader.nextInt();
                o7=reader.nextInt();
                o8=reader.nextInt();
                sumhop2 = o4+o5+o6+o7+o8;
                angrythug = 3 + (int)(Math.random()*85); 
                    }else if(sumhop2<angrythug){
                        thugloop = false;
                        break;
                    }
                }
                System.out.println("Aha, the thug picked: " + angrythug + " -- you couldn't outdo the thug, could you? Well, it was a good run; time to do battle with the skeleton.");
                        Thread.sleep(3000);
                        s4="n";
            }
            } if(s4.equals("n")){
                   Thread.sleep(1000);
                   System.out.println("Now -- it is time to fight the skeleton");
                Thread.sleep(2000);
                System.out.println("Your survival depends on your mathematics skills.");
                              Thread.sleep(2700);
                                System.out.println("Type in your answers one-by-one.");
                                Thread.sleep(2500);
                                  System.out.println("By the way, Fetty Wap's song six-seven-nine is missing something.");
                                  Thread.sleep(3500);
                                System.out.println("(1) What is the highest power of 2 that divides 10! (factorial)");
                                Thread.sleep(4000);
                                System.out.println("(2) You write the numbers from 1... onwards like: 123456789(10)(11)..(643)..(707)..(984)... What is the number that starts on the 2013th digit of this sequence?" );
                                int a1 = reader.nextInt();
                                int a2 = reader.nextInt();
                                int suma = a1+a2;
                                if(suma==715){
                                    System.out.println("Wow, you're pretty damn smart; you beat the skeleton and all the odds. You might have won the game, all there is left is one question. READ CAREFULLY.");
                                    Thread.sleep(5000);
                                    System.out.print('f');
                                    System.out.println("Read carefully: Write your answers AFTER ALL QUESTIONS APPEARED;");
                                    System.out.println("I hope you remembered everything; one-by-one, in order- write how many times you:");
                                    System.out.println("(1) Walked up the stairs (including loops, everything)");
                                    System.out.println("(2) How many times you beat the thug at hopscotch (including loops etc..)");
                                    int f1, f2;
                                    f1 = reader.nextInt();
                                    f2 = reader.nextInt();
                                    int sumf = f1+f2;
                                    if(sumf==counter+counter2){
                                        System.out.println("Congratulations, you have beaten the odds and won the game. Now, do something worthwhile =)");
                                        System.exit(0);
                                    }

                                }
            }


        }   if(i1 == 2){
          Thread.sleep(500);
            System.out.println("You go inside a dungeon, and meet a skeleton there; ");
            Thread.sleep(1000);
            System.out.println("You  talk to the skeleton; Press 'y' if you want to play thug hop-scotch, or 'n' if you want to fight.");
            s4=reader.next();
            if(s4.equals("y")){
                Thread.sleep(1000);
                System.out.println("Enter some 5 (after this) from 1 to maximum-17 (inclusive) individually. Repeats are allowed.");
                i4=reader.nextInt();
                i5=reader.nextInt();
                i6=reader.nextInt();
                i7=reader.nextInt();
                i8=reader.nextInt();
                int sumhop = i4 + i5 + i6 + i7 + i8;
                System.out.println("Through a complex algorithm your sequence of numbers read " + sumhop);
                Thread.sleep(3700);
                System.out.println("Fortunately (or unforunately) the thug you were playing hopscotch against was confident so he had this program generate him a random number.");
                Thread.sleep(2500);
                int randomhop = 3 + (int)(Math.random()*84); //thug hop scotch random.
                System.out.println("His random number generated was: " + randomhop);
                Thread.sleep(1000);
                if(sumhop < randomhop){
                System.out.println("Seriously? READ CAREFULLY - When asking the question, I told you: 'Enter 5 SUM-numbers-MAXIMUM'.");
                Thread.sleep(3000);
                System.out.println("You should have done 17*5  = 85 to outbeat the thug");
                Thread.sleep(3000);
                System.out.println("Anyway, time to do battle with the thug skeleton!");
                counter2=0;

            } else if(sumhop > randomhop){
                System.out.println("Woah, you beat the thug; but now the thug is angry and he won't rest until he beats you.");
                Thread.sleep(2000);
                thugloop = true;
                  System.out.println("Pick your numbers again");
                  o4=reader.nextInt();
                o5=reader.nextInt();
                o6=reader.nextInt();
                o7=reader.nextInt();
                o8=reader.nextInt();
                int sumhop2 = o4+o5+o6+o7+o8;
                  int angrythug = 3 + (int)(Math.random()*85); 
                while(thugloop == true){

                  if(sumhop2>angrythug){
                      counter2++;
                      Thread.sleep(1000);
                      System.out.println("The thug picked: " + angrythug + " .Woah, you are really good at this!");
                      Thread.sleep(1500);
                       System.out.println("But the thug is getting angrier. Pick again!");
                         o4=reader.nextInt();
                o5=reader.nextInt();
                o6=reader.nextInt();
                o7=reader.nextInt();
                o8=reader.nextInt();
                sumhop2 = o4+o5+o6+o7+o8;
                angrythug = 3 + (int)(Math.random()*85); 
                    }else if(sumhop2<angrythug){
                        thugloop = false;
                        break;
                    }
                }
                System.out.println("Aha, the thug picked: " + angrythug + " -- you couldn't outdo the thug, could you? Well, it was a good run; time to do battle with the skeleton.");
                        Thread.sleep(3000);
                        s4="n";
            }
            } if(s4.equals("n")){
                   Thread.sleep(1000);
                   System.out.println("Now -- it is time to fight the skeleton");
                Thread.sleep(2000);
                System.out.println("Your survival depends on your mathematics skills.");
                              Thread.sleep(2700);
                                System.out.println("Type in your answers one-by-one.");
                                Thread.sleep(2500);
                                  System.out.println("By the way, Fetty Wap's song six-seven-nine is missing something.");
                                  Thread.sleep(3500);
                                System.out.println("(1) What is the highest power of 2 that divides 10! (factorial)");
                                Thread.sleep(4000);
                                System.out.println("(2) You write the numbers from 1... onwards like: 123456789(10)(11)..(643)..(707)..(984)... What is the number that starts on the 2013th digit of this sequence?" );
                                int a1 = reader.nextInt();
                                int a2 = reader.nextInt();
                                int suma = a1+a2;
                                if(suma==715){
                                    System.out.println("Wow, you're pretty damn smart; you beat the skeleton and all the odds. You might have won the game, all there is left is one question. READ CAREFULLY.");
                                    Thread.sleep(5000);
                                    System.out.print('f');
                                    System.out.println("Read carefully: Write your answers AFTER ALL QUESTIONS APPEARED;");
                                    System.out.println("I hope you remembered everything; one-by-one, in order- write how many times you:");
                                    System.out.println("(1) Walked up the stairs (including loops, everything)");
                                    System.out.println("(2) How many times you beat the thug at hopscotch (including loops etc..)");
                                    int f1, f2;
                                    f1 = reader.nextInt();
                                    f2 = reader.nextInt();
                                    int sumf = f1+f2;
                                    if(sumf==counter+counter2){
                                        System.out.println("Congratulations, you have beaten the odds and won the game. Now, do something worthwhile =)");
                                        System.exit(0);
                                    }

                                }
        }  if(i1 == 3){
            Thread.sleep(1);
            System.out.println("Oooooooooh... you picked the maze - good luck");
            Thread.sleep(2000);
            System.out.println("You stepped on a trap and fall in a dungeon. One way you see the light, the other, darkness. Press 'y' for light-way and 'd' for dark-way.");
            String dun = reader.next();
            if(dun.equals("y")){
                System.out.println("Good you saw the light, now you see a white board with a math question. Press 'y' to proceed or 'n' to go to the dark side.");
                String pro = reader.next();
                if(pro.equals("y")){
                    System.out.println("You have chosen to answer the math question.... smart choice.");
                    Thread.sleep(2000);
                    System.out.println("what is 2^2?");
                    int ans = reader.nextInt();
                   Thread.sleep(7700);
                   System.out.println("You really thought it would be that easy?");
                   Thread.sleep(1000);
                   System.out.println("You are given a 12th degree polynomial is the form: P(x) = 23x^(12) + ax^(11) +  ... + 8");
                   Thread.sleep(1500);
                   System.out.println("The roots are respectively: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12");
                   Thread.sleep(3000);
                   System.out.println("What is the absolute value of the coefficient 'a' in the poylnomial P(x)?");
                   int poly = reader.nextInt();
                   if(poly==161){
                       System.out.println("You have done what many others never could. Now do something worthwhile!");
                        System.exit(0);
                    }
                } else if(pro.equals("n")){
                    dun="n";
                }
            } if(dun.equals("n")){
                System.out.println("Okay, you chose the dark side - what is the first name of the 4th US President? (No caps)");
                String res = reader.next();
                if(res.equals("james")){
                    System.out.println("Nice. Suddenly, an earthquake appears and you fall down the dungeon.");
                    Thread.sleep(1500);
                    System.out.println("You are stuck with a skeleton who wants to play thug hopscotch really badly; you must play - get ready!");
                    Thread.sleep(4300);

            }

        }  if(i1 == 4){
            System.out.println("Huh - smart guy, you won the game.");
            System.exit(0);
        }
    }


     }
    }
     }
     }

有完整的代码;请帮帮我!

你的大括号"错误排列"。您在错误的位置关闭了if (i1 == 2)块,它封装了对3和4的检查。您的IDE应该能够突出显示匹配的大括号,以帮助您看到块的结束位置。

如果你学过switch-case结构,你可以使用它,它通常使这类工作更容易。

相关内容

最新更新