java - 更改使用 switch 语句的文本菜单上的输出

标签 java if-statement switch-statement output

使用 Java 我有一个系统类,它有不同的方法来显示计算机系统的详细信息。然后,我有一个测试系统类,它使用 switch 语句将这些方法作为菜单调用。如果用户输入选项 1 或 2,则会显示一些我已硬编码的输入。但如果他们选择选项三,那么他们就会被提升为输入自己的内容。

如果他们在选项 3 中输入,然后再次输入选项 1 和 2,我如何才能让选项 1 和 2 显示他们的输入而不是我的输入?

我对此完全是新手,我不确定我会如何做到这一点,我在网上和书籍中进行了研究,并在想也许在某个地方有一个 if else 语句,但我可能是错的。任何意见,将不胜感激。

这是我的代码:

import java.util.Scanner;

public class SystemTest_Y3881268 {

    public static void main(String[] args) {
        //Create System_Y3881268 object and test methods
        System_Y3881268 s=new System_Y3881268("Lenovo", 
                "Ideacentre A340-24IWL", 2);
        s.setHardDisk(2);
        s.setMemory(128);
        s.setPurchaseCost(599);

        //Create textual menu
        int memorySize;
        double hardDiskSize;

        @SuppressWarnings("resource")
        Scanner keyboard = new Scanner(System.in);
        char choice;
        do 
        {
            System.out.println();
            System.out.println("***** Computer system menu *****");
            System.out.println();
            System.out.println("Choice 1: Print System Details");
            System.out.println("Choice 2: Diagnose System");
            System.out.println("Choice 3: Set Details");
            System.out.println("Choice 4: Print System Properties");
            System.out.println("Choice 5: Quit the Program");
            System.out.println();
            System.out.println("Enter a number from 1 - 5");
            System.out.println();
            choice = keyboard.next().charAt(0);
            switch(choice) 
            {

            case '1': 
            {
                s.displayDetails();
            }
            break;

            case '2': 
            {
                s.diagnoseSystem();
            } 
            break;

            case '3': 
            {
                System.out.println("Enter hard disk size in GB: ");
                hardDiskSize = keyboard.nextDouble();
                if(hardDiskSize<2) 
                {
                    System.out.println("Hard disk size = Low");
                }

                else 
                {
                    System.out.println("Hard disk size = Ok");
                }

                System.out.println();
                System.out.println("Enter memory size in MB: ");
                memorySize = keyboard.nextInt();
                if(memorySize<128) 
                {
                    System.out.println("Memory Ok = False");
                }

                else 
                {
                    System.out.println("Memory Ok = True");
                }


            }
            break;

            case '4' : 
            {
                System_Y3881268.displaySystemProperties();
            }
            break;

            case '5' : break;
            default :  System.out.println("Enter only numbers from 1 - 5");
                       System.out.println();
            }
        } while(choice != '5');




    }

}

最佳答案

在情况 3 的 switch 语句中,您正在设置两个变量 HardDiskSize 和 memorySize 的值。

选择 1 或 2 时,您将打印 s.displayDetails() 和 s.diagnoseSystem。

在本例中,s 是您的“系统”。

现在,在查看案例 3 时,您不会直接更改“系统”中的任何内容,而只需更改两个变量的值。

但是,为了显示新尺寸,您的实际“系统”需要进行这些更改

所以像这样的事情就可以解决问题:

case '3': 
    {
        System.out.println("Enter hard disk size in GB: ");
        hardDiskSize = keyboard.nextDouble();
        s.setHardDisk(hardDiskSize); //setting the harddisk size in the "System"
        if(hardDiskSize<2) 
        {
            System.out.println("Hard disk size = Low");
        }

        else 
        {
            System.out.println("Hard disk size = Ok");
        }

        System.out.println();
        System.out.println("Enter memory size in MB: ");
        memorySize = keyboard.nextInt();
        s.setMemory(memorySize); //setting the new memory size within the "system"
        if(memorySize<128) 
        {
            System.out.println("Memory Ok = False");
        }

        else 
        {
            System.out.println("Memory Ok = True");
        }
    }

或者如果您只想在满足条件时更改尺寸:

case '3': 
    {
        System.out.println("Enter hard disk size in GB: ");
        hardDiskSize = keyboard.nextDouble();
        if(hardDiskSize<2) 
        {
            System.out.println("Hard disk size = Low");
        }

        else 
        {
            System.out.println("Hard disk size = Ok");
            s.setHardDisk(hardDiskSize); //setting the harddisk size in the "System" if it is ok
        }

        System.out.println();
        System.out.println("Enter memory size in MB: ");
        memorySize = keyboard.nextInt();
        if(memorySize<128) 
        {
            System.out.println("Memory Ok = False");
        }

        else 
        {
            s.setMemory(memorySize); //setting the new memory size within the "system" if it is ok
            System.out.println("Memory Ok = True");
        }
    }

关于java - 更改使用 switch 语句的文本菜单上的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58971220/

相关文章:

java - 何时在 Java 中使用 switch 语句

c - switch 语句是否比 for 循环更快?

javascript - 将 VBA 转换为 JavaScript 自定义函数,得到不同的答案

javascript- if/else,用于 switch 语句中的语句

java - 为什么 Object.equals() 的实现没有使用 hashCode()?

java - 如何使用 JComponent 返回多个 JTextField?

ruby-on-rails - 在 if block 中更改和编辑哈希的键

excel - 获取多个规模不同的组的组平均值

java - 函数中设置的变量不断变化

java - org.openqa.selenium.NoSuchElementException : no such element: Unable to locate element error using Selenium through Java