Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java程序中的主要方法重叠_Java - Fatal编程技术网

java程序中的主要方法重叠

java程序中的主要方法重叠,java,Java,这是完整的编码。一旦我们编译了它|它就会抛出之前已经定义的main方法的错误。如何解决这个错误。需要指导。多谢各位 import java.util.Scanner; import java.lang.*; import java.math.*; public class AdditiveCipher { public static void main(String[] args){ String message, encryptedMessage = "", decr

这是完整的编码。一旦我们编译了它|它就会抛出之前已经定义的main方法的错误。如何解决这个错误。需要指导。多谢各位

import java.util.Scanner;
import java.lang.*;
import java.math.*;

public class AdditiveCipher {
    public static void main(String[] args){
        String message, encryptedMessage = "", decryptedMessage = "", decryptMessage = "", encryptMessage = "";
        String text;
        int key;
        String keyy = "15";
        char ch;
        char cha;

        System.out.println("");
        System.out.println("-------------------Additive Cipher----------------------");
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a message: ");
        message = sc.nextLine();

        System.out.println("Enter key: ");
        key = sc.nextInt();

        for(int i = 0; i < message.length(); ++i){
            ch = message.charAt(i);

            if(ch >= 'a' && ch <= 'z'){
                ch = (char)(ch + key);

                if(ch > 'z'){
                    ch = (char)(ch - 'z' + 'a' - 1);
                }

                encryptedMessage += ch;
            }
            else if(ch >= 'A' && ch <= 'Z'){
                ch = (char)(ch + key);

                if(ch > 'Z'){
                    ch = (char)(ch - 'Z' + 'A' - 1);
                }

                encryptedMessage += ch;
            }
            else {
                encryptedMessage += ch;
            }
        }

        System.out.println("Encrypted Message using additive cipher = " + encryptedMessage);



//Additive decrypted codes


            Scanner sca = new Scanner(System.in);

            System.out.println("Enter a encryted message: ");
            text = sca.nextLine();



            for(int i = 0; i < text.length(); ++i){
                ch = text.charAt(i);

                if(ch >= 'a' && ch <= 'z'){
                    ch = (char)(ch - key);

                    if(ch < 'a'){
                        ch = (char)(ch + 'z' - 'a' + 1);
                    }

                    decryptedMessage += ch;
                }
                else if(ch >= 'A' && ch <= 'Z'){
                    ch = (char)(ch - key);

                    if(ch < 'A'){
                        ch = (char)(ch + 'Z' - 'A' + 1);
                    }

                    decryptedMessage += ch;
                }
                else {
                    decryptedMessage += ch;
                }
            }

            System.out.println("Decrypted Message using additive cipher = " + decryptedMessage);



    }


//Autokey encrypted codes

    private static String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static void main(String[] args){
        String keyy = "15";  //15 - P
        String textt = "HELLO";

    if(keyy.matches("[-+]?\\d*\\.?\\d+"))
         keyy = ""+alpha.charAt(Integer.parseInt(keyy));

        System.out.println("");
                System.out.println("-------------------Autokey Cipher----------------------");
                System.out.println("");

                String enc = AutoEncryption(textt,keyy);
                System.out.println("Enter a encryted message: " +textt);
                System.out.println("Encrypted Message using additive cipher = " + enc);

    }
        public static String AutoEncryption(String textt,String keyy){
        int len = textt.length();

             String subkey = keyy + textt;
             subkey = subkey.substring(0,subkey.length()-keyy.length());

                String sb = "";
                 for(int x=0;x<len;x++){
                         int get1 = alpha.indexOf(textt.charAt(x));
                         int get2 = alpha.indexOf(subkey.charAt(x));

                         int total = (get1 + get2)%26;

                         sb += alpha.charAt(total);
                }
            return sb;

        }

         public static String AutoDecryption(String text,String key){
              int len = text.length();

              String current = key;
              String sb ="";

              for(int x=0;x<len;x++){
                 int get1 = alpha.indexOf(text.charAt(x));
                 int get2 = alpha.indexOf(current.charAt(x));

                 int total = (get1 - get2)%26;
                 total = (total<0)? total + 26 : total;
                 sb += alpha.charAt(total);

                 current += alpha.charAt(total);
              }
              return sb;
           }



}

JVM开始从main执行java程序您无法在java中定义多个main方法99行中出现错误请检查代码

如果您确实希望所有内容都在一个文件中,或者其中一个具有不同的参数,您可以将第二个main放入另一个类中

public class Sample2 {

    public static void main(String[] args){
        // do some stuff........
    }

}

class SampleClass{
    public static void main(String[] args){
        // do some stuff........
    }
}
您也可以这样做:

public class Sample2{
    public static void main(String[] args){
         // do some stuff........
    }

    public static void main(byte b){    // whatever datatype you want
         // do some stuff........
    }

}

Faiz,这个消息告诉你在你的类中只能有一个主方法。Java在运行程序时以main方法开始,因此您需要决定要从哪个方法开始。可能为另一个选择一个不同的名称。@DawoodibnKareem我已经选择了一个不同的名称,但它不会改变任何东西。仍然存在错误可能与主方法之前已定义的错误重复。如何解决此错误不要定义同一方法两次。您有两个主要方法,正如错误所说的。鉴于它们的名称相同且具有相同的参数,您希望调用它们中的哪一个?为什么?答案是不知道,这也正是错误告诉您的,即系统不知道在这里要做什么,所以通过删除/重命名其中一个来修复它。