2014-10-04 91 views
0

我有一個快速的問題,所以我完成了我的程序,這是一個簡單的岩石,剪刀遊戲,但目前它只顯示0,1,2其中0是剪刀,1是岩石,2是紙。我所面臨的問題是,我不太清楚如何給用戶提供輸入,以及計算機輸出從顯示數字到顯示剪刀,搖滾,紙張等。將數字輸出和輸入轉換爲字

import java.util.Scanner; 
import java.lang.Math; 

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

     Scanner in = new Scanner(System.in); 

     System.out.print("Hello user! lets play "); 
     System.out.println("Rock, Paper, Scissors."); 
     System.out.println(); 
     System.out.print("Type in 0 for Scissors, 1 for Rock, or 2 for Paper "); 
     int userInput = in.nextInt(); 

     int opponentsHand = (int)(Math.random()*3); 




     if (userInput == opponentsHand) 
     { 

     System.out.print("Darth Vader has played " + opponentsHand); 
     System.out.println(" Despite your efforts of playing " + userInput + ", this battle has  ended in a draw!"); 
    } 

    if (userInput < opponentsHand && opponentsHand != 2) 
    { 
     System.out.print("Darth vader has played "+ opponentsHand); 
     System.out.println(", You played " + userInput + "You have Lost"); 
    } 

    else if (userInput < opponentsHand && opponentsHand == 2) 
    { 
     System.out.print("Darth Vader has played " + opponentsHand); 
     System.out.println(" You played " + userInput + " You have won"); 
    } 

    if (userInput > opponentsHand && opponentsHand != 0) 
    { 
     System.out.print("Darth Vader has played " + opponentsHand); 
     System.out.println(" You have played " + userInput + " You have won"); 
    } 

    else if (userInput > opponentsHand && opponentsHand == 0) 
    { 
     System.out.print("Darth Vader has played " + opponentsHand); 
     System.out.println(" You have played " + userInput + " You have lost"); 
    } 




} 

}

謝謝

+0

這對d有什麼影響? o與JavaScript? – ndugger 2014-10-04 04:19:01

回答

0

一種方法就是使用一個數組來存儲手的名字:

String[] hands = {"scissors", "rock", "paper"}; 

打印時,請執行以下操作:

System.out.print("Darth Vader has played " + hands[opponentsHand]); 
+0

哇,它完美的作品非常感謝你! :)現在我的程序終於完成了再次btw只是好奇知道什麼時候我將值存儲到字符串中如何知道哪個數字是由Words Rock,Paper,Scissor表示的? – ChrisV 2014-10-04 04:39:10

+0

這就是出現在'手'數組中的單詞序列。顯然你可以調整,例如,如果你想要0搖滾,1剪刀和2紙,然後把它們按順序。 – 2014-10-04 04:48:31

相關問題