我一直收到错误,"The system couldn't find a suitable main method." - Java
I keep getting the error, "The system couldn't find a suitable main method." - Java
我正在为我的操作系统开发一个项目class,我们需要创建一个程序来读取 3 位整数的文本文件,解释整数的每一位以完成指定的任务,然后在执行结束时打印出各个寄存器的值。
我确定我的代码不完整 eloquent,但我现在遇到的主要问题是每次尝试 运行 它时,我都会收到错误 "The system couldn't find a suitable main method." 没有编译器错误。 Java 文件的名称是 "Assignment1.java"
感谢任何帮助。
import java.util.Scanner;
import java.io.*;
public class Assignment1
{
public static void main(String[] args) throws FileNotFoundException
{
try
{
int counter = 0;
int[] registers = new int[10]; // Create an array of integers to represent the instruction registers
for(int i = 0; i < registers.length; i++) // Initialize each register to 0
{
registers[i] = 0;
}
Scanner fileScan = new Scanner(new File("input1.txt"));
fileScan.useDelimiter(" ");
while(fileScan.hasNext())
{
number = fileScan.nextInt(); // Scan the instruction
String instr = number.toString(); // Convert the instruction to a String
if(instr.length()=3 && counter<=1000) // Verify no more than 1000 instructions will be executed and they are 3-digit numbers
{
executeInstr(instr);
}
}
System.out.println("Register 0: " + registers[0]);
System.out.println("Register 1: " + registers[1]);
System.out.println("Register 2: " + registers[2]);
System.out.println("Register 3: " + registers[3]);
System.out.println("Register 4: " + registers[4]);
System.out.println("Register 5: " + registers[5]);
System.out.println("Register 6: " + registers[6]);
System.out.println("Register 7: " + registers[7]);
System.out.println("Register 8: " + registers[8]);
System.out.println("Register 9: " + registers[9]);
System.out.println();
System.out.println("Number of instructions executed: " + counter);
}
catch(FileNotFoundException ex)
{
System.out.println("An error has occured. The file cannot be found.");
}
}
public static void executeInstr(String s) // method to implement instructions
{
instruction = s;
counter++;
int dig1 = (int)instruction.charAt(0); // convert first digit of the instruction to an integer variable
int dig2 = (int)instruction.charAt(1); // convert second digit of the instruction to an integer variable
int dig3 = (int)instruction.charAt(2); // convert third digit of the instruction to an integer variable
case 1: dig1=1
break;
case 2: dig1=2
registers[dig2]=dig3;
break;
case 3: dig1=3
registers[dig2]=registers[dig2]+dig3;
break;
case 4: dig1=4
registers[dig2]=registers[dig2]*dig3;
break;
case 5: dig1=5
registers[dig2]=dig3;
break;
case 6: dig1=6
registers[dig2]=dig2+dig3;
break;
case 7: dig1=7
registers[dig2]=registers[dig2]*dig3;
break;
case 8: dig1=8
break;
case 9: dig1=9
break;
case 10: dig1=0
break;
default:
System.out.println("Invalid entry.");
break;
}
}
我看到的一个问题是您在 main 方法中嵌套了 public static void executeInstr(String s)
。这是被禁止的。相反,将您的方法移动到 Assignment1
class 中,就像您的主要方法一样。
public class Assignment1 {
public static void executeInstr(String s) {
// your method logic here
}
public static void main(String[] args) {
// your main program code here
executeInstr(myString);
}
}
这应该为您编译并运行。
我正在为我的操作系统开发一个项目class,我们需要创建一个程序来读取 3 位整数的文本文件,解释整数的每一位以完成指定的任务,然后在执行结束时打印出各个寄存器的值。
我确定我的代码不完整 eloquent,但我现在遇到的主要问题是每次尝试 运行 它时,我都会收到错误 "The system couldn't find a suitable main method." 没有编译器错误。 Java 文件的名称是 "Assignment1.java" 感谢任何帮助。
import java.util.Scanner;
import java.io.*;
public class Assignment1
{
public static void main(String[] args) throws FileNotFoundException
{
try
{
int counter = 0;
int[] registers = new int[10]; // Create an array of integers to represent the instruction registers
for(int i = 0; i < registers.length; i++) // Initialize each register to 0
{
registers[i] = 0;
}
Scanner fileScan = new Scanner(new File("input1.txt"));
fileScan.useDelimiter(" ");
while(fileScan.hasNext())
{
number = fileScan.nextInt(); // Scan the instruction
String instr = number.toString(); // Convert the instruction to a String
if(instr.length()=3 && counter<=1000) // Verify no more than 1000 instructions will be executed and they are 3-digit numbers
{
executeInstr(instr);
}
}
System.out.println("Register 0: " + registers[0]);
System.out.println("Register 1: " + registers[1]);
System.out.println("Register 2: " + registers[2]);
System.out.println("Register 3: " + registers[3]);
System.out.println("Register 4: " + registers[4]);
System.out.println("Register 5: " + registers[5]);
System.out.println("Register 6: " + registers[6]);
System.out.println("Register 7: " + registers[7]);
System.out.println("Register 8: " + registers[8]);
System.out.println("Register 9: " + registers[9]);
System.out.println();
System.out.println("Number of instructions executed: " + counter);
}
catch(FileNotFoundException ex)
{
System.out.println("An error has occured. The file cannot be found.");
}
}
public static void executeInstr(String s) // method to implement instructions
{
instruction = s;
counter++;
int dig1 = (int)instruction.charAt(0); // convert first digit of the instruction to an integer variable
int dig2 = (int)instruction.charAt(1); // convert second digit of the instruction to an integer variable
int dig3 = (int)instruction.charAt(2); // convert third digit of the instruction to an integer variable
case 1: dig1=1
break;
case 2: dig1=2
registers[dig2]=dig3;
break;
case 3: dig1=3
registers[dig2]=registers[dig2]+dig3;
break;
case 4: dig1=4
registers[dig2]=registers[dig2]*dig3;
break;
case 5: dig1=5
registers[dig2]=dig3;
break;
case 6: dig1=6
registers[dig2]=dig2+dig3;
break;
case 7: dig1=7
registers[dig2]=registers[dig2]*dig3;
break;
case 8: dig1=8
break;
case 9: dig1=9
break;
case 10: dig1=0
break;
default:
System.out.println("Invalid entry.");
break;
}
}
我看到的一个问题是您在 main 方法中嵌套了 public static void executeInstr(String s)
。这是被禁止的。相反,将您的方法移动到 Assignment1
class 中,就像您的主要方法一样。
public class Assignment1 {
public static void executeInstr(String s) {
// your method logic here
}
public static void main(String[] args) {
// your main program code here
executeInstr(myString);
}
}
这应该为您编译并运行。