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

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相关问答推荐

如何在使用 Stream 的同时将 Long 转换为 BigDecimal

使用 UNICODE_CHARACTER_CLASS 标志时的不同 Java 正则表达式匹配行为

不支持内容类型 multipart/mixed / 415 UNSUPPORTED_MEDIA_TYPE(Spring + Spring Boot 升级后)

@Query Java Spring Boot

请求 Spring Data JPA 存储库时指定复杂的排序标准

Springboot的@Controller和@RestController注解什么时候使用,底层概念是什么?

递归写入内存中的字符串:Java

Java 流不打印出预期值

如何总结具有特定 ID 的列表中对象的属性值并使用 Streams 将其分配给另一个对象

是否可以将字符串“1L”转换为相应的 Long 值?

如何在收到 2 个整数之前要求输入?

如何在单个元音前插入音节

让 Jackson 使用 GSON 注释

有效地改变 JSON 数据的结构

春天卡夫卡 |如何使 DeserializationException 可重试?

试图在java spring中调用/发布第三方api

使用方法返回的收集器时泛型类型不匹配 - Java 17

Java 8 DateTimeFormatter 解析在future 很远的 ISO 日期失败

防止Java中的一个永无止境的while循环

我们如何定义一个带有填充的列,并且只有一个项目不继承 Jetpack Compose 中列的填充?