这是完整的编码。一旦我们编译它|它抛出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;
}
}
答案 0 :(得分:0)
JVM从main()开始执行java程序你无法在99行的java错误中定义多个main()方法请检查代码see this
答案 1 :(得分:0)
如果你真的想要一个文件中的所有内容或者其中一个有不同的参数,你可以把第二个主要文件放到另一个类中。
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........
}
}