将马在 x 轴和 y 轴上旋转 10 度角 Java

Rotate a horse by 10 degree angle in x axis as well as y axis in Java

我正在尝试在 Java 中编写一个程序,其中我在文本文件中编写了一些命令,根据这些命令,马将朝相同的方向移动或将其角度改变 10 度.马的起始位置是 (0,0) 并且面向东方 目的地表示为以 (500,0) 坐标为中心的正方形。正方形在(500,0)坐标的北、南、东、西各延伸30米:

here is the position of horse

这是 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,然后抛出上述异常。

请查看以下代码段,这里解决了一些其他问题:

  1. 处理每个命令的简化逻辑。
  2. 修复了 x2y2 的计算,因为 Math.cosMath.sin 接受以弧度为单位的角度,而不是度数。
  3. 添加了 angle 的印刷品。
  4. 添加了成功点和范围的参数。
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();
    }
}

给定样本文件的结果输出是:

Enter the path of the file
horse.txt
The horse starts at: (0.0, 0.0)
Command: Go     New position: ( 50.00,   0.00)  angle:   0
Command: Hah    New position: ( 50.00,   0.00)  angle:  10
Command: Go     New position: ( 99.24,   8.68)  angle:  10
Command: Gaff   New position: ( 99.24,   8.68)  angle:   0
Command: Go     New position: (149.24,   8.68)  angle:   0
Command: Gaff   New position: (149.24,   8.68)  angle: -10
Command: Go     New position: (198.48,   0.00)  angle: -10
Command: Go     New position: (247.72,  -8.68)  angle: -10
FAILURE