Java:if语句中的错误变量作用域

Java:if语句中的错误变量作用域,java,variables,if-statement,compiler-errors,switch-statement,Java,Variables,If Statement,Compiler Errors,Switch Statement,问题: internet服务提供三种不同的订阅包: 套餐1:每月15.95美元,最多10小时服务。额外工时为每小时2美元。 套餐2:每月20.95美元,最多20小时服务。额外工时为每小时1美元。 套餐三:每月30.99美元,无限制使用 编写一个执行以下操作的程序: 询问用户他们有哪些计划 向用户询问他们正在计费的月份编号(1-12) 询问用户使用了多少小时 显示他们账单的成本 此外,还显示第1包客户如果购买第2包或第3包会节省多少钱,以及第2包客户如果购买第3包会节省多少钱 如果没有存款,请不要

问题:

internet服务提供三种不同的订阅包:

套餐1:每月15.95美元,最多10小时服务。额外工时为每小时2美元。 套餐2:每月20.95美元,最多20小时服务。额外工时为每小时1美元。 套餐三:每月30.99美元,无限制使用

编写一个执行以下操作的程序:

询问用户他们有哪些计划

向用户询问他们正在计费的月份编号(1-12)

询问用户使用了多少小时

显示他们账单的成本

此外,还显示第1包客户如果购买第2包或第3包会节省多少钱,以及第2包客户如果购买第3包会节省多少钱

如果没有存款,请不要显示此消息

输入验证:

计划编号不能为负数

计划编号必须是一个值[1,3]

小时数不能为负数

小时数不能超过给定月份的总小时数

30天的月份有720小时,31天的月份有744小时,28天的二月有672小时(不用担心闰年)

验证用户输入的值是否不大于给定月份的总小时数

见下表。

月、日、小时。

1744年1月31日

672年2月28日

744年3月31日

720年4月30日

744年5月31日

720年6月30日

744年7月31日

744年8月31日

720年9月30日

744年10月31日

720年11月30日

1744年12月31日

我的问题。

我的变量有问题。我知道有更好的方法来编写这个程序,但是您应该记住,这是我用Java编写代码的第四周

我使用了太多的if语句,当我试图编译它时;我得到12个变量错误。我假设变量的变量范围被某个if语句阻塞。因为编译器要求初始化变量,即使它们以前已经初始化过。 任何帮助都将不胜感激:)

