2016-11-11 76 views
-4

我創建了這個程序來運行無限次,要求用戶輸入一個選項(岩石或紙張或剪刀),這似乎工作正常。問題不是別人....如果或如果.... else語句得到滿足。無論我輸入什麼輸入,都會在else語句中輸出else語句(輸入一個有效的選項)。我無法找到我犯這樣的錯誤鏈接我的代碼下面.....在此先感謝。調試我的簡單java遊戲(Rock Paper Scissor)

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

public class apples { 
    public static void main(String args[]){ 

     Scanner input = new Scanner(System.in); 
     Random number = new Random(); 

     String rps[] = {"rock", "paper", "scissor"}; 
     String player; 
     String ai; 
     int rand, pscore=0, aiscore=0; 

     while(1 < 2){ 
     System.out.println("Take your pick \nrock \npaper \nscissor"); 
     player = input.nextLine(); 

     rand = number.nextInt(3); 
     ai = rps[rand]; 

     System.out.println(ai); 

     if(player == ai){ 
      pscore+=0; 
      aiscore+=0; 
      System.out.println("Your opponent chose " + ai + "\tYour score= "+ pscore + "\tOpponents score = "+aiscore); 
      System.out.print("\n"); 
      continue; 
     }else{ 
      if(player == "rock" && ai == "paper"){ 
       aiscore+=1; 
       pscore+=0; 
       System.out.println("Your opponent chose " + ai + "\tYour score= "+ pscore + "\tOpponents score = "+aiscore); 
       System.out.print("\n"); 
       continue; 
      }else if(player == "rock" && ai == "scissor"){ 
       pscore+=1; 
       aiscore+=0; 
       System.out.println("Your opponent chose " + ai + "\tYour score= "+ pscore + "\tOpponents score = "+aiscore); 
       System.out.print("\n"); 
       continue; 
      }else if(player == "paper" && ai == "rock"){ 
       pscore+=1; 
       aiscore+=0; 
       System.out.println("Your opponent chose " + ai + "\tYour score= "+ pscore + "\tOpponents score = "+aiscore); 
       System.out.print("\n"); 
       continue; 
      }else if(player == "paper" && ai == "scissor"){ 
       aiscore+=1; 
       pscore+=0; 
       System.out.println("Your opponent chose " + ai + "\tYour score= "+ pscore + "\tOpponents score = "+aiscore); 
       System.out.print("\n"); 
       continue; 
      }else if(player == "scissor" && ai == "rock"){ 
       aiscore+=1; 
       pscore+=0; 
       System.out.println("Your opponent chose " + ai + "\tYour score= "+ pscore + "\tOpponents score = "+aiscore); 
       System.out.print("\n"); 
       continue; 
      }else if(player == "scissor" && ai == "paper"){ 
       aiscore+=0; 
       pscore+=1; 
       System.out.println("Your opponent chose " + ai + "\tYour score= "+ pscore + "\tOpponents score = "+aiscore); 
       System.out.print("\n"); 
       continue; 
      }else{ 
       System.out.println("Enter a valid choice"); 
       System.out.print("\n"); 
       continue; 
      } 
     } 
     } 


    } 
} 
+1

你最好在這裏發佈你的代碼。它會一直在這裏,如果你這樣做,而鏈接的內容可能不會! – LiXie

+0

請考慮首先看這個:http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Tim

+0

謝謝你們。順便說一句,我不知道如何尋找我面臨的問題。無論如何,我更新了pastebin鏈接中的代碼,並且工作得很好。 –

回答

0

相反的:

if(player == "rock" && ai == "paper"){ 
    ... 
} 

使用

if(player.equals("rock") && ai.equals("paper")){ 
    ... 
} 

這將適用於所有的代碼段,其比較字符串的。

0

在java中,==測試引用相等:如果兩個對象實際上是相同的對象。兩個持有相同字符的字符串將失敗此檢查,這是您的邏輯中發生的事情。

How do I compare strings in Java?