我正在尝试创建一个包含菜单并将执行用户选择的任何选项的程序。我完成了这些方法并让它们编译,但不知道如何调用这些类。这是屏幕截图中的代码,因此更易于阅读:
极客类:(包含所有方法)
public class Geek{
private String name;
private int numberofQuestions=0;
public Geek (String name){
this.name = name;
numberofQuestions = 0;
}
public String getName(){
return name;
}
public int getnumberofQuestions(){
return numberofQuestions;
}
public boolean allTheSame(int num1, int num2, int num3){
numberofQuestions++;
if(num1 == num2 && num2 == num3 && num1 == num3){
return true;}
else return false;
}
public int sum (int num1, int num2){
numberofQuestions++;
int largest = Math.max(num1, num2);
int smallest = Math.min(num1, num2);
int result =0;
for (int i=smallest; i <= largest;i++){
result = result + i;}
return result;
}
public String repeat(String str, int n){
numberofQuestions++;
String repetition = "";
for (int j=0; j < n; j++){
repetition = repetition + str;}
return repetition;
}
public boolean isPalindrome(String str){
numberofQuestions++;
int n = str.length();
for( int i = 0; i < n/2; i++ )
if (str.charAt(i) != str.charAt(n-i-1)) return false;
return true;
}
}
主要的:
http://i.imgur.com/DvJ0LU5.png
编辑:我在这部分代码中遇到找不到符号错误:
case "d":
myGeek.sum(num1, num2, num3);
System.out.println("Enter the first number");
int num1 = scan.nextInt();
System.out.println("Enter the second number");
int num2 = scan.nextInt();
System.out.println("Enter the third number");
int num3 = scan.nextInt();
break;