我创建了一个加密程序和一个解密程序,但是有完全独立的文件。我也使用了不同的变量名,有没有办法将它们组合起来而不必改变变量的所有名称等等?
加密代码:
import java.util.*;
public class Cipher //This class will encrpyt the program
{
public static char cipher (int j){ //this mehtod generates the random letter
char[] cipher1 = {'a','b','c','d','e','f','g'}; //characters for char array
j = (int) (Math.random() * cipher1.length);//choose a random element from the array
return cipher1[j]; //this will return the letter
} //end of cipher method
public static void main (String[] args){ //main method
System.out.print("Please type a sentence to be encrypted\n");//asks user for their word to be encrypted
Scanner inputScanner = new Scanner(System.in); //imports scanner reader
String userinput = inputScanner.next(); //assigns the word entered by user to varible userinput
userinput = userinput.toUpperCase(); //userinput in transferred to upper case letters
int yu = userinput.length(); //yu finds the length of charceters in userinput
char[] charArray = userinput.toCharArray(); //sends userinput to charArray
int w=1; //used for try catch block
System.out.println("please enter pattern"); //prompt for pattern
String pattern = inputScanner.next(); //pattern will decide how many characters will be input in the encrypted code
int pattern2 = Integer.parseInt(pattern); //changes the string value to integer value
do{
try{ //try block to catch if user enters letters or decimal numbers
w=2;
if(pattern2<0){
System.out.println("please enter a number above 0"); //prompt if user enters somthing below zero
w=1;
}
}catch (NumberFormatException f){
System.out.println("PLEASE ENTER A NUMBER!"); //prompt if user enters something user than a number
}
}while (w==1); //end of do and try catch block
System.out.print("your encrypted code is: "); //prompt to give user encrypted code
for(int i = 0; i < yu; i++){
System.out.print(charArray[i]);
for(int q = 0; q < pattern2; q++){
System.out.print( cipher(1));
} //end of for loop
} //end of for loop
} //end of main method
} //cipher class
解密代码:
import java.util.*;
public class unscrambler //This class will encrpyt the program
{
public static void main (String [] args){
int cip= 0;
String user ="";
System.out.println("Please enter the code to unscramble");
Scanner inputScanner = new Scanner(System.in); //imports scanner reader
String userinput = inputScanner.next();
char[] charArray = userinput.toCharArray(); //sends userinput to charArray
int j=charArray.length;
Character [] array = new Character[j];
for(int w=0; w<j; w++){
array[w] = charArray[w];
}
int a=1;
System.out.println("Please enter the number cipher pattern (an integer)");
do{
try{
user = inputScanner.next();
cip = Integer.parseInt(user);
a=2;
System.out.println("your code is ");
for(int w =0; w<j;){
System.out.println(charArray[j]);
w+=cip;
}
if(cip<=0){
System.out.println("please enter number greater than zero");
a=1;
}
}catch(NumberFormatException f){
System.out.println("please enter a proper number");
}
}while(a==1);
}
}
答案 0 :(得分:1)
据我所知,您可以使用以下步骤。
main
,并将代码内容作为单独的方法移动到第一个类中。main
。更具体地说,您的代码现在将如下所示:
public class Cipher {
public static char cipher (int j){
...
}
// Old main1
public static void encrypt() {
...
}
// Old main2
public static void decrypt() {
...
}
// One possible way to disambiguate, there are many others.
public static void main(String[] args) {
if (args[0].equals("encrypt") encrypt();
else decrypt();
}
}
请注意,我从您的两个旧电源中删除了String[] args
,因为您似乎没有使用它们。
答案 1 :(得分:0)
将它们保存为单独的文件。 1)在第一类重命名加密 2)重命名第二类主要解密 3)使用main方法创建一个新类。 a)在main方法中创建加密和解密对象 b)每当你需要Cipher.encypt()和descrambler.decrypt()
时调用它们加密和解密这两个类保持独立是一个很好的设计,您可以在以后重复使用而无需任何修改。