我必须在java中制作Rock,Paper,Scissors游戏。这是我的代码:
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.");
}
}
}
任何人都可以通过告诉我出了什么问题来帮助我吗?它不会打印结果。
答案 0 :(得分:1)
你不要将字符串与==进行比较,你应该使用.equals()方法。