如何在静态main方法中调用非静态方法? [Java]

时间:2019-02-24 00:46:25

标签: java methods getter-setter

因此,我目前正在参加Java初学者课程,我的教授周末不回复电子邮件,否则我会请他帮助。我们给了这个模板以完成代码,方法是根据用户的选择编写不同的方法进行不同的计算。我已经大功告成,但是对于每种方法,我尝试在main方法中调用(switch语句中的convertMoney和s​​electDay方法调用),我收到一条错误消息,内容为“无法从非引用方法convertMoney()静态上下文”(或调用任何方法)。如果我从main方法中删除了“ static”,则错误消失了,但由于找不到主类,该程序将无法运行。我还尝试将convertMoney和s​​electDay声明为静态,但这会创建大量其他错误消息。谁能指出我正确的方向?我想了解我在做什么错。 谢谢,下面的代码:

package choices;

import java.util.Scanner;

public class Choices {


public class Choices {

private int quarters;
private int dimes;
private int nickels;
private int pennies;
private static int weekDay;

public static void Choices() 
{
// initialize all attributes
quarters=0;
dimes=0;
nickels=0;
pennies=0;
weekDay=0;


}

//Write a setter for each attribute
public void setQuarters(int cents){ this.quarters = quarters; }
public void setDimes(int cents){ this.dimes = dimes; }
public void setNickels(int cents){ this.nickels = nickels; }
public void setPennies(int cents){ this.pennies = pennies; }
public void setWeekDay(int day){ this.weekDay = weekDay ;}

//Write a getter for each attribute
public int getQuarters() { return quarters; }
public int getDimes(){ return dimes; }
public int getNickels(){ return nickels; }
public int getPennies(){ return pennies; }
public int getWeekDay(){ return weekDay; }

// Complete the logic for the method 
public void convertMoney() {
Scanner in = new Scanner(System.in);
int cents = 0;

System.out.println("Welcome to the Change Calculator");
System.out.print("Enter the number of cents (0-99): ");
cents = in.nextInt(); /* capture the input */
if ( cents > 99 || cents < 0) /*test for less than 0 or greater than 99*/{
System.out.println(cents + " amount invalid.");
System.out.println("Value must be between 0 and 99.");
System.out.println("");
return;
}

setQuarters(cents / 25);
cents = cents % 25;
/*complete the code for dimes, nickles and pennies */
setDimes(cents / 10);
cents = cents % 10;

setNickels(cents / 5);
cents = cents % 5;

setPennies(cents / 1);
cents = cents % 1;

System.out.println("");
System.out.println("Quarters: " + getQuarters());

/* Complete the code for dimes, nickles and pennies*/
System.out.println("Dimes: " + getDimes());
System.out.println("Nickels: " + getNickels());
System.out.println("Pennies: " + getPennies());
System.out.println("");
}

public void selectDay() {
Scanner in = new Scanner(System.in);

System.out.println("Welcome to the Day Selector");
System.out.print("Enter a number for the day (1-7): ");
setWeekDay(in.nextInt());

switch (getWeekDay()) {
case 1:
System.out.println("Sunday: weekend");
break;
case 2:
System.out.println("Monday: weekday");
break;
/* complete the code for Tuesday to Saturday */
case 3:
System.out.println("Tuesday: weekday");
break;
case 4:
System.out.println("Wednesday: weekday");
break;
case 5:
System.out.println("Thursday: weekday");
break;
case 6:
System.out.println("Friday: weekday");
break;
case 7:
System.out.println("Saturday: weekend");
break;
default:
System.out.println("Invalid day");
}

}

public static void main(String[] args) {
String firstName;
String lastName;
int input;
Scanner in = new Scanner(System.in);
Choices Obj = new Choices();

System.out.print("Enter your first name: ");
firstName = in.nextLine();
System.out.print("Enter your last name : ");
lastName = in.nextLine();
System.out.println("Hello " + firstName + lastName );/* complete the code to             
print the first and last name*/ 
System.out.println("");

System.out.println("Press 1 - Calculate coins for change");
System.out.println("Press 2 - Determine the day of the week");
System.out.println("Press 3 - Both");
/* complete the code to display the info with Press 2 and Press 3 */
input = in.nextInt();

switch(input) {
case 1:
/* complete the code to convert the money */
convertMoney();
break;
case 2:
/* complete the code to call the method that evaluates the days of the week                         
*/
selectDay();   //HERE IS WHERE IM GETTING ERRORS
break;
case 3:
/* complete the code to call both methods */
convertMoney();    //AND ALSO GETTING ERRORS HERE
selectDay();
break;
default:
System.out.println(input + "is invalid");
}
System.out.println("");
System.out.println("GoodBye");
}

}

}

0 个答案:

没有答案