获取用户int输入-这个类没有静态void main方法接受String[]



知道为什么我一直得到运行时错误吗?我是新来的,请对我宽容点。我试图接受用户输入生成一个非常简单的加密文件。我得到一个错误:

静态错误:该类没有静态void主方法接受String[].

我有一个主方法接受字符串[]!我迷路了。有什么建议吗?

import java.io.*;
import java.util.Scanner;

public class Encryption
{
public static void main(String[] args, String existing, String encrypted) throws IOException
   {
   boolean eof = false;
   int key = 10;
   Scanner scan = new Scanner(System.in);
   key = scan.nextInt();
 /* Your encryption program should work like a filter, reading the contents of one file...
 */
   FileInputStream inStream = new FileInputStream(existing);
   DataInputStream inFile = new DataInputStream(inStream);
   FileOutputStream outStream = new FileOutputStream(encrypted);
   DataOutputStream outFile = new DataOutputStream(outStream);
   while (!eof)
   {
      try
      {
         byte input = inFile.readByte();
 /* modifying the data into a code...
  */            
          input += key;
 /* and then writing the coded contents out to a second file.
 * The second file will be a version of the first file, but written in a secret code.
 */            
      outFile.writeByte(input);
      }
      catch (EOFException e)
      {
         eof = true;
      }
   }
  }
}

你的main方法只需要接受String[] args。在您的示例中,您已经加密并存在为字符串。如果main方法有不同的输入参数,JVM将无法识别它。

所以不用

main(String[] args, String existing, String encrypted)

你应该有

main(String[] args)

然后从args数组中获取所需的两个参数,并将它们赋值给具有相同名称的string。例如:

String existing = args[0];
String encrypted = args[1];

您也可以使用foreach循环收集这些

您的main方法需要使用仅包含String[] args的参数列表来定义。您还通过了String existing, String encrypted,这意味着JVM忽略或忽略了您的主方法。

看一下这篇文章,了解如何使用args参数传递这些参数。什么是&;String args[]&;?

你的方法接受String[], true,但它也接受其他参数,特别是String和String。

您的main方法必须具有public static void main(String args[])签名。

在Java中,方法根据它们的方法签名被调用。可以在网上找到很多关于这方面的信息,但本质上,方法是根据它们的名称和它们接受的参数来调用的。这意味着方法myMethod(int)myMethod(String)myMethod(int, int)都是不同的方法,可以单独调用(这顺便被称为方法重载)。

最新更新