/*
*作业04问题05
*学生:凯文·克雷斯平
*CIN:
*说明:此程序执行以下操作:
*询问用户他们有哪些计划。
*向用户询问他们正在计费的月份编号(1-12)。
*询问用户使用了多少小时。
*显示他们账单的成本。
*还显示如果购买套餐1,客户将节省多少钱
*套餐2或套餐3,以及套餐2的客户如果购买该产品会节省多少钱
*方案3。如果没有存款,请不要显示此消息。
*/
//导入扫描仪类
导入java.util.Scanner;
公共类HW04P05{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
//用户菜单
最终字符串用户菜单=
“\n”+\
“[1]套餐1:$15.95/月,最多10小时的服务。额外的小时数为$2.00/小时。\n”+
“[2]套餐2:$20.95/月,最多20小时的服务。额外的小时数为$1.00/小时。\n”+
“[3]包3:$30.99/月,无限制访问。\n”+
“\n”+
“输入[1-3]以选择您的包:”;
最终整数包_1=1;
最终整数包_2=2;
最终整数包_3=3;
系统输出打印(用户菜单);
int choice=input.nextInt();
System.out.println(“”);
如果(选项<0){
System.out.println(“菜单选项不能是负数,必须是值[1-3]”);
System.out.println(“程序将知道退出”);
系统出口(1);
}
else if(选项==0){
System.out.println(“菜单选项不能为零,必须是值[1-3]”);
System.out.println(“程序将知道退出”);
系统出口(1);
}
否则如果(选项>3){
System.out.println(“菜单选项必须是一个值[1-3]”);
System.out.println(“程序将知道退出”);
系统出口(1);
}
System.out.print(“[1]一月[4]四月[7]七月[10]十月[10]日\n”);
System.out.print(“[2]二月[5]五月[8]八月[11]十一月\n]日”);
System.out.print(“[3]三月[6]六月[9]九月[12]十二月[12]日\n”);
System.out.println(“”);
System.out.print(“输入[1-12]以选择计费月份:”;
int month=input.nextInt();
如果(月<0){
System.out.println(“月份不能为负数,必须是值[1-12]”);
System.out.println(“程序将知道退出”);
系统出口(1);
}
如果(月==0),则为else{
System.out.println(“月份不能为零,必须是值[1-12]”);
System.out.println(“程序将知道退出”);
系统出口(1);
}
否则,如果(月份>12){
System.out.println(“月份必须是一个值[1-12]”);
System.out.println(“程序将知道退出”);
系统出口(1);
}
双倍小时;
双倍储蓄1;
双储蓄2;
双倍总_1;
两倍的总量;
最终双倍总面积=15.95;
最终双倍总计=20.95;
最终双倍总计=30.99;
//用户菜单开关
开关(选择){
案例包_1:
如果((月==1)^(月==3)^(月==5)^(月==7)^(月==8)^(月==10)^(月==12)){
System.out.print(“输入使用计划包的小时数:”);
小时=输入。nextInt();
如果(小时数>744){
System.out.println(“错误:所选月份的小时数不能大于744”);
System.out.println(“现在程序将退出”);
/*
 * Homework 04 Problem 05
 * Student: Kevin Crespin
 * CIN: 
 * Description: This program does the following:
 * Ask the user which plan they have.
 * Ask the user for the month number (1-12) of which month they are being billed.
 * Ask the user how many hours they used.
 * Display the cost of their bill.
 * Also display how much money Package 1 customers would would have saved if they purchased
 * packages 2 or 3, and how much money Package 2 customers would have saved if they purchased
 * Package 3. If there were no savings, do not display this message.
 */
// import scanner class
import java.util.Scanner;

public class HW04P05{
public static void main (String[] args) {
    Scanner input = new Scanner(System.in);
    // USER MENU
    final String USER_MENU =
        " \n" +\
        "[1] Package 1: $15.95 a month for up to 10 hours of service. Additional hours are $2.00 per hour.\n" +
        "[2] Package 2: $20.95 a month for up to 20 hours of service. Additional hours are $1.00 per hour.\n" +
        "[3] Package 3: $30.99 per month unlimited access.\n" +
        " \n" +
        "Enter [1 - 3] for select your package: ";
    final int PACKAGE_1 = 1;
    final int PACKAGE_2 = 2;
    final int PACKAGE_3 = 3;
    System.out.print(USER_MENU);
    int choice = input.nextInt();
    System.out.println(" ");
    if (choice < 0) {
        System.out.println("The menu choice cannot be negative, must be a value [1 - 3]");
        System.out.println("The program will know exit.");
        System.exit(1);
    }
    else if (choice == 0) {
        System.out.println("The menu choice cannot be zero, must be a value [1 - 3]");
        System.out.println("The program will know exit.");
        System.exit(1);
    }
    else if (choice > 3) {
        System.out.println("The menu choice must be a value [1 - 3]");
        System.out.println("The program will know exit.");
        System.exit(1);
    }
    System.out.print("[1] January   [4] April  [7] July       [10] October\n");
    System.out.print("[2] February  [5] May    [8] August     [11] November\n");
    System.out.print("[3] March     [6] June   [9] September  [12] December\n");
    System.out.println(" ");
    System.out.print("Enter [1 - 12] for select billed month: ");
    int month = input.nextInt();
    if (month < 0) {
        System.out.println("The month cannot be negative, must be a value [1 - 12]");
        System.out.println("The program will know exit.");
        System.exit(1);
    }
    else if (month == 0) {
    System.out.println("The month cannot be zero, must be a value [1 - 12]");
    System.out.println("The program will know exit.");
    System.exit(1);
    }
    else if (month > 12) {
        System.out.println("The month must be a value [1 - 12]");
        System.out.println("The program will know exit.");
        System.exit(1);
    }
    double hours;
    double savings_1;
    double savings_2;
    double total_1;
    double total_2;
    final double total_1d = 15.95;
    final double total_2d = 20.95;
    final double total_3d = 30.99;
    // USER MENU SWITCH
    switch (choice) {
        case PACKAGE_1:
            if ((month == 1) ^ (month == 3) ^ (month == 5) ^ (month == 7) ^ (month == 8) ^ (month == 10) ^ (month == 12)) {
                System.out.print("Enter the number of hours the plan package was used: ");
                hours = input.nextInt();
                if (hours > 744) {
                    System.out.println("ERROR: The number of hours cannot be higher than 744 on the month selected.");
                    System.out.println("Now the program will exit.");
                    System.exit(1);
                }
                else if (hours < 0) {
                    System.out.println("ERROR: The number of hours cannot be negative.");
                    System.out.println("Now the program will exit.");
                    System.exit(1);
                }
                else {
                    if (hours < 10 && hours >= 1){
                        System.out.println("The cost of your bill is: $15.95");
                    }
                    else if (hours > 10 && hours <= 744) {
                        total_1 = ((hours - 10) * 2) + 15.95;
                        System.out.println("The cost of your bill is: $" + total_1);
                    }
                }
            }
            else if (month == 2) {
                System.out.print("Enter the number of hours the plan package was used: ");
                hours = input.nextInt();
                if (hours > 672) {
                    System.out.println("ERROR: The number of hours cannot be higher than 672 on February.");
                    System.out.println("Now the program will exit.");
                    System.exit(1);
                }
                else if (hours < 0) {
                    System.out.println("ERROR: The number of hours cannot be negative.");
                    System.out.println("Now the program will exit.");
                    System.exit(1);
                }
                else {
                    if (hours < 10 && hours >= 1){
                        System.out.println("The cost of your bill is: $15.95");
                    }
                    else if (hours > 10 && hours <= 672) {
                        total_1 = ((hours - 10) * 2) + 15.95;
                        System.out.println("The cost of your bill is: $" + total_1);
                    }
                }
            }
            else {
                System.out.print("Enter the number of hours the plan package was used: ");
                hours = input.nextInt();
                if (hours > 720) {
                    System.out.println("ERROR: The number of hours cannot be higher than 722 on the month selected.");
                    System.out.println("Now the program will exit.");
                    System.exit(1);
                }
                else if (hours < 0) {
                    System.out.println("ERROR: The number of hours cannot be negative.");
                    System.out.println("Now the program will exit.");
                    System.exit(1);
            }
                else {
                    if (hours < 10 && hours >= 1){
                        System.out.println("The cost of your bill is: $15.95");
                    }
                    else if (hours > 10 && hours <= 720) {
                        total_1 = ((hours - 10) * 2) + 15.95;
                        System.out.println("The cost of your bill is: $" + total_1);
                    }
                }
            }
        case PACKAGE_2:
            if ((month == 1) ^ (month == 3) ^ (month == 5) ^ (month == 7) ^ (month == 8) ^ (month == 10) ^ (month == 12)) {
                if (choice == 2) {
                System.out.print("Enter the number of hours the plan package was used: ");
                hours = input.nextInt();
                if (hours > 744) {
                    System.out.println("ERROR: The number of hours cannot be higher than 744 on the selected month.");
                    System.out.println("Now the program will exit.");
                    System.exit(1);
                }
                else if (hours < 0) {
                    System.out.println("ERROR: The number of hours cannot be negative.");
                    System.out.println("Now the program will exit.");
                    System.exit(1);
                }
                else {
                    if (hours < 20 && hours >= 1) {
                        System.out.println("The cost of your bill is: $20.95");
                    }
                    else if (hours > 20 && hours <= 744) {
                        total_2 = (hours - 20) + 20.95;
                        System.out.println("The cost of your bill is: $" + total_2);
                    }
                }
                }
                else {
                    if (hours < 20 && hours >= 1) {
                        System.out.println("The cost of package 2 bill is: $20.95");
                        savings_1 = total_1 - total_2d;
                        System.out.println("You could saved: $" + savings_1); 
                    }
                    else if (hours > 20 && hours <= 744) {
                        total_2 = (hours - 20) + 20.95;
                        System.out.println("The cost of package 2 bill is: $" + total_2);
                        savings_1 = total_1 - total_2d;
                        System.out.println("You could saved: $" + savings_1);
                    }
                }

            else if (month == 2) {
                if (choice == 2) {
                System.out.print("Enter the number of hours the plan package was used: ");
                hours = input.nextInt();
                if (hours > 672) {
                    System.out.println("ERROR: The number of hours cannot be higher than 672 on February.");
                    System.out.println("Now the program will exit.");
                    System.exit(1);
                }
                else if (hours < 0) {
                    System.out.println("ERROR: The number of hours cannot be negative.");
                    System.out.println("Now the program will exit.");
                    System.exit(1);
                }
                else {
                    if (hours < 20 && hours >= 1){
                        System.out.println("The cost of your bill is: $20.95");
                    }
                    else if (hours > 20 && hours <= 672) {
                        total_2 = (hours - 20) + 20.95;
                        System.out.println("The cost of your bill is: $" + total_2);
                    }
                }
                }
                else {
                    if (hours < 20 && hours >= 1) {
                        System.out.println("The cost of package 2 bill is: $20.95");
                        savings_1 = total_1 - total_2d;
                        System.out.println("You could saved: $" + savings_1); 
                    }
                    else if (hours > 20 && hours <= 672) {
                        total_2 = (hours - 20) + 20.95;
                        System.out.println("The cost of package 2 bill is: $" + total_2);
                        savings_1 = total_1 - total_2d;
                        System.out.println("You could saved: $" + savings_1);
                    }
                }
            }
            else {
                if (choice == 2) {
                System.out.print("Enter the number of hours the plan package was used: ");
                hours = input.nextInt();
                if (hours > 720) {
                    System.out.println("ERROR: The number of hours cannot be higher than 722 on the selected month.");
                    System.out.println("Now the program will exit.");
                    System.exit(1);
            }
                else if (hours < 0) {
                    System.out.println("ERROR: The number of hours cannot be negative.");
                    System.out.println("Now the program will exit.");
                    System.exit(1);
                }
                else {
                    if (hours < 20 && hours >= 1){
                        System.out.println("The cost of your bill is: $20.95");
                    }
                    else if (hours > 20 && hours <= 720) {
                        total_2 = (hours - 20) + 20.95;
                        System.out.println("The cost of your bill is: $" + total_2);
                    }
                }
                }
                else {
                    if (hours < 20 && hours >= 1) {
                        System.out.println("The cost of package 2 bill is: $20.95");
                        savings_1 = total_1 - total_2d;
                        System.out.println("You could saved: $" + savings_1); 
                    }
                    else if (hours > 20 && hours <= 720) {
                        total_2 = (hours - 20) + 20.95;
                        System.out.println("The cost of package 2 bill is: $" + total_2);
                        savings_1 = total_1 - total_2d;
                        System.out.println("You could saved: $" + savings_1);
                    }
                }
            }
        case PACKAGE_3:
            if ((month == 1) ^ (month == 3) ^ (month == 5) ^ (month == 7) ^ (month == 8) ^ (month == 10) ^ (month == 12)) {
                if (choice == 3) {
                System.out.print("Enter the number of hours you used your plan package: ");
            hours = input.nextInt();
                if (hours > 744) {
                    System.out.println("Enter the number of hours the plan package was used: ");
                    System.out.println("Now the program will exit.");
                    System.exit(1);
                }
                else if (hours < 0) {
                    System.out.println("ERROR: The number of hours cannot be negative.");
                    System.out.println("Now the program will exit.");
                    System.exit(1);
                }
                else {
                    System.out.println("The cost of your bill is: $30.99");
                }
                }
                else {
                    System.out.println("The cost of package 3 bill is: $30.99");
                    savings_2 = total_1 - total_3d;
                    System.out.println("You could saved: $" + savings_2);
                }
            }
            else if (month == 2) {
                if (choice == 3) {
                System.out.print("Enter the number of hours the plan package was used: ");
                hours = input.nextInt();
                if (hours > 672) {
                    System.out.println("ERROR: The number of hours cannot be higher than 672 on February.");
                    System.out.println("Now the program will exit.");
                    System.exit(1);
                }
                else if (hours < 0) {
                    System.out.println("ERROR: The number of hours cannot be negative.");
                    System.out.println("Now the program will exit.");
                    System.exit(1);
                }
                else {
                    System.out.println("The cost of your bill is: $30.99");
                }
                }
                else {
                    System.out.println("The cost of package 3 bill is: $30.99");
                    savings_2 = total_1 - total_3d;
                    System.out.println("You could saved: $" + savings_2);
                }
            }    
            else {
                if (choice == 3) {
                System.out.print("Enter the number of hours the plan package was used: ");
                hours = input.nextInt();
                if (hours > 720) {
                    System.out.println("ERROR: The number of hours cannot be higher than 720 on the selected month.");
                    System.out.println("Now the program will exit.");
                    System.exit(1);
                }
                else if (hours < 0) {
                    System.out.println("ERROR: The number of hours cannot be negative.");
                    System.out.println("Now the program will exit.");
                    System.exit(1);
                }
                else {
                    System.out.println("The cost of your bill is: $30.99");
                }
                }
                else {
                    System.out.println("The cost of package 3 bill is: $30.99");
                    savings_2 = total_1 - total_3d;
                    System.out.println("You could saved: $" + savings_2);
                }
            }
    }
}
}
HW04P05.java:176: error: variable hours might not have been initialized
                                        if (hours < 20 && hours >= 1) {
                                            ^
HW04P05.java:178: error: variable total_1 might not have been initialized
                                                savings_1 = total_1 - total_2d;
                                                            ^
HW04P05.java:184: error: variable total_1 might not have been initialized
                                                savings_1 = total_1 - total_2d;
                                                            ^
HW04P05.java:214: error: variable hours might not have been initialized
                                        if (hours < 20 && hours >= 1) {
                                            ^
HW04P05.java:216: error: variable total_1 might not have been initialized
                                                savings_1 = total_1 - total_2d;
                                                            ^
HW04P05.java:222: error: variable total_1 might not have been initialized
                                                savings_1 = total_1 - total_2d;
                                                            ^
HW04P05.java:252: error: variable hours might not have been initialized
                                        if (hours < 20 && hours >= 1) {
                                            ^
HW04P05.java:254: error: variable total_1 might not have been initialized
                                                savings_1 = total_1 - total_2d;
                                                            ^
HW04P05.java:260: error: variable total_1 might not have been initialized
                                                savings_1 = total_1 - total_2d;
                                                            ^
HW04P05.java:286: error: variable total_1 might not have been initialized
                                        savings_2 = total_1 - total_3d;
                                                    ^
HW04P05.java:310: error: variable total_1 might not have been initialized
                                        savings_2 = total_1 - total_3d;
                                                    ^
HW04P05.java:334: error: variable total_1 might not have been initialized
                                    savings_2 = total_1 - total_3d;
                                                ^
12 errors
public static void main(String[] args) {
    int plan = getPlanFromUser();
    int month = getMonthFromUser();
    int hours = getHoursFromUser(month);
    evaluateSavings(plan, month, hours);
}
import java.util.Scanner;

public class HW04P05 {

    private static final int PACKAGE_1 = 1;
    private static final int PACKAGE_2 = 2;
    private static final int PACKAGE_3 = 3;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        int plan = getPlan(input);
        int month = getMonth(input);
        int hours = getHours(input, month);
        calculateSavings(plan, hours);
    }

    private static int getPlan(Scanner input) {
        // packages menu
        System.out.println();
        System.out.println("[1] Package 1: $15.95 a month for up to 10 hours of service. Additional hours are $2.00 per hour.");
        System.out.println("[2] Package 2: $20.95 a month for up to 20 hours of service. Additional hours are $1.00 per hour.");
        System.out.println("[3] Package 3: $30.99 per month unlimited access.");
        System.out.println();
        System.out.print("Enter [1 - 3] for select your package: ");

        // get input
        int choice = input.nextInt();

        // input validation
        System.out.println(" ");
        if (choice < 0) {
            exitProgram("The menu choice cannot be negative, must be a value [1 - 3]");
        } else if (choice == 0) {
            exitProgram("The menu choice cannot be zero, must be a value [1 - 3]");
        } else if (choice > 3) {
            exitProgram("The menu choice must be a value [1 - 3]");
        }
        return choice;
    }

    private static int getMonth(Scanner input) {
        // month menu
        System.out.println("[1] January   [4] April  [7] July       [10] October");
        System.out.println("[2] February  [5] May    [8] August     [11] November");
        System.out.println("[3] March     [6] June   [9] September  [12] December");
        System.out.println("");
        System.out.print("Enter [1 - 12] for select billed month: ");

        // get input
        int month = input.nextInt();

        // input validation
        if (month < 0) {
            exitProgram("The month cannot be negative, must be a value [1 - 12]");
        } else if (month == 0) {
            exitProgram("The month cannot be zero, must be a value [1 - 12]");
        } else if (month > 12) {
            exitProgram("The month must be a value [1 - 12]");
        }
        return month;
    }

    private static int getHours(Scanner input, int month) {
        // hours menu
        System.out.println(" ");
        System.out.print("Enter the number of hours the plan package was used: ");

        // get input
        int hours = input.nextInt();

        // input validation
        if (hours < 0) {
            exitProgram("ERROR: The number of hours cannot be negative.");
        }
        if ((month ==  1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && hours > 744) {
            exitProgram("ERROR: The number of hours cannot be higher than 744 on the month selected.");
        }
        if ((month ==  4 || month == 6 || month == 9 || month == 11) && hours > 720) {
            exitProgram("ERROR: The number of hours cannot be higher than 720 on the month selected.");
        }
        if (month == 2 && hours > 672) {
            exitProgram("ERROR: The number of hours cannot be higher than 672 on the month selected.");
        }

        return hours;
    }

    private static void calculateSavings(int plan, int hours) {
        double pricePlan1 = calculatePricePlan1(hours);
        double pricePlan2 = calculatePricePlan2(hours);
        double pricePlan3 = 30.99;

        switch(plan) {
            case PACKAGE_1:
                System.out.println(String.format("The cost of your bill is: $%.2f", pricePlan1));
                if (pricePlan1 > pricePlan2) {
                    System.out.println(String.format("On package 2 you could have saved $%.2f", pricePlan1 - pricePlan2));
                }
                if (pricePlan1 > pricePlan3) {
                    System.out.println(String.format("On package 3 you could have saved $%.2f", pricePlan1 - pricePlan3));
                }
                break;

            case PACKAGE_2:
                System.out.println(String.format("The cost of your bill is: $%.2f", pricePlan2));
                if (pricePlan2 > pricePlan3) {
                    System.out.println(String.format("On package 3 you could have saved $%.2f", pricePlan2 - pricePlan3));
                }
                break;

            case PACKAGE_3:
                System.out.println(String.format("The cost of your bill is: $%.2f", pricePlan3));
                break;
        }
    }

    private static double calculatePricePlan1(int hours) {
        if (hours <= 10) {
            return 15.95;
        }

        return 15.95 + ((hours - 10) * 2);
    }


    private static double calculatePricePlan2(int hours) {
        if (hours <= 20) {
            return 20.95;
        }

        return 20.95 + (hours - 20);
    }

    private static void exitProgram(String reason) {
        System.out.println(reason);
        System.out.println("The program will now exit.");
        System.exit(1);
    }
}