当我尝试加载复制教程后开始制作的游戏时出现该错误:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Maze.Map.readFile(Map.java:59)
at Maze.Map.<init>(Map.java:28)
at Maze.Board.<init>(Board.java:16)
at Maze.Maze.<init>(Maze.java:18)
at Maze.Maze.main(Maze.java:7)
如果您知道如何解决此错误,请提供帮助。这是代码,类文件在顶部。
Maze.java
package Maze;
import javax.swing.JFrame;
public class Maze {
public static void main(String[] args){
new Maze();
}
public Maze(){
JFrame f = new JFrame();
f.setTitle("Maze Game");
f.setSize(500,400);
f.setLocationRelativeTo(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Board());
}
}
Board.java
package Maze;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
public class Board extends JPanel implements ActionListener{
private Timer timer;
private Map m;
public Board(){
m = new Map();
timer = new Timer(25, this);
timer.start();
}
public void actionPerformed(ActionEvent e){
repaint();
}
public void paint(Graphics g){
super.paint(g);
for(int y = 0;y < 14; y++){
for(int x = 0;x < 14; x++){
if(m.getMap(x , y).equals("g")){
g.drawImage(m.getGrass(), x * 32, y * 32, null);
}
if(m.getMap(x , y).equals("g")){
g.drawImage(m.getWall(), x * 32, y * 32, null);
}
}
}
}
}
地图.java
package Maze;
import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import javax.swing.ImageIcon;
public class Map {
private Scanner m;
private String Map[] = new String[14];
private Image grass,
wall;
public Map(){
ImageIcon img = new ImageIcon("C://grass.png");
grass = img.getImage();
img = new ImageIcon("C://wall.png");
wall = img.getImage();
openFile();
readFile();
closeFile();
}
public Image getGrass(){
return grass;
}
public Image getWall(){
return wall;
}
public String getMap(int x, int y){
String index = Map[y].substring(x,x + 1);
return index;
}
public void openFile(){
try {
m = new Scanner(new File("C://map.txt"));
} catch (FileNotFoundException e) {
}
}
public void readFile(){
while(m.hasNext()){
for(int i = 0; i < 14; i++){
Map[i] = m.next();
}
}
}
public void closeFile(){
m.close();
}
}
感谢您提前提供任何帮助。抱歉之前没有发布代码,但我之前从未在任何地方发布过代码:/