【问题标题】:Getting user int input - This class does not have a static void main method accepting String[] [duplicate]获取用户int输入-此类没有接受String []的静态void main方法[重复]
【发布时间】:2016-01-04 23:24:38
【问题描述】:

任何想法为什么我不断收到运行时错误?我很新,请放轻松。我正在尝试接受用户输入以生成非常简单的文件加密。我收到一个错误:

静态错误:此类没有接受 String[] 的静态 void main 方法。

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

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;
      }
   }
  }
}

【问题讨论】:

    标签: java class input java.util.scanner main


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

      查看这篇文章,了解如何使用 args 参数传递这些参数。 What is "String args[]"? parameter in main method Java

      【讨论】:

        【解决方案3】:

        您的 main 方法只需要使用 String[] 参数。在您的示例中,您已加密并作为字符串存在。如果输入参数不同,JVM 将无法识别您的 main 方法。

        所以不是

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

        你应该有

        main(String[] args)
        

        然后从 args 数组中获取您需要的两个参数,并将它们分配给具有相同名称的字符串。例如:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-12-16
          • 2018-03-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-11-04
          • 2017-02-25
          相关资源
          最近更新 更多