2つの可能なゲームモードを持つ単純な迷路ゲームでは、スキャナを使用してプレーヤーの名前を尋ねた後、ゲームの開始時にスキャナを実装しようとしています。私はクラスMenuParser
を作ることにしました。オブジェクトを受け入れ、それぞれのメソッドに渡す静的メソッドを使いたいと思います。一つの理由または別のためにNoSuchElementException try/catchブロック内のスキャナで
、Javaは同じエラーを返すに保つ:
Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at game.MenuParser.startUpMenu(MenuParser.java:26) at game.MainProgram.main(MainProgram.java:18)
私は/試してから値を返すに対処する方法について(this oneまたはthis oneなど)同様の質問を見てきましたブロックをキャッチし、NoSuchElementException
とScanner
の原因は何ですか、私はそれを把握していないようです。 return
を移動して、Scanner
を単純化する(try/catchブロックを単純に削除するなど)ようにしましたが、どちらも問題を解決しません。
ご協力いただければ幸いです。
package game;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Player {
private String playerName;
private int playerStepScore;
public Player() {
}
public void setPlayerNameWithScanner(){
System.out.println("Please enter your name. \n");
try(Scanner playerNameScanner = new Scanner(System.in);){
String playerName = playerNameScanner.nextLine();
setPlayerName(playerName);
}
catch(InputMismatchException im) {
System.out.println("Input seems to be wrong");
setPlayerNameWithScanner();
}
}
public String getPlayerName() {
return playerName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public int getPlayerStepScore() {
return playerStepScore;
}
public void setPlayerStepScore(int playerStepScore) {
this.playerStepScore = playerStepScore;
}
}
前のmainメソッドを次のように
package game;
import java.util.InputMismatchException;
import java.util.Scanner;
public class MenuParser {
public static void pause() {
System.out.println("Please hit enter to continue.");
Scanner hitEnter = new Scanner(System.in);
hitEnter.nextLine();
hitEnter.close();
}
public static int startUpMenu() {
int choice = 0;
System.out.println("Which game mode would you like to play?" + "\n");
System.out.println("\t 1. Single player.");
System.out.println("\t 2. Robot.");
try(Scanner startUp = new Scanner(System.in)){
choice = startUp.nextInt();
System.out.println(choice);
if(choice != 1 || choice != 2) {
System.out.println("Sorry, this is not a valid choice.");
startUpMenu();
}
}
catch(InputMismatchException im) {
System.out.println("Sorry, it seems the input is of wrong type.");
startUpMenu();
}
return choice;
}
}
プレーヤークラスが見えます:次のように
package game;
import world.Maze;
public class MainProgram {
public static void main(String[] args) {
// Initialize objects concerned with playing the game
Player myPlayer = new Player();
Maze myMaze = new Maze();
// Print welcome message and ask the player for his/her name
System.out.println("Hi there! Welcome to this maze game in Java by MV. \n");
myPlayer.setPlayerNameWithScanner();
// Ask the player which game mode he/she would like to play: single player or multiplayer?
int choice = MenuParser.startUpMenu();
System.out.println(choice);
// Parse input and depending on the answer initialize the corresponding menu to start the game
}
}
MenuParserクラスが見えます:次のように
mainメソッドが見えます(try-with-recourcesを含む複数のスキャナーを使用していて、エラーや例外なしで)次の:
package game;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.FileReader;
import game.Cell;
import game.Maze;
import game.Border;
public class Program {
public static void main(String[] args) {
/*
* General variables that will be used throughout the loops
* that follow; maxXCoor and maxYCoor are used as maximum values
* to initialize a grid of a certain size; myMaze is a Maze object
* which is needed to access its methods throughout the loops.
*/
int maxXCoor = 0;
int maxYCoor = 0;
Maze myMaze = new Maze();
/*
* A Scanner is invoked to simply deal with the entry
* of the file name; this location is then saved in the
* variable fileName, which is then passed onto the next
* Scanner and FileReader.
*/
System.out.println("Please enter a file name");
Scanner scan = new Scanner(System.in);
String fileName = scan.nextLine();
scan.close();
System.out.println(fileName);
/*
* This first block is used to scan the entire input file
* and look for maxXCoor and maxYCoor to initialize the grid.
*/
try(Scanner input = new Scanner(new FileReader(fileName));){
int count = 1;
while(input.hasNextLine()) {
/*
* Every line is read as a String which is then
* split using a comma as the delimiter. Every
* part that is delimited by a comma is then put into an
* array called words.
*/
String line = input.nextLine();
String[] words = line.split(",");
/*
* Basic sanity check to make sure the input file uses
* the template which was agreed upon, where the first line
* are just headers.
*/
if(count == 1) {
if(!(words[0].toLowerCase()).equals("xcoordinate")) {
System.out.println("It looks like there is an issue with the template of your input file.");
System.out.println("Please check your input file and try again!");
System.exit(0);
}
}
/*
* Once we're not at line one anymore, we can start checking
* the current coordinates, xCoor and yCoor, against
* the maximum coordinates, maxXCoor and maxYCoor.
*/
else {
int xCoor = Integer.parseInt(words[0]);
int yCoor = Integer.parseInt(words[1]);
if(xCoor > maxXCoor) {
maxXCoor = xCoor;
}
if(yCoor > maxYCoor) {
maxYCoor = yCoor;
}
}
count++;
}
}
catch(FileNotFoundException fe) {
System.out.println("File not found");
System.exit(0);
}
/*
* As Java uses zero-indexing, we will want to
* augment the values of our maximum coordinates
* by one, to avoid IndexOutOfBoundsExceptions in
* the future. After that we initialize a 2D array
* of type Cell with these coordinates.
*/
maxXCoor++;
maxYCoor++;
Cell[][] myGrid = new Cell[maxXCoor][maxYCoor];
try(Scanner input = new Scanner(new FileReader(fileName));){
int count = 1;
while(input.hasNextLine()) {
String line = input.nextLine();
String[] words = line.split(",");
if(count > 1) {
int xCoor = Integer.parseInt(words[0]);
int yCoor = Integer.parseInt(words[1]);
Border borderNorth = new Border(words[2]);
Border borderSouth = new Border(words[3]);
Border borderEast = new Border(words[4]);
Border borderWest = new Border(words[5]);
Cell myCell = new Cell(borderNorth,borderSouth,borderEast,borderWest);
myGrid[xCoor][yCoor] = myCell;
}
count++;
}
}
catch(FileNotFoundException fe) {
System.out.println("File not found");
System.exit(0);
}
for(int x = maxXCoor-1; x >= 0; x--) {
// Draw north & south wall
for(int y = 0; y < maxYCoor; y++) {
Border myBorderNorth = myGrid[x][y].getNorthWall();
String myTypeNorth = myBorderNorth.getType();
if(myTypeNorth.equals("wall")) {
System.out.print("+---");
}
else {
System.out.print("+ ");
}
if(y == maxYCoor-1) {
System.out.println("+");
}
}
// Draw west & east wall
for(int y = 0; y < maxYCoor; y++) {
Border myBorderWest = myGrid[x][y].getWestWall();
String myTypeWest = myBorderWest.getType();
if(myTypeWest.equals("wall")) {
System.out.print("| ");
}
else {
System.out.print(" ");
}
// Draw last eastern wall
if(y == maxYCoor-1) {
System.out.println("|");
}
}
// Draw last southern wall
if(x == 0) {
for(int y = 0; y < maxYCoor; y++) {
System.out.print("+---");
}
System.out.println("+");
}
}
myMaze.setGrid(myGrid);
}
}
これはtryブロック{}内に "Scanner startUp = new Scanner(System.in)"を置くことができます。この行は常にtrueです(choice!= 1 || choice!= 2) –
実際に入力していますかあなたが入力を求められたら番号? –
'setPlayerNameWithScanner'メソッドを見ることはできますか? –