Java 试图弄明白为什么我的数学不能加上

Java 试图弄明白为什么我的数学不能加上,java,Java,我目前正在为大学做一个项目,要求你创建一个基本的购物菜单。我目前正在用物品的数量乘以成本来计算我的数学总数,但总数保持为零。我创建了存储项目成本的独立整数(ex:int hat=32)和存储数量的独立整数(ex:quanHat=0)。由于某种原因,即使我添加了一个++,项目的数量仍然保持为零。有人帮我吗 我曾尝试将整数转换为字符串并返回,但它似乎没有任何作用 public static void main(String[] args) { Scanner input = new Scan

我目前正在为大学做一个项目,要求你创建一个基本的购物菜单。我目前正在用物品的数量乘以成本来计算我的数学总数,但总数保持为零。我创建了存储项目成本的独立整数(ex:int hat=32)和存储数量的独立整数(ex:quanHat=0)。由于某种原因,即使我添加了一个++,项目的数量仍然保持为零。有人帮我吗

我曾尝试将整数转换为字符串并返回,但它似乎没有任何作用

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Pirate Trading Post v3");
    System.out.println("----------------------");
    int eight = 8;
    int hat = 32;
    int patch = 2;
    int sword = 20;
    int map = 100;
    int shirt = 150;
    int test = -1;
    int quanEight = 0;
    int quanHat = 0;
    int quanPatch = 0;
    int quanSword = 0;
    int quanMap = 0;
    int quanShirt = 0;
    int total = ( quanEight * eight) + ( quanHat * hat) + ( quanPatch * patch) + ( quanSword * sword) + ( quanShirt * shirt) + ( quanMap * map);
    while (test != 0){
        System.out.println("Enter Item Code, ? or Q: ");
        String code = input.next();
        char ch = code.charAt(0);
        ch = Character.toUpperCase(ch);

        if (ch == '?'){
            System.out.println("Valid Item codes are: 8 I H M S T.");
            System.out.println("Q to quit.");
        }
        else if (ch == 'Q'){
            test++;
            System.out.println("Pirate Trading Post");
            System.out.println(quanEight + " Genuine Piece Of Eight " + quanHat + " Pirate Hat " + quanPatch + " Eye Patch " + quanSword + " Sword " + quanMap + " Treasure Map " + quanShirt + " T-Shirt ");
            System.out.println("Total: " + total + " bits");

        }
        else if (ch == '8'){
            quanEight ++;

        }
        else if (ch == 'I'){
            quanHat++;
        }
        else if (ch == 'H'){
            quanPatch++;
        }    
        else if (ch == 'M'){
            quanSword++;
        }
        else if (ch == 'S'){
            quanMap++;
        }
        else if (ch == 'T'){
            quanShirt++;
        }

    }

预期产出应为项目成本乘以数量,但数量不会存储该值。我认为该值没有存储,因为它是一个字符串,但我不确定。

当代码计算总数时,quanHat为0。所以total被赋值为0

在while循环期间,当quanHat递增时,其值递增1

但由于没有更新或重新计算总计,它仍然显示0


您需要重新计算
的if块中的total,否则if(ch='Q')

循环开始之前,您正在计算
total
,而
循环甚至开始,其中每个
变量都是
0
。显然,
total
的值本身不会更新。在循环内移动
total
的计算。
total=(quanEight*eight)+……
不会在变量中保存公式;它使用实际值进行计算,并将结果(一次)存储在变量中。谢谢,我对Java非常陌生,这实际上更有意义。