Ok, so I run this code and it give me the error:
SammysRentalPriceWithMethods.java:49: error: variable Minutes might not have been initialized
int TOTAL_COST = Minutes - 60 * 1 + 40;
^
I have no idea how to fix it, also I am sorry if my code is inefficient, I have only been on Java for 3 weeks, very much a beginnner.
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 + ".");
}
}
I get the error in my last method telling me that the varible Minutes isnt initialized, got any ideas?
2 Answers 2
I think you should try this:
int min = rentalTime();
companyMotto();
whenIGetMoney(min);
with this modification:
public static void whenIGetMoney(int min) {
final int HOURLY_RATE = 40;
final int EXTRA_MIN_RATE = 1;
int Minutes = min;
...
1 Comment
You can't use a local variable without initializing it first. Your Minutes variable isn't initialized and you try to use it. just declare it like int Minutes = 0; in the whenIGetMoney() method.
Anyway, the result won't be what you expect to be, as the variable Minutes has not the correct value in it.