调用main函数的方法时出现Java引用错误

时间:2013-09-06 09:07:35

标签: java methods reference static

嗨我有这个问题我在main函数中遇到这个错误:“非静态方法无法从静态上下文引用”所有调用的方法都带有此错误的下划线。我已经尝试了一些东西,我找不到克服它的方法,而且它正在努力。有没有办法可以摆脱这个错误?谢谢,这是代码:

import java.util.Scanner;

public class Survey {

int age;
char gender;
char show;
int over30MY = 0, over30FY = 0, under30MY = 0, under30FY = 0;
int over30MN = 0, over30FN = 0, under30MN = 0, under30FN = 0;
int totalCalls;
int totalWatchers;
float perTotalWatchers;
float perU30M, perU30F, perO30M, perO30F, perTotalF, perTotalM;
float perTotalU30, perTotalO30, perTotal;

public static void main(String[] args) {       

    System.out.println("Thank you for your call,\nPlease take some time to answer a few questions");
    Info();
    collectData();
    getTotals();
    printTotals();

}//end of main

public void Info() { //Prints student Info

    System.out.println("");
    System.out.println("");
    System.out.println("");
    System.out.println("");
    System.out.println("");
}

public void collectData() {

    Scanner userInput = new Scanner(System.in); //Does everything

    //ask questions
    System.out.println("What is your age?\n");
    age = userInput.nextInt();

    System.out.println("Male or Female (Enter M or Y)");
    gender = userInput.next().charAt(0);
    gender = Character.toLowerCase(gender);

    System.out.println("Do you watch the show regularly? (Enter Y or N)");
    show = userInput.next().charAt(0);
    show = Character.toLowerCase(show);

    //store data
    if((age > 30) && (gender == 'm') && (show == 'y')) {       
        over30MY++;             
    }
    else if((age > 30) && (gender == 'f') && (show == 'y')) {
        over30FY++;
    }
    else if((age < 30) && (gender == 'm') && (show == 'y')) {
        under30MY++;
    }
    else if((age < 30) && (gender == 'f') && (show == 'y')) {
        under30FY++;
    }
    else if((age > 30) && (gender == 'm') && (show == 'n')) {
        over30MN++;
    }
    else if((age > 30) && (gender == 'f') && (show == 'n')) {
        over30FN++;
    }
    else if((age < 30) && (gender == 'm') && (show == 'n')) {
        under30MN++;
    }
    else if((age < 30) && (gender == 'f') && (show == 'n')) {
        under30FN++;

    }//end of if else

}//end of collectData

    public void getTotals() {//calculate totals for printing

        totalCalls = over30MY + over30FY + under30MY + under30FY + over30MN + over30FN +      under30MN + under30FN;

        totalWatchers = over30MY + over30FY + under30MY + under30MN;

        perTotalWatchers = Math.round(totalWatchers / totalCalls); //rounds percentage to nearest whole number

        perU30F = Math.round(under30FY / totalWatchers); //Total Under 30 Females
        perU30M = Math.round(under30MY / totalWatchers); //Total Under 30 Males
        perO30F = Math.round(over30FY / totalWatchers); //Total Over 30 Females
        perO30M = Math.round(over30MY / totalWatchers); //Total Over 30 Males

        perTotalF = Math.round(perU30F + perO30F); //Total Females
        perTotalM = Math.round(perU30M + perO30M); //Total Males

        perTotalU30 = Math.round(perU30F + perU30M); //Total Under 30
        perTotalO30 = Math.round(perO30F + perO30M); //Total Over 30
        perTotal = Math.round(perTotalF + perTotalM); //Total 

    }//end of getTotals

    public void printTotals () { //Prints the Totals

        System.out.println("Total People called is "+totalCalls);
        System.out.println("Number of people who watch the show regularly is "+totalWatchers);
        System.out.println("Percentage of those who watch the show is "+perTotalWatchers);      

        System.out.println("");
        System.out.println("");

    }//end of printTotals

}// end of class

4 个答案:

答案 0 :(得分:3)

你不能从静态方法调用非静态方法。 你必须先创建它的实例。

public static void main(String[] args) {       

    System.out.println("Thank you for your call,\nPlease take some time to answer a few questions");
    Survey s = new Survey();
    s.Info();        
    s.collectData();
    s.getTotals();
    s.printTotals();

}//end 

答案 1 :(得分:0)

你正在调用非静态方法

Info();
    collectData();
    getTotals();
    printTotals(); 

