我正在尝试用Java编写一个程序,其中我在文本文件中写入了一些命令,根据这些命令,马将沿相同方向移动或将其角度更改10度。马的起始位置是(0,0),且朝东。目的地以以(500,0)坐标为中心的正方形表示。正方形在(500,0)坐标的北,南,东和西延伸30米:
这是command.txt文件:
Go
Hah
Go
Gaff
Go
Gaff
Go
Go
Whoa
命令含义:
Go
朝着您的方向向前移动50米。
Hah
。向左转10度,但不要向前移动。
Gaff
右转10度,但不要向前移动。
Whoa
停止。
代码如下:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class HorseRiding {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Enter the path of the file");
Scanner sc = new Scanner(System.in);
String filename = sc.nextLine();
horse(filename);
}
public static void horse(String filename) {
File inputFile = new File(filename);
Scanner readFile = null;
try {
readFile = new Scanner(inputFile);
} catch (FileNotFoundException e) {
System.out.println("Error! File not found!");
}
double x=0, y=0, angle=0, distance=50, x2=0, y2=0;
System.out.println("The horse starts at: (" + x + "," + y + ")");
while(readFile.hasNextLine()){
String line = readFile.nextLine();
while(line.equals("Go"))
{
x += distance + x2;
y += y2;
System.out.println("Command: "+line+", New position: ("+x+", "+y+")");
line = readFile.nextLine();
}
while(line.equals("Hah"))
{
angle -= 10;
x2 = distance * Math.cos(angle);
y2 = distance * Math.sin(angle);
System.out.println("Command: "+line+", New position: ("+x+", "+y+")");
line = readFile.nextLine();
}
while(line.equals("Gaff"))
{
angle+=10;
x2 = distance * Math.cos(angle);
y2 = distance * Math.sin(angle);
System.out.println("Command: "+line+", New position: ("+x+", "+y+")");
line = readFile.nextLine();
}
while(line.equals("Whoa"))
{
System.out.println("Final Position of Horse: ("+x+", "+y+")");
if((x<530.00 && x>470) && (y>-30.00 && y<30.00)) {
System.out.println("SUCCESS");
break;
}
else {
System.out.println("FAILURE");
break;
}
}
readFile.close();
}
}
}
我收到异常错误:
Exception in thread "main" java.lang.IllegalStateException: Scanner closed
at java.base/java.util.Scanner.ensureOpen(Scanner.java:1150)
at java.base/java.util.Scanner.findWithinHorizon(Scanner.java:1781)
at java.base/java.util.Scanner.hasNextLine(Scanner.java:1610)
at HorseRiding.horse(HorseRiding.java:40)
at HorseRiding.main(HorseRiding.java:14)
此外,它运行两次,直到第二个命令Hah,然后引发上述异常。
答案 0 :(得分:0)
请查看以下代码段,此处还解决了其他一些问题:
:blogs="blogs"
,x2
,因为y2
,Math.cos
接受以弧度表示的角度,而不是度数。Math.sin
的打印件。angle
给定样本文件的结果输出为:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class HorseRiding {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Enter the path of the file");
Scanner sc = new Scanner(System.in);
String filename = sc.nextLine();
horse(filename);
//horse("horse.txt");
}
private static void horse(String filename) throws FileNotFoundException {
File inputFile = new File(filename);
Scanner readFile = new Scanner(inputFile);
final double distance = 50, successX = 500, successY = 0, range = 30;
double x = 0, y = 0, angle = 0, x2 = distance, y2 = 0;
System.out.println("The horse starts at: (" + x + ", " + y + ")");
while (readFile.hasNext()) {
String command = readFile.next();
if ("Whoa".equals(command)) {
break;
}
if ("Go".equals(command)) {
x += x2;
y += y2;
} else if ("Hah".equals(command) || "Gaff".equals(command)) {
angle += "Hah".equals(command) ? 10 : -10;
x2 = distance * Math.cos(Math.toRadians(angle));
y2 = distance * Math.sin(Math.toRadians(angle));
}
//System.out.println("Command: " + command + ", New position: (" + x + ", " + y + "), angle: " + angle);
System.out.printf("Command: %-4s\tNew position: (%6.2f, %6.2f)\tangle: % 3.0f%n",
command, x, y, angle);
}
if (x >= successX - range && x <= successX + range &&
y >= successY - range && y <= successY + range) {
System.out.println("SUCCESS");
} else {
System.out.println("FAILURE");
}
readFile.close();
}
}