2016-02-12 75 views
0

所以我創建了一個簡單的岩石剪刀剪刀,我最近犯了一個錯誤。我剛開始學習,我犯了一個錯誤,並失去了在哪裏。我真的很感謝任何我犯過錯的地方。當它打印出提示時,我輸入我想要的,然後再打印出來。謝謝!在Java中的循環

import java.util.Scanner; 
import java.util.Random; 
import java.io.*; 
public class rock_Paper_Scissor { 
    public static void main (String[] args) { 
     String playerhand; 
     boolean x = true; 
     Scanner input = new Scanner(System.in); 
     Random num = new Random(); 
     int rand = num.nextInt(2) + 1; 
     System.out.println("I challenge you to Rock Paper Scissor"); 
     System.out.println("If you want to quit, type exit twice"); 
     System.out.println("Type Rock, Paper, or scissor"); 
     playerhand = input.nextLine(); 
     String hands = playerhand.toLowerCase(); 
     while (x == true) { 
      if (hands == "rock") {    
       if (rand == 1) { 
        System.out.println("Rock vs. Rock: TIE"); 
       } else if (rand == 2) { 
        System.out.println("Rock vs. Scissor: YOU WIN"); 
       } else if (rand == 3) { 
        System.out.println("Rock vs. Paper: YOU LOSE"); 
       } 
      } 
      else if (hands == "paper") { 
       if (rand == 1) { 
        System.out.println("Paper vs. Rock: YOU WIN"); 
       } else if (rand == 2) { 
        System.out.println("Paper vs. Scissor: YOU LOSE"); 
       } else if (rand == 3) { 
        System.out.println("Paper vs. Paper: TIE"); 
       } 
      } 
      else if (hands == "scissor") { 
       if (rand == 1) { 
        System.out.println("Scissor vs. Rock: YOU LOSE"); 
       } else if (rand == 2) { 
        System.out.println("Scissor vs. Scissor: TIE"); 
       } else if (rand == 3) { 
        System.out.println("Scissor vs. Paper: YOU WIN"); 
       } 
      } 
      else if (hands == "exit") { 
       System.out.println("Thank you for playing!"); 
       x = false; 
      } 
      System.out.println("Please type your hand to play again: "); 
      hands = input.nextLine(); 
     } 
    } 
} 
+0

學習如何調試自己的代碼的時間。希望您正在使用具有分步調試功能的體面IDE。 –

回答

1

在你所有的條件,如果嘗試使用eqauls()方法而不是==這樣的:

if ("rock".equals(hands)) { 
    ... 
    else if ("paper".equals(hands)) { 
    ...  
    else if ("scissor".equals(hands)) { 
     ... 
    else if ("exit".equals(hands)) { 

.equals()用來比較字符串值。 但==與引用字符串進行比較。

+0

可能是..它的答案很糟糕,沒有給出任何正確的解釋或文件.. – Satya