別のクラスの条件内から 1 つの Java クラスを初期化しようとしています - MarsRovers に Rover を初期化させたいのです。MarsRovers から Rover オブジェクトを初期化しようとすると、「シンボルが見つかりません」というエラーが表示されます。私はJavaが初めてなので、platauCoordsとinputLinesの範囲と関係があると感じています。ここで見た他の解決策を試しましたが、それらは私の問題に対して機能していません (変数を公開するなど)。
目標は、inputLines % 2 が 0 に等しい限り (until ループを使用して)、最終的に新しいローバーを作成することです。
火星探査機のコードは次のとおりです。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class MarsRover {
public static void main(String []args) throws FileNotFoundException {
Scanner console = new Scanner(System.in);
System.out.println("Mars rover is ready for input, please enter name of input file: ");
String filename = console.nextLine();
console.close();
List<String> inputLines = new ArrayList<String>();
Scanner scanner = new Scanner(new File(filename));
scanner.useDelimiter("\n");
while(scanner.hasNext()){
inputLines.add(scanner.next());
}
String plateauCoords = inputLines.get(0);
inputLines.remove(0);
scanner.close();
System.out.println(inputLines);
if(inputLines.size() % 2 == 0) {
MarsRover rover = new Rover(plateauCoords, inputLines);
} else {
System.out.println("Your directions are not formatted correctly");
}
}
}
ローバーのコードは次のとおりです。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class Rover {
public Rover(String platCoords, String[] input) {
System.out.println("INSIDE ROVER");
}
}
MarsRovers.java をコンパイルすると、次のエラーが発生します。
MarsRover.java:27: cannot find symbol
symbol : constructor Rover(java.lang.String,java.util.List<java.lang.String>)
location: class Rover
MarsRover rover = new Rover(plateauCoords, inputLines);
^
1 error