按Enter键时程序不会继续。我该如何解决这个错误?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (16)

我正在为我的java课程做石头剪刀家庭作业。我完成了程序,我成功编译了它。我运行该文件,看起来它正在工作。它给了我菜单,我选择了一个变量R,例如,当我按下回车时,它不会做任何事情,只能转到下一行。我再次按回车键,它给了我一个索引越界错误,我假设是因为它第二次没有使用的变量。如何让程序继续前进?该计划应该播放五次然后返回一个胜利者。提前致谢。这个图像是我运行程序并按两次输入时得到的图像

package Rock;
import java.util.Scanner;




public class RPSG {




public static void main(String[] args) {

    String[] computerHandArray = {"R","P","S"};
    String computerHand ="", thisWinner="", theFinalWinner="";
    int index=0;
    int timesIWon=0;
    int timesComputerWon=0;
    Scanner in = new Scanner(System.in);
    System.out.println("\tMenu\n\n(R) Rock\n(P) Paper\n(S) Scissors" + "\n\nEnter Your Hand (R, P, or S): ");
    for (int i=0; i<5; i++) {
        String myHandString = in.nextLine();
        String myHand = myHandString.substring(0,1);
        myHand = myHand.toUpperCase();
        index = (int)(Math.random() * 10) % 3;
        computerHand = computerHandArray[index];
        thisWinner = theWinnerofOneGame(myHand, computerHand);

        if(thisWinner.equals("ME")){
            timesIWon++;
        }
            else if(thisWinner.equals("COMPUTER")) {
                timesComputerWon++;
        }
    }

        if(timesIWon == timesComputerWon)
        theFinalWinner = "TIE";
    else if(timesIWon > timesComputerWon)
            theFinalWinner = "ME";
    else if(timesComputerWon > timesIWon)
        theFinalWinner = "COMPUTER";
    System.out.println("I won :" + timesIWon);
    System.out.println("I won :" + timesComputerWon);
    System.out.println("The Final Winner after 5 games is:" +theFinalWinner);
    }
private static String theWinnerofOneGame(String myHand, String computerHand){
    String theWinner = "Tie";
    if(myHand.equals(computerHand)) {
        theWinner = "Tie"; 
}
    else if(myHand.equals("R")) {
        if (computerHand.equals("P")) {
            theWinner = "COMPUTER";
        }
    }

    else if(computerHand.equals("S")) {
        theWinner = "ME";
    }

else if(myHand.equals("P")) {
    if (computerHand.equals("R")) {
        theWinner = "ME";
    }
    else if(computerHand.equals("S")) {
        theWinner = "COMPUTER";
        }
    }
else if(myHand.equals("S")) {
    if (computerHand.equals("R")) {
        theWinner = "COMPUTER";

    }

    else if(computerHand.equals("P")) {
        theWinner = "ME";
    }
}
    return theWinner;
    }

}
提问于
用户回答回答于

您只打印一次输入提示,即在for循环之前。现在,当您输入第一个输入时,将执行循环的内容。因为你不在循环中打印任何东西,所以下一轮没有提示。再次按Enter键后,in.nextLine()返回一个空字符串,然后该substring方法抛出异常。

你可能应该这样做(注意标记的行):

System.out.println("\tMenu\n\n(R) Rock\n(P) Paper\n(S) Scissors" + "\n\n");
    for (int i=0; i<5; i++) {
>        System.out.println("Enter Your Hand (R, P, or S): ");
        String myHandString = in.nextLine();
        String myHand = myHandString.substring(0,1);
        myHand = myHand.toUpperCase();
        index = (int)(Math.random() * 10) % 3;
        computerHand = computerHandArray[index];
        thisWinner = theWinnerofOneGame(myHand, computerHand);

        if(thisWinner.equals("ME")){
            timesIWon++;
>            System.out.println("You won.");
        } else if(thisWinner.equals("COMPUTER")) {
            timesComputerWon++;
>            System.out.println("The computer won.");
        }
    }

更好的是,在计算子字符串之前检查用户的输入是否有效。

扫码关注云+社区

领取腾讯云代金券