在main方法中 这些都是非静态的,主要方法是静态方法。 要么将这些方法设置为静态,要么创建类的对象,然后调用这些方法

import java.util.Scanner;

public class Survey {

int age;
char gender;
char show;
int over30MY = 0, over30FY = 0, under30MY = 0, under30FY = 0;
int over30MN = 0, over30FN = 0, under30MN = 0, under30FN = 0;
int totalCalls;
int totalWatchers;
float perTotalWatchers;
float perU30M, perU30F, perO30M, perO30F, perTotalF, perTotalM;
float perTotalU30, perTotalO30, perTotal;

public static void main(String[] args) {       

    System.out.println("Thank you for your call,\nPlease take some time to answer a few questions");
    Survey s=new Survey();
   s. Info();
    s.collectData();
    s.getTotals();
   s. printTotals();

}//end of main

public void Info() { //Prints student Info

    System.out.println("");
    System.out.println("");
    System.out.println("");
    System.out.println("");
    System.out.println("");
}

public void collectData() {

    Scanner userInput = new Scanner(System.in); //Does everything

    //ask questions
    System.out.println("What is your age?\n");
    age = userInput.nextInt();

    System.out.println("Male or Female (Enter M or Y)");
    gender = userInput.next().charAt(0);
    gender = Character.toLowerCase(gender);

    System.out.println("Do you watch the show regularly? (Enter Y or N)");
    show = userInput.next().charAt(0);
    show = Character.toLowerCase(show);

    //store data
    if((age > 30) && (gender == 'm') && (show == 'y')) {       
        over30MY++;             
    }
    else if((age > 30) && (gender == 'f') && (show == 'y')) {
        over30FY++;
    }
    else if((age < 30) && (gender == 'm') && (show == 'y')) {
        under30MY++;
    }
    else if((age < 30) && (gender == 'f') && (show == 'y')) {
        under30FY++;
    }
    else if((age > 30) && (gender == 'm') && (show == 'n')) {
        over30MN++;
    }
    else if((age > 30) && (gender == 'f') && (show == 'n')) {
        over30FN++;
    }
    else if((age < 30) && (gender == 'm') && (show == 'n')) {
        under30MN++;
    }
    else if((age < 30) && (gender == 'f') && (show == 'n')) {
        under30FN++;

    }//end of if else

}//end of collectData

    public void getTotals() {//calculate totals for printing

        totalCalls = over30MY + over30FY + under30MY + under30FY + over30MN + over30FN +      under30MN + under30FN;

        totalWatchers = over30MY + over30FY + under30MY + under30MN;

        perTotalWatchers = Math.round(totalWatchers / totalCalls); //rounds percentage to nearest whole number

        perU30F = Math.round(under30FY / totalWatchers); //Total Under 30 Females
        perU30M = Math.round(under30MY / totalWatchers); //Total Under 30 Males
        perO30F = Math.round(over30FY / totalWatchers); //Total Over 30 Females
        perO30M = Math.round(over30MY / totalWatchers); //Total Over 30 Males

        perTotalF = Math.round(perU30F + perO30F); //Total Females
        perTotalM = Math.round(perU30M + perO30M); //Total Males

        perTotalU30 = Math.round(perU30F + perU30M); //Total Under 30
        perTotalO30 = Math.round(perO30F + perO30M); //Total Over 30
        perTotal = Math.round(perTotalF + perTotalM); //Total 

    }//end of getTotals

    public void printTotals () { //Prints the Totals

        System.out.println("Total People called is "+totalCalls);
        System.out.println("Number of people who watch the show regularly is "+totalWatchers);
        System.out.println("Percentage of those who watch the show is "+perTotalWatchers);      

        System.out.println("");
        System.out.println("");

    }//end of printTotals

}// end of class

注意 制作方法会导致你的问题,因为你需要将所有变量,如年龄,性别等变为static.So而不是将方法设为静态只是创建一个对象并调用这些方法

答案 2 :(得分:0)

这样做:

Survey s = new Survey();
s.collectData();
s.getTotals();
s.printTotals();

答案 3 :(得分:0)

collectData();
getTotals();
printTotals();

这些方法不是静态的,因此您无法在不启动的情况下进行访问。

您可以有两种可能的解决方案。首先制作这些方法static

  public static void Info() {
  }

第二

Survey  s=new Survey ();
s.collectData();
s.getTotals();
s.printTotals();

如上所述初始化并调用这些方法。