두 가지 게임 모드가있는 간단한 미로 게임의 경우 플레이어의 이름을 물어보기 위해 스캐너를 사용한 후 게임을 시작할 때 스캐너를 구현하려고합니다. 나는 클래스를 MenuParser
으로 만들기로 결정했다. 객체를 받아들이고 각각의 메소드에 전달하는 정적 메소드를 사용하고 싶다./NoSuchElementException try/catch 블록에서 스캐너 사용
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;
}
}
이전의 주요 방법을 다음과 같이
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 클래스 보이는 다음과 같이
주요 방법은 보인다 (try-with-recources를 포함하여 오류나 예외없이 여러 스캐너 사용)는 다음과 같이 표시됩니다. 다음 :
try(Scanner startUp = new Scanner(System.in)){
//stuff
}
이는 시도 --자원으로 알려져 있습니다
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)"을 넣을 수 있습니다.이 경우에는 (choice! = 1 || choice! = 2) –
실제로 입력 하시겠습니까? 입력 요청을 받았을 때 번호? –
'setPlayerNameWithScanner' 메소드를 볼 수 있습니까? –