gpt4 book ai didi

java - 扫描仪元件异常

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

我正在做一个简单的剪刀石头布程序,允许用户继续玩。当涉及到重复字符串扫描器的声明时,我不断收到元素异常 -

import java.util.Scanner;
import java.util.Random;

public class RockPaperScissors {
String ai = null;
String player = null;
static int playerscore=0, aiscore=0, ties=0, rounds=0;
static String repeatstring = null;

public static void main(String[] args) {
boolean repeat = false;
RockPaperScissors rps = new RockPaperScissors();
System.out.println("Welcome to Rock Paper Scissors!\n");
do {
System.out.println("Choose your output! R - Rock, P - Paper, S - Scissors");
rps.user();
rps.comp();
rps.game();
System.out.println("YOU - " + playerscore + " / COMPUTER - " + aiscore);
System.out.println("Rounds played - " + rounds + "\n");
System.out.println("Do you wish to continue? Y/N");

// Error reading string input... Solution pending.
Scanner repeatinput = new Scanner(System.in);
repeatstring = repeatinput.nextLine();
if(repeatstring.equals("Y")) {
repeat = true;
}
else if(repeatstring.equals("N")) {
repeat = false;
}
repeatinput.close();

} while (repeat == true);
System.exit(0);
}

void user() {
Scanner scan = new Scanner(System.in);
String input = scan.next();
if(input.equals("R")) {
player = "Rock";
}
else if (input.equals("P")) {
player = "Paper";
}
else if (input.equals("S")) {
player = "Scissors";
}
scan.close();
}

void comp() {
Random rand = new Random();
int randno = rand.nextInt((3 - 1) + 1) + 1;
switch (randno) {
case 1: ai = "Rock";
case 2: ai = "Paper";
case 3: ai = "Scissors";
}
}

void game() {
// If both player and AI has the same output -
if(ai.equals(player)) {
ties = ties + 1;
System.out.println("You chose - " + player);
System.out.println("Computer chose - " + ai);
System.out.println("We have a TIE!");
}

// ROCK VS SCISSORS
if(((ai.equals("Rock")) && (player.equals("Scissors"))) || (player.equals("Rock") && (ai.equals("Scissors")))) {
if(ai.equals("Rock")) {
aiscore = aiscore + 1;
System.out.println("You chose - " + player);
System.out.println("Computer chose - " + ai);
System.out.println("The COMPUTER WINS!");
}
else {
playerscore = playerscore + 1;
System.out.println("You chose - " + player);
System.out.println("Computer chose - " + ai);
System.out.println("YOU WIN!");
}
}

// ROCK VS PAPER
if(((ai.equals("Rock")) && (player.equals("Paper"))) || (player.equals("Rock") && (ai.equals("Paper")))) {
if(ai.equals("Paper")) {
aiscore = aiscore + 1;
System.out.println("You chose - " + player);
System.out.println("Computer chose - " + ai);
System.out.println("The COMPUTER WINS!");
}
else {
playerscore = playerscore + 1;
System.out.println("You chose - " + player);
System.out.println("Computer chose - " + ai);
System.out.println("YOU WIN!");
}
}

// PAPER VS SCISSORS
if(((ai.equals("Scissors")) && (player.equals("Paper"))) || (player.equals("Scissors") && (ai.equals("Paper")))) {
if(ai.equals("Scissors")) {
aiscore = aiscore + 1;
System.out.println("You chose - " + player);
System.out.println("Computer chose - " + ai);
System.out.println("The COMPUTER WINS!");
}
else {
playerscore = playerscore + 1;
System.out.println("You chose - " + player);
System.out.println("Computer chose - " + ai);
System.out.println("YOU WIN!");
}
}
rounds = rounds + 1;
}
}

程序在请求重复输入(Y/N)后自行终止,并显示以下错误消息:

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at RockPaperScissors.main(RockPaperScissors.java:28)

最佳答案

通常,当您遇到异常时,最好在线查找该异常(除非您知道抛出异常的原因)。在这种情况下,如果我们查找您收到的异常,我们将定向到 Javadocs,其中指出:

Thrown by the nextElement method of an Enumeration to indicate that there are no more elements in the enumeration.

这让我们查看 Scanner 的 Javadoc,看看这是否是由 nextLine() 抛出的。 。我们在Throws部分看到它说:

NoSuchElementException - if no line was found

这意味着您的程序正在尝试从尚未获得结果的扫描仪中获取数据。如果您希望它等到有结果,那么还有其他可用的方法可以帮助您的程序知道结果何时可用,特别是 hasNextLine() 。这意味着您应该在尝试获取响应之前检查用户是否已响应。

一如既往,最好尽可能检查 Javadocs。

顺便说一句,最好不要每次循环运行时都生成扫描器,而是在循环外部声明它,然后在完成后关闭它。

关于java - 扫描仪元件异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29837639/

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