gpt4 book ai didi

java - 变量未初始化错误?

转载 作者:行者123 更新时间:2023-12-02 05:26:17 24 4
gpt4 key购买 nike

好的,所以我运行这段代码,它给了我错误:

SammysRentalPriceWithMethods.java:49: error: variable Minutes might not have been initialized
int TOTAL_COST = Minutes - 60 * 1 + 40;
^

我不知道如何修复它,如果我的代码效率低下,我也很抱歉,我只使用 Java 3 周,非常初学者。

import java.util.Scanner; 

public class SammysRentalPriceWithMethods {

public static void main(String[] args) {
rentalTime();
companyMotto();
whenIGetMoney();
}

public static int rentalTime() {
int Minutes;
Scanner inputDevice = new Scanner(System.in);
System.out.print("Enter total minutes equipment was rented:");
Minutes = inputDevice.nextInt();
return Minutes;
}

public static void companyMotto() {
System.out.println(

"SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS \r\n" +
"S S \r\n" +
"S S \r\n" +
"S S \r\n" +
"S S \r\n" +
"S S \r\n" +
"S Sammy's makes it fun in the sun S \r\n" +
"S S \r\n" +
"S S \r\n" +
"S S \r\n" +
"S S \r\n" +
"S S \r\n" +
"S S \r\n" +
"SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS \r\n");


}

public static void whenIGetMoney() {
final int HOURLY_RATE = 40;
final int EXTRA_MIN_RATE = 1;
int Minutes;

int TOTAL_COST = Minutes - 60 * 1 + 40;
System.out.println("You rented our equipment for " + Minutes + " minutes!");
System.out.println("The total cost of an " + Minutes + " minute rental is $" + TOTAL_COST + ".");
}

}

我在上一个方法中收到错误,告诉我变量 Minutes 未初始化,有什么想法吗?

最佳答案

我认为你应该尝试这个:

int min = rentalTime();
companyMotto();
whenIGetMoney(min);

经过此修改:

public static void whenIGetMoney(int min) {
final int HOURLY_RATE = 40;
final int EXTRA_MIN_RATE = 1;
int Minutes = min;
...

关于java - 变量未初始化错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25981803/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com