我试图比较三种方案下用户使用手机的成本

import java.util.Scanner;

public class MainMenuSelection {
    
    public static int menuSel() {       
    int menuSel ;       
    System.out.println("ENTER USAGE DETAILS MENU\n");
    System.out.println("Please select an option from the menu:");
    System.out.println("1.Phone Call");
    System.out.println("2.SMS ");
    System.out.println("3.Data usage");
    System.out.println("4. Return to main menu");
    
    
    Scanner in = new Scanner(System.in);    
    System.out.println("Enter selection: ");        
    menuSel = in.nextInt();
    
    while (menuSel < 1 || menuSel >4){
          System.out.println("Value must bebetween 1 and 4, plese try again.");
          System.out.println("Enter your selection: ");
          menuSel = in.nextInt();
        }
    
    return menuSel;
                
}
public static int mainMenuSelection() {

    System.out.println("MAIN MENU\n");
    System.out.println("Please select from the menu:");
    System.out.println("1. Enter Usage details");
    System.out.println("2. Display Cost under provider 1");
    System.out.println("3. Display Cost under provider 2");
    System.out.println("4. Display Cost under provider 3");
    System.out.println("5. Clear usage details");
    System.out.println("6. Exit System");
    
    int mainMenuSelection;
    Scanner userInput = new Scanner(System.in);
    System.out.println("Enter your selection: ");
    mainMenuSelection = userInput.nextInt();
    
    //return mainMenuSelection;
    
    while (mainMenuSelection < 1 || mainMenuSelection >6){
          System.out.println("Value must bebetween 1 and 6, plese try again.");
          System.out.println("Enter your selection: ");
          mainMenuSelection = userInput.nextInt();
        }
            
    System.out.println("You chose selection " + mainMenuSelection + "\n");
    return mainMenuSelection;
    
}

    public static void main(String[] args) {
        System.out.println("Hello World!");

        int callLength = 0;
        int numSMS = 0;
        int numberOfMB = 0;

        int numberofCalls;
        int callTime;
        int totalcallTime = 0;

        int mainMenuSelection = mainMenuSelection();

        if (mainMenuSelection == 1) {

            int menuSel = menuSel();
            if (menuSel == 1) {

                Scanner in = new Scanner(System.in);
                System.out.println("Enter the number of phone call you have made? ");
                numberofCalls = in.nextInt();

                for (int i = 0; i < numberofCalls; i++) {
                    Scanner callInput = new Scanner(System.in);
                    System.out.println("How long was the call in seconds?");
                    callTime = callInput.nextInt();

                    totalcallTime = callTime + totalcallTime;

                }
                System.out.println("Your total number of calls is " + numberofCalls);
                System.out.println("Your total call time is " + totalcallTime + " seconds" + "\n");

            } else if (menuSel == 2) {
                System.out.println("Enter the number of SMS messages: ");
                //int numSMS;
                Scanner userNumSMS = new Scanner(System.in);
                numSMS = userNumSMS.nextInt();

                System.out.println("Your number of SMSs is " + numSMS + "\n");

            } else if (menuSel == 3) {
                System.out.println("Enter the amount of data in MB: ");
                //int numberOfMB;
                Scanner userNumMB = new Scanner(System.in);
                numberOfMB = userNumMB.nextInt();

                System.out.println("Your amount of data in MB is " + numberOfMB + "\n");

            } else {
                mainMenuSelection(); // go back to main menu

            }

        } else if (mainMenuSelection == 2) {
            //Display cost under provider 1

            double callCost = 0.03 * totalcallTime;
            double smsCost = 0.10 * numSMS;
            double dataCost = 0.02 * numberOfMB;
            double totalCostProvider1 = callCost + smsCost + dataCost;

            System.out.println(totalCostProvider1);

        } else if (mainMenuSelection == 3) {
            //Display cost under provider 2
            double callCost2 = 0.04 * totalcallTime;
            double smsCost2 = 0.12 * numSMS;
            double dataCost2 = 0.04 * numberOfMB;
            double totalCostProvider2 = callCost2 + smsCost2 + dataCost2;
            System.out.println(totalCostProvider2);

        } else if (mainMenuSelection == 4) {
            //Display cost under provider 3
            double callCost3 = 0.05 * totalcallTime;
            double smsCost3 = 0.11 * numSMS;
            double dataCost3 = 0.03 * numberOfMB;
            double totalCostProvider3 = callCost3 + smsCost3 + dataCost3;
            System.out.println(totalCostProvider3);

        } else if (mainMenuSelection == 5) {
            //clear usage details 

        } else {
            System.out.println("Exiting System");
            System.exit(0);

        }

        mainMenuSelection = mainMenuSelection();
    }
}

推荐答案

代码从上到下运行.因此,在运行int mainMenuSelection = mainMenuSelection();if-else语句后,代码将再次为mainMenuSelection = mainMenuSelection()运行并结束,因为块中没有剩余内容.

一个相当简单的解决方案是使用whiledo-while循环,并通过布尔值判断用户是否输入了6以退出系统.

您可以try 以下示例:

public static void main(String[] args) {  
    /* 
     * Your variables here
     */
    boolean isExit = false;

    while(!isExit) {
        int mainMenuSelection = mainMenuSelection();

        if (mainMenuSelection == 1) {
                // menuSel codes here
        } else if (mainMenuSelection == 2) {
                // Display cost under provider 1
        } else if (mainMenuSelection == 3) {
                // Display cost under provider 2
        } else if (mainMenuSelection == 4) {
                // Display cost under provider 3
        } else if (mainMenuSelection == 5) {
                // Clear usage details
        } else {
            System.out.println("Exiting System");
            isExit = true;
        }

    }

希望这个答案对你有帮助.

Java相关问答推荐

将linkedHashMap扩展到Java中的POJO类

Java:根据4象限中添加的行数均匀分布行的公式

Java WireMock定义存根在Cucumber并行执行的多线程测试中失败

为什么我们仍然需要实现noArgsConstructor如果Java默认提供一个非参数化的构造函数?''

在spring—data中自动发现native—sql查询期间遇到重复的SQL别名[id]

无法在org. openjfx:javafx—fxml:21的下列变体之间进行 Select

有关手动创建的包的问题

暂停计时器

在Java 15应用程序中运行Java脚本和Python代码

如何在Spring Boot中创建可以将值传递给配置的&Enable&Quot;注释?

如何使这两种方法合二为一?

在Oracle db中,当我们提供字符串而不是数字时,比较是如何工作的?

如何从命令行编译包中的所有类?

Win32函数的JNA绑定DwmGetColorizationColor返回E_INVALIDARG错误

无法在IntStream上应用Collectors.groupingBy

为什么没有加载java.se模块?

在单例类上获取Java锁,了解原因

Java CDI:@Singleton@Startup@Inject无法实现接口

ResponseEntity.控制器截断响应的JSON部分

BigDecimal stripTrailingZeros 和相等