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;
}
}
我想将字符串 Human 转换为整数,这是我的老师在插入 try/catch 之前告诉我要做的事情
由于 Human 不是像 (123) 这样的值,我认为 try catch 适用于数字格式异常。
在我的原始代码中,我没有使用 HC 作为变量,我继续使用 Human 但它仍然列出了所有答案,而不是比较人类和计算机
最佳答案
这一行以及代码中的许多类似行有两个大错误:
if (Human.equals(2));
第一个错误是分号写错了。 Java(与 C 和其他语言一样)有一个“空”语句,它只是一个分号,没有任何其他内容,并且它什么也不做。您实际上所做的是
if (Human.equals(2))
/* do nothing */ ;
System.out.println("Rock crushes scissors, i win");
if
没有任何作用,并且无论 if
是否为 true,println
都会发生。去掉分号。另外,最好养成在 if
主体中始终使用大括号的习惯,例如
if (Humans.equals(2)) {
System.out.println("Rock crushes scissors, i win");
}
Java 不需要大括号,但最好始终使用它们。一些公司和风格检查员确实需要大括号。当您认为某个语句是 if
的一部分但它不是时,它们有助于防止出现错误。
第二个是您将String
(Human
) 与整数(2) 进行比较,这是行不通的。无论 Human
是什么,都将始终返回 false
。 Java 允许您比较不同类的两个对象,但结果始终为 false
,除非您编写了自己的自定义 equals
来比较两个不同类的对象(不是通常是个好主意,除非它们是同一祖先类的子类)。
看起来您想将输入字符串解析为整数,给出一个 int
,然后使用 int
进行比较。 (这对用户来说有点困惑,因为您没有告诉他们输入数字。但我会同意它,因为它让我可以提出其他一些重要的观点。)这就是您尝试过的:
try
{
int HC = Integer.parseInt(Human.trim()); // i know this part is wrong
}
catch (NumberFormatException nfe)
{
其余的逻辑位于catch
内,这意味着只有当Human
不是有效整数时才会执行,这可能是不是你想要的。 catch
block 中的代码仅在异常发生时执行。您可以通过将其余逻辑移至 try
block (大括号之间)来解决此问题,然后该代码可以比较 HC
而不是 Human
,像这样:
if (HC == 2) {
System.out.println("Rock crushes scissors, i win");
}
您可以做的另一件事是提前关闭 catch
block 。你可以这样做:
int HC;
try
{
HC = Integer.parseInt(Human.trim()); // i know this part is wrong
}
catch (NumberFormatException nfe)
{
HC = -1;
}
我不建议以这种方式处理异常,但我这样做是为了表明一点。如果您在 try
的大括号内输入 int HC = Integer.parseInt(Human.trim());
,它仅在这些大括号内可见。因此,catch
block 下方的逻辑看不到 HC
。通过将 HC
的声明移动到大括号之外,现在它对于 catch
block 之后的逻辑可见。那么你可以再次将 if (Human.equals(2))
更改为
if (HC == 2)
关于java - 如何修复石头剪刀布的java代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34758494/