我觉得这很简单,但我无法让它发挥作用。我想用这个:
import java.util.Scanner;
import java.lang.Math;
public class SammysRentalPriceWithMethods
{
public static void main(String[] args)
{
Rental rental = new Rental();
SammysRentalPriceWithMethods SRPWM = new SammysRentalPriceWithMethods();
getLogo();
getContractNum();
getHoursAndMinutes();
}
public static void getLogo()
{
rental.setlogo();
}
public static void getContractNum()
{
rental.setContractNumber();
}
public static void getHoursAndMinutes()
{
rental.setHoursAndMinutes();
}
}
调用此类及其中包含的方法:
import java.util.Scanner;
import java.lang.Math;
public class Rental
{
public final int minutes = 60;
public final double hourlyRate = 40.0;
private static String contractNum;
private static double hours;
private static int minutes2;
private static double price;
Scanner Input = new Scanner(System.in);
public static void setlogo()
{
System.out.println("*********************************");
System.out.println("*Sammy's makes it fun in the sun*");
System.out.println("*********************************");
}
public static void setContractNumber()
{
Scanner Input = new Scanner(System.in);
System.out.println("Please enter your contract number.");
contractNum = Input.nextLine();
}
public static void setHoursAndMinutes()
{
int timeOver;
Scanner Input2 = new Scanner(System.in);
System.out.println("Please enter the amount of time in minutes you rented the equipment.");
minutes2 = Input2.nextInt();
if (minutes2 > 60)
{hours = (minutes2/60);
price = (hours * 40);
timeOver = (minutes2%60);
price = (price + timeOver);
System.out.println("You rented the equipment for " + hours + " hours and " + timeOver + " minutes.");
System.out.println("Your total price is: " + price);
}
else if (minutes2 < 60)
{
price = (minutes2 * 1);
System.out.println(price);
}
}
}
但编译器在SRPWM类的每个租借参考上都说“错误:找不到符号”。我已经在main方法中调用了类。有什么想法吗?
答案 0 :(得分:1)
编译器是对的。
变量租赁和SRPWM的范围仅限于主要方法。 您可以将属性传递给类的方法,也可以将它们设置为SammysRentalPriceWithMethods的静态字段。
答案 1 :(得分:0)
问题是您正在混合static
和非static
成员并在SammysRentalPriceWithMethods
类中调用它们,因此请按以下代码更改类,并注释:< / p>
public class SammysRentalPriceWithMethods {
private Rental rental;
//Use constructor to inject Rental object
public SammysRentalPriceWithMethods(Rental rental) {
this.rental = rental;
}
public static void main(String[] args) {
//create Rental object
Rental rental = new Rental();
SammysRentalPriceWithMethods srpwm =new SammysRentalPriceWithMethods();
//invoke methods using srpwm reference
srpwm.getLogo();
srpwm.getContractNum();
srpwm.getHoursAndMinutes();
}
public void getLogo() {
rental.setlogo();
}
public void getContractNum() {
rental.setContractNumber();
}
public void getHoursAndMinutes() {
rental.setHoursAndMinutes();
}
}
您需要记住以下基础知识:
(1)使用classname和.
运算符调用类的静态成员(如果要在静态方法中调用静态成员,在没有classname的情况下调用它们。)
(2)使用对象和.
运算符
(3)将变量,方法名称命名为camel case(以小写字母开头的字母)
答案 2 :(得分:0)
由于rental
方法中声明了main
,因此只能在该方法中显示{
"Comment": "A simple example of the Amazon States Language using an AWS Lambda Function",
"StartAt": "Lambda_A",
"States": {
"Lambda_A": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"Next": "Lambda_B"
},
"Lambda_B":{
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"End": true
}
}
}
。您应该考虑在类级别声明此变量。
答案 3 :(得分:0)
您需要在 main()函数之外声明 Rental 类变量。如果您在 main()中声明它,那么您将无法在其他功能中使用它。因此,请将租借变量设为全局。
新文件应为:
import java.util.Scanner;
import java.lang.Math;
public class SammysRentalPriceWithMethods {
Rental rental = new Rental();
public static void main(String[] args) {
SammysRentalPriceWithMethods SRPWM = new SammysRentalPriceWithMethods();
getLogo();
getContractNum();
getHoursAndMinutes();
}
public static void getLogo() {
rental.setlogo();
}
public static void getContractNum() {
rental.setContractNumber();
}
public static void getHoursAndMinutes() {
rental.setHoursAndMinutes();
}
}