package rps;
import java.util.Random;
import java.util.Scanner;
public class Rps
{
public static void main(String[] args)
{
int rock, paper, scissors;
int compa;
String Computer;
String Human;
Scanner keyboard = new Scanner(System.in);
System.out.println("Rock, paper, or scissors?");
Human= keyboard.nextLine();
System.out.println("You chose");
System.out.println(Human);
try
{
int HC = Integer.parseInt(Human.trim()); // i know this part is wrong
}
catch (NumberFormatException nfe)
{
Random generator = new Random();
compa=generator.nextInt(3);
switch(compa){
case 0:
rock=0;
System.out.println("I choose Rock");
if (Human.equals(2));
System.out.println("Rock crushes scissors, i win");
if(Human.equals(0));
System.out.println("You must be psychic, i chose rock too!");
if (Human.equals(1));
System.out.println("paper covers rock, you lost");
break;
case 1:
paper=1;
System.out.println("I chose paper");
if (Human.equals(0));
System.out.println("Paper covers rock, you lose");
if (Human.equals(1));
System.out.println("It's a tie!");
if (Human.equals(2));
System.out.println("Paper covers rock, i lost");
break;
case 2:
scissors=2;
System.out.println("I chose scissors");
if (Human.equals(1));
System.out.println("scissors cut through paper, i win.");
if (Human.equals(2));
System.out.println("We both chose scissors");
if (Human.equals(0));
System.out.println("Rock smashed scissors, i lost");
break;
}
}
我要串人转换为整数这就是我的教练告诉我,做之前,我插入的try/catch我该如何修复我的java代码的岩石剪刀剪刀?
因为人类并不像(123)的值,我图的尝试catch可以用于数字格式异常。
在我的原代码,我没有使用HC为变量我继续使用人力 但它仍然列出的所有问题的答案,而不是人比较计算机
首先,你的'if'语句被打破。 if语句需要用'{'和'}'来包围它们的身体。像这样:'if(1 + 1 == 2){return 2; }'。 “if”部分旁边没有分号,只在主体('return 2;')中。试着看看[Codecademy](https://www.codecademy.com/learn/learn-java)来补充你在学校的学习。或者,如果您已分配了课本,则可能需要更频繁地参考。您也可以为您提供辅导资源。修复程序中的现有问题可能会帮助您解决问题。 – ordonezalex
在上面的代码中为什么你在catch块中使用switch,并且你在catch块的catch下面编码,因为你没有关闭它。 –