你需要的是一个主要的方法。这是一个入口点
public static void main(String[] args) {
//Your code here
}
在你的情况下,它可能看起来像:
package feetToInches2;
public class Feet2Inches {
public double feetToInches_ (double feet) {
return 12* feet;
}
public int max(int val1, int val2) {
if (val1 > val2) {
return (val1);
}
else {
return (val2);
}
}
public static void main(String[] args) {
Feet2Inches converter=new Feet2Inches();
int max = converter.max(100,50);
double inches = converter.feetToInches_(10.5);
System.out.println("Max value = " + max);
System.out.println("Inches = " + inches);
}
}
我已经改变了你的方法修饰符,所以它会更有用。没有公共方法的类是不需要的类;)我还修复了您代码中的一些错误。
编辑:
如果您想让人们使用您的代码进行自己的计算,您有两种选择。您可以从 args 数组中获取参数,该数组包含程序运行时使用的参数。或者您可以在运行时提示输入数据。
1 使用参数
public static void main(String[] args) {
Feet2Inches converter = new Feet2Inches();
if (args.length != 3) {
System.err.println("Missing arguments! give me three numbers");
System.exit(1);//error exit
}
int val1 = Integer.valueOf(args[0]);
int val2 = Integer.valueOf(args[1]);
double val3 = Double.valueOf(args[1]);
int max = converter.max(val1, val2);
double inches = converter.feetToInches_(val3);
System.out.println("Max value from (" + val1 + "," + val2 + ")= " + max);
System.out.println(val3 + "Feet = " + inches + " inches");
}
现在您必须使用参数Here 调用您的程序,您可以从 cmd 中查看如何执行此操作,Here 是在 Eclipse 中执行此操作的方法
2 在运行时询问用户参数
public static void main(String[] args) {
Feet2Inches converter = new Feet2Inches();
Scanner input = new Scanner(System.in);
System.out.print("Argument 1:");
int val1 = input.nextInt();
System.out.print("Argument 2:");
int val2 = input.nextInt();
System.out.print("Argument 3:");
double val3 = input.nextDouble();
int max = converter.max(val1, val2);
double inches = converter.feetToInches_(val3);
System.out.println("Max value from (" + val1 + "," + val2 + ")= " + max);
System.out.println(val3 + "Feet = " + inches + " inches");
}