import java.util.Scanner;
public class data {
public static int setoriginal() { //prompt user to input 4 digit number
int n = 0;
int l = 0;
do {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a 4 digit number: ");
n = input.nextInt();
if (n >= 1000 && n <= 9999) {
break;
} else {
System.err.
printf("You did not enter 4 digits\n");
}
l = l + 1;
}
while (l < 3);
return n;
} //end method
public static int encrypt(int n) { //encrypt number inputted by user
int e1 = (((n / 1000) + 7) / 10);
int e2 = ((((n % 1000) / 100) + 7) / 10);
int e3 = ((((n % 100) / 10) + 7) / 10);
int e4 = ((((n % 10) / 1) + 7) / 10);
int e = e1 * 1000 + e2 * 100 + e3 * 10 + e4;
int firstPart = e % 100;
int lastPart = e / 100;
int result = firstPart * 100 + lastPart;
return result;
} //end method
public static int decrypt(int result) { //decrypt encrypted number
int d1 = (((result / 1000) - 7) * 10);
int d2 = ((((result % 1000) / 100) - 7) * 10);
int d3 = ((((result % 100) / 10) - 7) * 10);
int d4 = ((((result % 10) / 1) - 7) * 10);
int d = d1 * 1000 + d2 * 100 + d3 * 10 + d4;
int firstPart1 = d % 100;
int lastPart1 = d / 100;
int result1 = firstPart1 * 100 + lastPart1;
return result1;
} //end method
public static int decrypt1(int n) { //decrypted number inputted by user
int dd1 = (((n / 1000) - 7) * 10);
int dd2 = ((((n % 1000) / 100) - 7) * 10);
int dd3 = ((((n % 100) / 10) - 7) * 10);
int dd4 = ((((n % 10) / 1) - 7) * 10);
int dd = dd1 * 1000 + dd2 * 100 + dd3 * 10 + dd4;
int firstPart1 = dd % 100;
int lastPart1 = dd / 100;
int result1 = firstPart1 * 100 + lastPart1;
return result1;
} //end method
public static void display(int n, int result, int result1) { //output results
System.out.println("Originl number is: " + n);
System.out.println("\nEcrypted numebr is: " + result);
} //end method
public static void display1(int n, int result, int result1) { //output results
System.out.println("Originl number is: " + n);
System.out.println("\nDecrypted numebr is: " + result1);
} //end method
public static void getOriginal(int n) { //return original number
System.out.println("The original number is: \n" + n);
} //end method
public static void getEncrypt(int n, int result) { //return encrypted number
System.out.println("The encrypted number is: \n" + result);
} //end method
public static void main(String[]args, int result, int n, int result1) {
int m = 0;
Scanner input1 = new Scanner(System.in);
System.out.print("\nPlease choose from the following menu ");
System.out.print("\n1. Enter an original number");
System.out.print("\n2. Encrypt the number and print it");
System.out.print("\n3. Decrypt a number and print it");
System.out.print("\n4. Quit\n");
m = input1.nextInt();
while (m < 1 || m > 4) {
System.out.print("Error choose a number from 1-4");
m = input1.nextInt();
}
if (m == 1) {
setoriginal();
main(args, m, m, m);
}
else if (m == 2) {
if (setoriginal() == 0) {
System.out.
print
("Please enter an original number first");
main(args, m, m, m);
} else {
encrypt(n);
display(n, result, result1);
main(args, n, result, result1);
}
} else if (m == 3) {
if (encrypt(n) < 0) {
System.out.
print("Please encrypt your number first");
main(args, n, result, result1);
} else {
decrypt(n);
display1(n, result, result1);
main(args, n, result, result1);
}
} else if (m == 4) {
System.exit(0);
}
}
}
我在eclipse中没有编译错误,但是我收到一条错误,指出“选择不包含主类型”。什么想法可能是错的?如果您发现任何其他错误,请告诉我。
答案 0 :(得分:5)
您应该使用:
public static void main(String[] args)
而不是:
public static void main(String[] args, int result, int n, int result1)
答案 1 :(得分:1)
public static void main(String[]args, int result, int n, int result1) {
你需要学习做Java编程的一件事就是咒语:
public static void main(String args[]) {
正是这样(嗯,String[] args
也起作用 - 并且名称无关紧要)JVM在查找运行程序的入口点时搜索的咒语。
<强>更新强>
如果您想从main()
函数中获取任何参数,它们将被编码为String
数组中的args[]
个对象。只需索引它们:
$ cat Echo.java ; javac Echo.java ; java Echo Hello Cruel World
import java.lang.System;
class Echo {
public static void main(String args[]) {
for(int i=0; i<args.length; i++) {
System.out.println(args[i]);
}
}
}
Hello
Cruel
World
$
当然,它们是String
个对象。如果需要将它们转换为其他对象类,则必须正确解析字符串。 (Integer()
和类似的事情对于简单的案例可以派上用场。)
答案 2 :(得分:1)
为了运行程序,java需要能够找到具有以下签名的方法:
public static void main(String[])
由于它无法在程序中找到它,因此无法开始执行,因此无法编译它。
要获得所需的效果,请将main方法的签名更改为上述方法,并根据String []的索引访问参数,如下所示:
result = Integer.parseInt( args[0] );
n = Integer.parseInt( args[1] );
result1 = Integer.parseInt( args[2] );
如果你将它添加到主方法的开头,你应该没问题。 :d