【问题标题】:How to count the total of items sold? then print it out when choosen如何计算已售出的商品总数?然后在选择时打印出来
【发布时间】:2022-11-13 10:51:49
【问题描述】:

我是一名 IT 新生,如果用户输入 5,我们被要求创建一个程序来确定售出的物品总数,我看了几个 youtube 教程,但我不能理解它..任何帮助请。

该程序应显示 3 个选项,然后用户输入他/她的选项。之后,如果用户选择 5,则该程序应打印出已售出的商品数量。

import java.util.Scanner;
public class Machine{
    public static void main(String[] args) {

        int choice;
        Scanner scan = new Scanner(System.in);

        do {
            System.out.println("[1] Get Gum");
            System.out.println("[2] Get Chocolate");
            System.out.println("[3] Get Popcorn");
            System.out.println("[4] Get Juice");
            System.out.println("[5] Display total sold items so far");
            System.out.println("[6] Quit");
            System.out.println("Your choice: ");
            choice = scan.nextInt();

            if (choice == 1 ) {
                System.out.println("Here is your Gum ");
            }
            else if (choice == 2 ) {
                System.out.println("Here is your Chocolate ");
                }
            else if (choice == 3 ) {
                System.out.println("Here is your Popcorn ");
                }
            else if (choice == 4 ) {
                System.out.println("Here is your Juice ");
                }

            else if (choice == 5) {
                System.out.println("3 items of gums sold");
                System.out.println("2 items of chocolate sold");
                System.out.println("6 items of popcorn sold");
                System.out.println("9 items of juice sold");
            }

            else if (choice== 6)
            System.out.println("Stop"); 

            else
            System.out.println("Error, Option 1-6 only");

        }while (choice<6);

    }
}

【问题讨论】:

    标签: java


    【解决方案1】:

    我不是 Java 编码员,但我认为问题可能是您使用的是等于 (==) 与严格等于 (===),这意味着只要提供的值不是 false/0,== 1 就是 true。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      相关资源
      最近更新 更多