gpt4 book ai didi

Java 石头剪刀布蜥蜴 Spock 使用字符和字符串

转载 作者:行者123 更新时间:2023-12-01 17:56:11 25 4
gpt4 key购买 nike

您好,感谢您提前提供的任何帮助。我是 Java 新手,在我的第一个类上,并且已经为此工作了几天。

从标题中您可能可以看出,这是《石头剪刀布》的扩展版本。

我使用 string 和 int 作为变量编写了一个很棒的工作程序,但后来被告知我必须以字符形式获取用户输入。现在我在让用户输入等于计算机输入时遇到问题。

更新:我重写了程序,使 comp 成为一个字符。现在,除了平局之外,一切都有效。问题是这样的。示例: userInput = r comp=R 这应该是平局并输出“这是平局!”我一直在尝试 Charactor.toUpperCase 和 Charactor.toLowerCase 但我认为我没有正确的语法,因此如果 userInput = comp 则忽略大小写。谁能给我正确的语法?谢谢!

import java.util.Scanner;

public class Rocktest
{
public static void main(String[] args) throws Exception
{
char userInput;
char comp;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter R for Rock, P for Paper, S for Scissors, L
for Lizard and K for Spock: ");
userInput = (char)System.in.read();
keyboard.nextLine();
int comt=(int)(5*Math.random())+1;
comp=(' ');

if (comt==1){
comp=('R');
}
else if (comt==2){
comp='P';
}
else if (comt==3){
comp='S';
}

else if (comt==4){
comp='L';
}

else if (comt==5){
comp='K';
}


if((userInput == 'R')||( userInput == 'r'))
{
System.out.println("You choose: Rock");

}
else if((userInput == 'P')||(userInput == 'p'))
{
System.out.println("You choose: Paper");

}
if((userInput == 'S')||( userInput == 's'))
{
System.out.println("You choose: Scissors");

}
if((userInput == 'L')||( userInput == 'l'))
{
System.out.println("You choose: Lizard");
}

if((userInput == 'K')||( userInput == 'k'))
{
System.out.println("You choose: Spock");
}

System.out.println("I choose: " + comp); //This is where I'm having the
//problem
//need to ignore case
if (userInput==comp)
{
System.out.println("A tie!");
}

else if((userInput == 'R')||( userInput == 'r')) {
if (comp == 'S')
System.out.println("Rock crushes scissor - you win!");
else if (comp == 'L')
System.out.println("Rock crushes Lizard - you win!");
else if (comp == 'P')
System.out.println("Paper covers rock - you lose!");
else if (comp == 'K')
System.out.println("Spock vaporizes rock - you lose!");
}
else if ((userInput == 'P')||(userInput == 'p')) {
if (comp == 'S')
System.out.println("Scissors cut paper- you lose!");
else if (comp == 'K')
System.out.println("Paper disproves Spock - you win!");
else if (comp == 'R')
System.out.println("Paper covers rock - you win!");
if (comp == 'L')
System.out.println("Lizard eats paper- you lose!");
}
else if((userInput == 'S')||( userInput == 's')) {
if (comp == 'P')
System.out.println("Scissors cut paper - you win!");
else if (comp == 'L')
System.out.println("Scissors decapitates lizard - you
win!");
else if (comp == 'R')
System.out.println("Rock crushes scissors - you lose!");
else if (comp == 'K')
System.out.println("Spock smashes scissors - you lose!");
}
else if((userInput == 'L')||( userInput == 'l')) {
if (comp == 'P')
System.out.println("Lizard eats paper - you win!");
else if (comp == 'K')
System.out.println("Lizard poisons Spock - you win!");
else if (comp == 'R')
System.out.println("Rock crushes lizard - you lose!");
else if (comp == 'S')
System.out.println("Scissors decapatates lizard - you
lose!");
}
else if ((userInput == 'K')||( userInput == 'k')) {
if (comp == 'P')
System.out.println("Paper disproves Spock - you lose!");
else if (comp == 'L')
System.out.println("Lizard poisons Spock - you lose!");
else if (comp == 'R')
System.out.println("Spock vaporizes rock - you win!");
else if (comp == 'S')
System.out.println("Spock smashes scissors - you lose!");
}
//System.out.println(+ comt); used these lines for testing
//System.out.println(+ comp);
//System.out.println(+ userInput);

}//end main
}//end class

最佳答案

好的,所以我相信我已经找到了您的问题。

在您的代码中,您有行 if (comp == 'S') ,假设变量 comp 是一个 ,该行可以正常工作字符。但是,如果 compString,它永远不能等于 char S 或变量 userInput,这也是一个char

简单修复:更改 comp 的变量类型。

就是这样。我希望这对您有所帮助,并祝您在 Java 世界中好运。

关于Java 石头剪刀布蜥蜴 Spock 使用字符和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44599282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com