2014-10-04 3 views
0

나는 간단한 질문을 했으므로 간단한 프로그램 인 Rock, Paper Scissors 게임을 작성했지만 지금은 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

자바 스크립트? – ndugger

답변

0

한 가지 방법은 단순히 손의 이름을 저장하는 배열을 사용하는 것입니다 감사합니다 :

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

인쇄 할 때 다음을 수행하십시오

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

와우 완벽하게 작동합니다. :) 이제 내 프로그램이 마침내 완료되었습니다. btw는 문자열에 값을 저장할 때 프로그램이 단어 수, 종이, 가위로 표시되는 것을 어떻게 알 수 있습니까? – ChrisV

+0

그 '손'배열에 나타난 단어의 순서입니다. 예를 들어, 0을 바위처럼, 1을 가위로, 2를 종이로하고, 그 순서대로 놓으려면 조정할 수 있습니다. –

관련 문제