java - 为什么我的程序在获取字符串而不是字符时会自行终止?
问题描述
我正在为我的java类制作一个石头剪刀布游戏,但是当我要求它接受一个字符串时,程序在运行后就终止了。我之前将它设置为取一个字符,并且每次都有效。我哪里出错了?
PS如果您不介意向我展示如何再次插入播放选项,那就太好了。
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Choose your hand (rock, paper, scissors, lizard, Spock):");
String hand = input.nextLine().toUpperCase();
int opponent = (int)(Math.random() * ((4) + 1));
if (hand == "ROCK") {
if (opponent == 0 ) {
System.out.println("Rock does nothing to rock. Tie.");
}
else if (opponent == 1) {
System.out.println("Paper covers rock. You Lose...");
}
else if (opponent == 2) {
System.out.println("Rock smashes scissors. You Win! ");
}
else if (opponent == 3) {
System.out.println("Rock smashes lizard. You Win!");
}
else {
System.out.println("Spock vaporizes rock. You Lose...");
}
}
if (hand == "PAPER") {
if (opponent == 0 ) {
System.out.println("Paper covers rock. You Win!");
}
else if (opponent == 1) {
System.out.println("paper does nothing to paper. Tie.");
}
else if (opponent == 2) {
System.out.println("Scissors cuts paper. You Lose...");
}
else if (opponent == 3) {
System.out.println("Lizard eats paper. You Lose...");
}
else {
System.out.println("Paper disproves Spock. You Win!");
}
}
if (hand == "SCISSORS") {
if (opponent == 0 ) {
System.out.println("Rock smashes Scissors. You Lose...");
}
else if (opponent == 1) {
System.out.println("Scissors cuts paper. You Win!");
}
else if (opponent == 2) {
System.out.println("Scissors do nothing to scissors. Tie.");
}
else if (opponent == 3) {
System.out.println("Scissors decapitates lizard. You Win!");
}
else {
System.out.println("Spock smashes scissors. You Lose...");
}
}
if (hand == "LIZARD") {
if (opponent == 0 ) {
System.out.println("Rock smashes lizard. You Lose...");
}
else if (opponent == 1) {
System.out.println("Lizard eats paper. You Win!");
}
else if (opponent == 2) {
System.out.println("Scissors decapitates lizard. You Lose...");
}
else if (opponent == 3) {
System.out.println("Lizard does nothing to lizard. Tie.");
}
else {
System.out.println("Lizard poisons Spock. You Win!");
}
}
if (hand == "SPOCK" ) {
if (opponent == 0 ) {
System.out.println("Spock vaporizes rock. You Win!");
}
else if (opponent == 1) {
System.out.println("Paper disproves Spock. You Lose...");
}
else if (opponent == 2) {
System.out.println(" Spock smashes scissors. You Win!");
}
else if (opponent == 3) {
System.out.println("Lizard poisons Spock. You Lose...");
}
else {
System.out.println("Spock does nothing to Spock. Tie");
}
if (hand != "ROCK" && hand != "PAPER" && hand != "SCISSORS" && hand != "LIZARD" && hand != "SPOCK") {
System.out.println("Invalid hand.");
}
}
}
}
解决方案
尝试这个
if (hand == "PAPER") {
if (opponent == 0 ) {
System.out.println("Paper covers rock. You Win!");
}
else if (opponent == 1) {
System.out.println("paper does nothing to paper. Tie.");
}
else if (opponent == 2) {
System.out.println("Scissors cuts paper. You Lose...");
}
else if (opponent == 3) {
System.out.println("Lizard eats paper. You Lose...");
}
else {
System.out.println("Paper disproves Spock. You Win!");
}
像这样改变
if(hand.equals("//Preferred String to Check"))
if (hand.equals("PAPER")) {
if (opponent == 0 ) {
System.out.println("Paper covers rock. You Win!");
}
else if (opponent == 1) {
System.out.println("paper does nothing to paper. Tie.");
}
else if (opponent == 2) {
System.out.println("Scissors cuts paper. You Lose...");
}
else if (opponent == 3) {
System.out.println("Lizard eats paper. You Lose...");
}
else {
System.out.println("Paper disproves Spock. You Win!");
}
推荐阅读
- ios - ios 中的 Twilio QuickStarter 项目问题
- java - Tika:OOXMLParser 是什么意思?
- python - 在 python 中拦截和过滤对 HTTPS 服务器的请求?
- python - Kivy 将图像放置在动态生成按钮的某个位置
- android - 安卓工作室。设计看不到设计上的任何东西
- java - 用户在片段中的不同视图
- xml - 用 xslt 确定 xml 中的列数
- python - Vispy 没有名为“vispy.visuals.text._sdf_cpu”的模块
- angular - 路由 Angular 4 中的错误
- laravel - 使用 axios vue.js 检索数据