【问题标题】:My Rock Paper Scissors game won't display ties我的石头剪刀布游戏不会显示领带
【发布时间】:2014-03-13 04:53:50
【问题描述】:

我已经用 BlueJ 编写了这个剪刀石头布代码,到目前为止,一切都运行良好......除了一件事。当我与电脑绑定时,游戏不会告诉我。它会告诉我电脑使用了什么,它会告诉我我是赢了还是输了。但是,如果我打了领带,它只会告诉我计算机使用了什么,而忽略领带。代码如下。 ~~~

/**
*A simple game of Rock Paper Scissors.
*
*@author Erik Ingvoldsen
*@version 1.0
*/

import java.util.Scanner;
import java.util.Random;
public class Rock
{
public static void main(String[] args)
{
String playAgain = "4";    
do
    {
    String personPlay;
    String computerPlay = "";
    int computerInt;
    String response;
    Scanner scan = new Scanner(System.in);
    Random generator = new Random();
    System.out.println("   ");
    System.out.println("Press 1 for Rock, 2 for Scissors, 3 for Paper, and 4 to quit.");
    System.out.println();
    computerInt = generator.nextInt(3)+1;
    if (computerInt == 1)
       computerPlay = "Rock";
    else if (computerInt == 2)
       computerPlay = "Scissors";
    else if (computerInt == 3)
       computerPlay = "Paper";
    //1=Rock 2=Scissors 3=Paper
    {System.out.println("Rock Paper Scissors: ");
    {personPlay = scan.next();
    System.out.println("Computer chooses: " + computerPlay);
    if (personPlay.equals(computerPlay)){ 
       System.out.println("It's a tie!");}
    else if (personPlay.equals("1")){
       if (computerPlay.equals("Paper"))
          System.out.println("Paper beats rock. Loser.");
    else if (computerPlay.equals("Scissors"))
            System.out.println("Rock beats scissors. Winner.");}
    else if(personPlay.equals("2")){
       if (computerPlay.equals("Paper"))
          System.out.println("Scissors beats paper. Winner");
    else if (computerPlay.equals("Rock"))
            System.out.println("Rock beats scissors. Loser.");}
   else if (personPlay.equals("3")) {
       if (computerPlay.equals("Rock"))
          System.out.println("Paper beats rock. Winner.");
   else if (computerPlay.equals("Scissors"))
            System.out.println("Scissors beats paper. Loser.");}
   else
System.out.println("Not a valid key. Cheaters never win.");}
System.out.println("Press 4 to quit or any other key to continue.");
Scanner End = new Scanner(System.in);
String quit=End.nextLine();
if(quit.equals("4"))
{
System.exit(0);}
}
}while(true);
}
}

【问题讨论】:

    标签: java


    【解决方案1】:

    您似乎正在尝试将"1""Paper" 进行比较,并期待true

    要么同时比较true,即将用户输入与"1" 和计算机与"Paper" 进行比较,要么将"1" 转换为"Paper"(以及相应的其他两个值)并比较响应直接。

    【讨论】:

      【解决方案2】:

      personPlay 可以是 1、2 或 3。 computerPlay 可以是 Paper、Rock 或 Scissors。

      因此f (personPlay.equals(computerPlay)){ 行没有意义。

      【讨论】:

        【解决方案3】:

        如前所述,您正在尝试使用此行将数字与字符串进行比较

        personPlay.equals(computerPlay)
        

        这里personPlay 是该人输入的数字(作为字符串读入),computerPlay 是您根据计算机获得的数字设置的字符串,即computerInt

        简单的解决方法是更改​​上面的比较来检查computerInt的String值

        personPlay.equals(String.valueOf(computerInt))
        

        所以数字是比较的(作为字符串)

        【讨论】:

          猜你喜欢
          • 2016-01-16
          • 2022-11-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多