-1
我的代碼不斷收到此錯誤錯誤:無法對類型爲RationalDriver的非靜態方法InputMenu()或display_Menu()進行靜態引用。我調用了這個方法,甚至試圖讓它們變成靜態的,這沒有幫助。我如何解決這個代碼。我是新手,所以請解釋一下,或者明確告訴我該怎麼做。謝謝無法對非靜態方法進行靜態引用。我該如何解決這個問題
import java.util.Scanner;
import java.lang.String;
public class RationalDriver{
public static int rationalNum1, rationalDen1, rationalNum2, rationalDen2;
public static Rational r1, r2;
public static void main(String[] args){
Scanner in = new Scanner(System.in);
// first rational
System.out.println(" Input first rational number for the Numerator");
rationalNum1 = in.nextInt();
System.out.println(" Input first rational number for the Denominator");
rationalDen1 = in.nextInt();
if (rationalDen1 == 0){
System.out.println(" Cannont divide by zero ");
System.out.println(" please re enter another number ");
}
System.out.println("Rational Number #1 = ("+rationalNum1+"/"+rationalDen1+")"); //Displays 1st Rational Number
// second rational
System.out.println(" Input 2nd rational number for the 2nd Numerator");
rationalNum2 = in.nextInt();
System.out.println(" Input 2nd rational number for the 2nd Denominator");
rationalDen2 = in.nextInt();
if (rationalDen2 == 0){
System.out.println("Cannont divide by zero");
System.out.println(" please re enter another number");
}
System.out.println("Rational Number #2 = ("+rationalNum2+"/"+rationalDen2+")"); /// /Displays 2nd Rational Number
r1 = new Rational (rationalNum1, rationalDen1);
r2 = new Rational (rationalNum2, rationalDen2);
display_menu();
InputMenu() ;
}
// System.out.println;//toString
public void display_menu() //menu options
{
System.out.print(" Enter the corresponding number for the desired action ");
System.out.println("1) Addition\n2) 2) Subtraction\n3) 3) Multiplication\n4) 4)Division\n5) 5) Test for Eqaulity\n6) 6) Change 1st rational number\n7) 7) Change 2nd rational number");
}
public void question()
{
System.out.println("Do you want to exit? [y/n]"); // ask user if they want to quit with yes or no option
Scanner q = new Scanner(System.in);
switch (q.nextInt())
{
case 'y' :
System.out.println ("Thank you and goodbye.");
break;
case 'n' :
InputMenu();
break;
default:
System.err.println ("Unrecognized option");
break;
}
}
public void InputMenu() // keys for the menu
{
Scanner in = new Scanner(System.in);
display_menu();
int temp = in.nextInt();
switch (temp)
{
case 1: //addition
System.out.println ("1");
System.out.println("(" +rationalNum1+ "/" +rationalDen1+ ") + (" +rationalNum2+ "/" +rationalDen2+ ") = " +r1.add(r2));
break;
case 2: //subtraction
System.out.println ("2");
System.out.println("("+rationalNum1+"/"+rationalDen1+")"+ " - " + "("+rationalNum2+"/"+rationalDen2+")" + "=" + r1.subtract(r2));
break;
case 3: //mulitplication
System.out.println ("3");
System.out.println("("+rationalNum1+"/"+rationalDen1+")"+ " * " + "("+rationalNum2+"/"+rationalDen2+")" + "=" + r1.multiply(r2));
break;
case 4: //division
System.out.println ("4");
System.out.println("("+rationalNum1+"/"+rationalDen1+")"+ "/" + "("+rationalNum2+"/"+rationalDen2+")" + "=" + r1.divide(r2));
break;
case 5: //compare to
System.out.println ("5");
// question();
break;
case 6: //change the 1st Rational Number
System.out.println ("6");
System.out.println(" Input first rational number for the Numerator");
rationalNum1 = in.nextInt();
System.out.println(" Input first rational number for the Denominator");
rationalDen1 = in.nextInt();
if (rationalDen1 == 0){
System.out.println(" Cannont divide by zero");
System.out.println(" please re enter another number");
}
break;
case 7: //change the 2nd Rational Number
System.out.println ("7");
System.out.println(" Input 2nd rational number for the 2nd Numerator");
rationalNum2 = in.nextInt();
System.out.println(" Input 2nd rational number for the 2nd Denominator");
rationalDen2 = in.nextInt();
if (rationalDen2 == 0){
System.out.println("Cannont divide by zero");
System.out.println(" please re enter another number");
break;
}
default:
System.out.println ("Unrecognized option");
break;
}
}
}
你需要了解什麼'static'手段。 – SLaks
什麼是「非靜態」的意思。什麼是對象。 –
@HotLicks感謝 – user2921727