2017-03-28 125 views
-2

我正在嘗試製作遊戲,它非常簡單,並在代碼中進行了解釋。問題是如果我輸入一個類似於redright或redleft的命令,它將不起作用。你能發現問題嗎?調試簡單遊戲

下面的代碼:

進口java.util.Scanner的;

公共類PillarTransferEditSpaces {

String top = "red   "; 

String midup = "green   "; 

String midlow = "yellow   "; 

String low = "blue   "; 

public static void main(String args[]){ 

    Scanner s = new Scanner(System.in); 
    System.out.println("Welcome to PillarShift. Press enter to continue"); 
    String key = "anything but this"; 

    if(key != s.nextLine()){ 
    System.out.println("There are three pillars with 4 colors on them, you must move the colors from the left pillar to the right pillar"); 
    } 
    if(key != s.nextLine()){ 
     System.out.println("There are conditions."); 
     } 
    if(key != s.nextLine()){ 
     System.out.println("1.The red color can move anywhere"); 
     System.out.println("2.The green color can not move where the red color is "); 
     System.out.println("3.The yellow color can only be moved to an empty pillar or with the blue color"); 
     System.out.println("4.The blue color must move to an empty pillar"); 
     } 
    if(key != s.nextLine()){ 
     System.out.println("This is what the first move looks like if you enter 'redright'"); 
     } 
    if(key != s.nextLine()){ 
     System.out.println(" _  _   _"); 
     System.out.println("   red   "); 
     System.out.println(" |  |   |"); 
     System.out.println("green     "); 
     System.out.println(" |  |   |"); 
     System.out.println("yellow     "); 
     System.out.println(" |  |   |"); 
     System.out.println("blue     "); 
     System.out.println(" |  |   |"); 
     System.out.println(" ---  ---  ---"); 
     } 
    if(key != s.nextLine()){ 
     System.out.println("To make a move type the color and direction without a space and hit enter. ex: redright or blueleft"); 
     } 

    String top = "red     "; 

    String midup = "green     "; 

    String midlow = "yellow     "; 

    String low = "blue     "; 


    while(top != "     red" && midup != "     green" && midlow != "     yellow" && low != "     blue"){ 

     String move = s.nextLine(); 


    if (move == "redleft"){ 
     top = moveRedLeft(top, midup, midlow, low); 
    } 
    if (move == "redright"){ 

     top = moveRedRight(top, midup, midlow, low); 
    }if (move == "greenright"){ 
     midup = moveGreenRight(top, midup, midlow, low); 
    }if (move == "greenleft"){ 
     midup = moveGreenLeft(top, midup, midlow, low); 
    }if (move == "yellowleft"){ 
     midlow = moveYellowLeft(top, midup, midlow, low); 
    }if (move == "yellowright"){ 
     midlow = moveYellowRight(top, midup, midlow, low); 
    }if (move == "blueleft"){ 
     low = moveBlueLeft(top, midup, midlow, low); 
    }if (move == "blueright"){ 
     low = moveBlueRight(top, midup, midlow, low); 
    } 
    System.out.println(" _  _   _"); 
    System.out.println(top); 
    System.out.println(" |  |   |"); 
    System.out.println(midup); 
    System.out.println(" |  |   |"); 
    System.out.println(midlow); 
    System.out.println(" |  |   |"); 
    System.out.println(low); 
    System.out.println(" |  |   |"); 
    System.out.println(" ---  ---  ---"); 

    } 

    System.out.println("Congrats, you solved it. Was it hard? :P"); 
} 


    private static String moveRedRight(String top,String midup,String midlow,String low){ 

     if(top == "red     "){ 
     top = ("   red   "); 

     return top;} 

     if(top == ("   red   ")){ 
      top = "     red"; 

     return top; 
     } 
     if (top == "     red"){ 
      System.out.println("No pillar to the right, where's it supposed to go?"); 

     return top; 
     } 
     return top; 
     } 


    private static String moveRedLeft(String top,String midup,String midlow,String low){ 
     if(top == "     red"){ 
     top = ("   red   "); 
     return top; 
     } 

     if(top == ("   red   ")){ 
      top = "red     "; 
      return top; 
     } 
     if (top == "red     "){ 
      System.out.println("No pillar to the left, where's it supposed to go?"); 

     return top; 
     } 
     return top; 
     } 

    private static String moveGreenLeft(String top,String midup,String midlow,String low){ 

      if(top == "     red" && midup == "     green"){ 
      midup = ("   green   "); 
      return midup; 
      } 

      if(top == "red     " && midup == "     green"){ 
      midup = ("   green   "); 
      return midup; 
      } 
      if(top == "   red   " && midup == "     green"){ 
       System.out.println("invalid movement"); 
       return midup; 
      } 
      if (top == "     red" && midup == "   green   "){ 
       midup = "green     "; 
       return midup; 
      } 
      if (top == "red     " && midup == "   green   "){ 
       System.out.println("invalid movement"); 
       return midup; 
      } 
      if (top == "   red   " && midup == "   green   "){ 
       midup = "green     "; 
       return midup; 
      } 
     return midup; 
    } 

    private static String moveGreenRight(String top,String midup,String midlow,String low){ 

        if(top == "     red" && midup == "     green"){ 
        System.out.println("No pillar to the right, where's it supposed to go?"); 
        return midup; 
        } 

        if(top == "red     " && midup == "     green"){ 
        System.out.println("No pillar to the right, where's it supposed to go?"); 
        return midup; 
        } 
        if(top == "   red   " && midup == "     green"){ 
        System.out.println("No pillar to the right, where's it supposed to go?"); 
        return midup; 
        } 
        if (top == "     red" && midup == "   green   "){ 
         System.out.println("invalid movement"); 
         return midup; 
        } 
        if (top == "red     " && midup == "   green   "){ 
         midup = "     green"; 
         return midup; 
        } 
        if (top == "   red   " && midup == "   green   "){ 
         midup = "     green"; 
         return midup; 
        } 
        if (top == "     red" && midup == "green     "){ 
         midup = "   green   "; 
         return midup; 

        } 
        if (top == "red     " && midup == "green     "){ 
         midup = "   green   "; 
         return midup; 
        } 
        if (top == "   red   " && midup == "green     "){ 
         System.out.println("invalid movement"); 
         return midup; 
        } 
        return midup; 

} 

    private static String moveYellowRight(String top,String midup,String midlow,String low){ 

      if(midlow == "yellow     "){ 

       if(top == "   red   " || midup == "   green   "){ 
        System.out.println("invalid movement");return midlow; 
       } 
      }else{ 
       midlow = "   yellow   ";return midlow; 
      } 

      if(midlow == "   yellow   "){ 

       if(top == "     red" || midup == "     green"){ 
        System.out.println("invalid movement");return midlow; 
       } 
      }else{ 
       midlow = "     yellow";return midlow; 
      } 
      if(midlow == "     yellow"){ 
       System.out.println("No pillar to the right, where's it supposed to go?"); 
      } 
      return midlow; 
} 
    private static String moveYellowLeft(String top,String midup,String midlow,String low){ 

     if(midlow == "     yellow"){ 

      if(top == "   red   " || midup == "   green   "){ 
       System.out.println("invalid movement");return midlow; 
      } 
     }else{ 
      midlow = "   yellow   ";return midlow; 
     } 

     if(midlow == "   yellow   "){ 

      if(top == "red     " || midup == "green     "){ 
       System.out.println("invalid movement");return midlow; 
      } 
     }else{ 
      midlow = "yellow     ";return midlow; 
     } 
     if(midlow == "yellow     "){ 
      System.out.println("No pillar to the left, where's it supposed to go?"); 
     } 
     return midlow; 

} 私人靜態字符串moveBlueLeft(字符串頂部,字符串midup,字符串midlow,字符串低){

 if(low == "     blue"){ 

      if(top == "   red   " || midup == "   green   " || midlow == "   yellow   "){ 
       System.out.println("invalid movement");return low; 
      } 
     }else{ 
      low = "   blue   ";return low; 
     } 

     if(low == "   blue   "){ 

      if(top == "red     " || midup == "green     " || midlow == "yellow     "){ 
       System.out.println("invalid movement");return low; 
      } 
     }else{ 
      low = "blue     ";return low; 
     } 
     if(midlow == "blue     "){ 
      System.out.println("No pillar to the left, where's it supposed to go?"); 
      return low; 
     } 
     return low; 
     } 


    private static String moveBlueRight(String top,String midup,String midlow,String low){ 

     if(low == "blue     "){ 

      if(top == "   red   " || midup == "   green   " || midlow == "   yellow   "){ 
       System.out.println("invalid movement");return low;} 

     }else{ 
      low = "   blue   ";return low; 
     } 

     if(low == "   blue   "){ 

      if(top == "     red" || midup == "     green" || midlow == "     yellow"){ 
       System.out.println("invalid movement");return low; 
      } 
     }else{ 
      low = "     blue";return low; 
     } 
     if(low == "     blue"){ 
      System.out.println("No pillar to the right, where's it supposed to go?"); 
      return low; 
     } 
     return low; 
    } 

}

+1

嘗試在字符串比較中使用「equals」而不是==。例如:if(low.equals(「blue」){}等 –

+0

天才!謝謝! – Sirsarcastic

+0

不客氣:)。保持良好的工作 –

回答

0

使用字符串函數字符串比較中的「等於」而不是「==」。例如:if(low.equals(「blue」){}等):