如何修复石头剪刀的java代码?
我想将字符串Human转换成整数,这是我的指导老师在插入try/catch之前告诉我的 因为Human不是(123)那样的值,所以我认为try-catch适用于数字格式异常 在我最初的代码中,我没有使用HC作为变量,而是继续使用Human如何修复石头剪刀的java代码?,java,Java,我想将字符串Human转换成整数,这是我的指导老师在插入try/catch之前告诉我的 因为Human不是(123)那样的值,所以我认为try-catch适用于数字格式异常 在我最初的代码中,我没有使用HC作为变量,而是继续使用Human 但它仍然列出了所有答案,而不是从代码的开关部分将人与计算机进行比较,看起来您接受用户输入为整数,对吗 如果是这样,为什么不使用nextInt()来读取int输入,而不是从代码的开关部分读取nextLine()?,看起来您接受用户输入作为整数,对吗 如果是这种情
但它仍然列出了所有答案,而不是从代码的开关部分将人与计算机进行比较,看起来您接受用户输入为整数,对吗
如果是这样,为什么不使用
nextInt()
来读取int输入,而不是从代码的开关部分读取nextLine()
?,看起来您接受用户输入作为整数,对吗
如果是这种情况,为什么不使用
nextInt()
来读取int输入,而不是nextLine()
?而不是笨拙的try/catch块,我建议给用户一个菜单,提示他们输入一个可以轻松与随机整数进行比较的整数,例如:
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;
}
}
如果您需要接受字符串是赋值的一部分,则使用字符串的equals方法通过If语句转换为整数
Scanner keyboard = new Scanner(System.in);
System.out.println("Make Selection:");
System.out.println("1) Rock");
System.out.println("2) Paper");
System.out.println("3) Scissors ");
int choice = keyboard.nextInt()-1;
您的代码中还有其他错误(请注意注释),但这至少应该为您提供一些选项,以克服将用户输入转换为您的程序可以轻松使用的内容的困难。而不是笨拙的try/catch块,我建议为用户提供一个菜单,提示他们输入一个整数,您可以轻松使用该整数与随机整数进行比较,例如:
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;
}
}
如果您需要接受字符串是赋值的一部分,则使用字符串的equals方法通过If语句转换为整数
Scanner keyboard = new Scanner(System.in);
System.out.println("Make Selection:");
System.out.println("1) Rock");
System.out.println("2) Paper");
System.out.println("3) Scissors ");
int choice = keyboard.nextInt()-1;
您的代码中还有其他错误(请注意注释),但这至少应该为您提供一些选项,以克服将用户输入到您的程序可以轻松使用的内容中的困难。这一行和代码中的许多类似行上有两个大错误:
int choice;
string human= keyboard.nextLine();
if(human.equals("Rock")){
choice = 0;
}else if(human.equals("Paper"){
choice = 1;
}else if(human.equals("Scissors"){
choice = 2;
}else{
System.out.print("Invalid Selection");
}
第一个错误是分号错误。Java(像C和其他语言一样)有一个“空”语句,它只是一个分号,没有其他任何东西,它什么也不做。你实际上做的是
if (Human.equals(2));
if
无效,无论if
是否为真,都会发生println
。去掉分号。此外,最好养成习惯,在if
的主体部分始终使用花括号,如
if (Human.equals(2))
/* do nothing */ ;
System.out.println("Rock crushes scissors, i win");
Java不需要大括号,但最好总是使用大括号。一些公司和风格检查器确实需要大括号。它们有助于防止当您认为语句是if
的一部分而它不是时出现的错误
第二个问题是,您正在将一个字符串
(Human
)与一个不起作用的整数(2)进行比较。这将始终返回false
,无论Human
是什么。Java允许您比较不同类的两个对象,但结果总是false
,除非您编写了自己的自定义equals
,可以比较两个不同类的对象(通常不是一个好主意,除非它们是同一祖先类的子类)
看起来您希望将输入字符串解析为整数,给出一个int
,然后使用int
进行比较。(这让用户有点困惑,因为您没有告诉他们输入一个数字。但我会同意,因为它让我提出了一些其他要点。)这就是您尝试的:
if (Humans.equals(2)) {
System.out.println("Rock crushes scissors, i win");
}
其余的逻辑在catch
中,这意味着只有Human
不是有效的整数(可能不是您想要的)时,它才会执行。catch
块中的代码仅在异常发生时执行。您可以通过将逻辑的其余部分移动到try
块(在花括号之间)来解决此问题,然后该代码可以比较HC
,而不是Human
,如下所示:
try
{
int HC = Integer.parseInt(Human.trim()); // i know this part is wrong
}
catch (NumberFormatException nfe)
{
您可以做的另一件事是提前关闭catch
块。您可以这样做:
if (HC == 2) {
System.out.println("Rock crushes scissors, i win");
}
我不建议以这种方式处理异常,但我这样做是为了说明一点。如果你说inthc=Integer.parseInt(Human.trim())代码>在try
的花括号内,它仅在这些花括号内可见。因此,catch
块下面的逻辑无法看到HC
。通过将HC
的声明移到这些花括号之外,现在可以通过catch
块后面的逻辑看到它。所以,您可以再次将if(Human.equals(2))更改为
int HC;
try
{
HC = Integer.parseInt(Human.trim()); // i know this part is wrong
}
catch (NumberFormatException nfe)
{
HC = -1;
}
这一行以及代码中许多类似的行上有两个大错误:
int choice;
string human= keyboard.nextLine();
if(human.equals("Rock")){
choice = 0;
}else if(human.equals("Paper"){
choice = 1;
}else if(human.equals("Scissors"){
choice = 2;
}else{
System.out.print("Invalid Selection");
}
第一个错误是分号错误。Java(像C和其他语言一样)有一个“空”语句,它只是一个分号,没有其他任何东西,它什么也不做。你实际上做的是
if (Human.equals(2));
if
无效,无论if
是否为真,都会发生println
。去掉分号。此外,最好养成习惯,在if
的主体部分始终使用花括号,如
if (Human.equals(2))
/* do nothing */ ;
System.out.println("Rock crushes scissors, i win");
Java不需要大括号,但最好总是使用大括号。一些公司和风格检查器确实需要大括号。它们有助于防止当您认为语句是if
的一部分而它不是时出现的错误
第二个问题是,您正在将一个字符串
(Human
)与一个不起作用的整数(2)进行比较。这将始终返回false
,无论Human
是什么。Java允许您比较不同类的两个对象,但结果总是false
,除非您编写了自己的自定义equals
,可以比较两个不同类的对象(通常不是g