2015-09-17 33 views
-3

我必須在java中製作一個Rock,Paper,Scissors遊戲。這是我的代碼:任何人都可以幫我用我的岩石,紙,剪刀遊戲在Java?

package examplepackage; 
// Write a program that will play Rock, Paper, Scissors with the user. 
// The program should take an input of either rock, paper, or scissors 
// and then the computer should generate a random result of rock, paper, 
// or scissors and compare these outputs and say who won; this should 
// then be displayed to the screen. Don't worry about making your program  loop. 
// just use the run button to restart it. 
import java.util.Scanner; 
public class RockPaperScissors { 

    public static void main(String[] args) { 
    double cpum = Math.random(); 
    Scanner a = new Scanner(System.in); 
    String x; 
    System.out.println("Enter Rock, Paper, or scissors: "); 
    x = a.nextLine(); 
    System.out.println("You chose " + x + "."); 
    if((cpum >= 0.0 && cpum <= .34) && x == "Rock") 
    { 
    System.out.println("The game is a tie!"); 
    } 
    else if((cpum >= 0.0 && cpum < .34) && x == "Paper") 
    { 
    System.out.println("You win! :D"); 
    } 
    else if((cpum >= 0.0 && cpum < .34) && x == "Scissors") 
    { 
    System.out.println("The Computer Wins! :("); 
    } 
    else if((cpum > 0.34 && cpum < 0.67) && x == "Rock") 
    { 
    System.out.println("The game is a tie!"); 
    } 
    else if((cpum > 0.34 && cpum < 0.67) && x == "Paper") 
    { 
    System.out.println("The Computer Wins! :("); 
    } 
    else if((cpum > 0.34 && cpum < 0.67) && x == "Scissors") 
    { 
    System.out.println("You win! :D"); 
    } 
    else if((cpum > 0.67 && cpum < 1.0) && x == "Rock") 
    { 
    System.out.println("You win! :D"); 
    } 
    else if((cpum > 0.67 && cpum < 1.0) && x == "Paper") 
    { 
    System.out.println("The Computer Wins! :("); 
    } 
    else if((cpum > 0.67 && cpum < 1.0) && x == "Scissors") 
    { 
    System.out.println("The game is a tie!"); 
    } 
    compCheck(); 
} 
public static void compCheck() 
{ 
    double cpum = Math.random(); 
    if(cpum <= 0.34) 
    { 
     System.out.println("The Computer chose Rock."); 
    } 
    else if(cpum > 0.34 && cpum <= 0.67) 
    { 
     System.out.println("The Computer chose Paper."); 
    } 
    else if(cpum > 0.67 && cpum <= 1.0) 
    { 
     System.out.println("The Computer chose Scissors."); 
    } 
} 

} 

任何人都可以幫助我告訴我哪裏出了問題?它不會打印結果。

回答

1

你不要用==比較字符串,你應該使用.equals()方法。