我正在 jGrasp 上用 JavaScript 编写代码,我尝试寻找解决方法,但我就是无法理解。它显示以下内容。
rockPaperScissor.java:35: 错误:类型不兼容:int 无法转换为 String 案例 0: ^ rockPaperScissor.java:38: 错误:类型不兼容:无法将 int 转换为 String 情况1: ^ rockPaperScissor.java:41: 错误:类型不兼容:无法将 int 转换为 String 案例 2: ^ 3个错误
这是我的代码。
import java.util.Scanner;
public class rockPaperScissor
{
public static void main(String [] args)
{
Scanner input = new Scanner (System.in);
int computer = (int)(Math.random() * 3);
System.out.print("rock (1), paper(2), scissor(3): ");
int user = input.nextInt();
System.out.print("the computer is ");
switch (computer)
{
case 0: System.out.print("scissor."); break;
case 1: System.out.print("rock."); break;
case 2: System.out.print("paper.");
}
switch (user)
{
case 0: System.out.print("scissor"); break;
case 1: System.out.print("rock"); break;
case 2: System.out.print("paper");
}
String name = " ";
switch (name)
{
case 1:
name = "scissor";
break;
case 2:
name = "rock";
break;
case 3:
name = "paper";
}
System.out.print("You are " + name );
if (computer == user)
System.out.println("too.It is a draw.");
else
{
boolean win = (user == 0 && computer == 2) ||
(user == 1 && computer == 0)||
(user == 2 && computer == 1);
if (win)
System.out.println(". You won.");
else
System.out.println(". You lose.");
}
}
}
我期待它说 石头(1)、布(2)、剪刀(3):1 电脑是剪刀。你是摇滚乐。你赢了
石头(1)、布(2)、剪刀(3):3 电脑是摇滚。你是剪刀。你输了
石头(1)、布(2)、剪刀(3):2 电脑是纸。你是纸。你画。