Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 零售价格总误差_Java - Fatal编程技术网

Java 零售价格总误差

Java 零售价格总误差,java,Java,为什么当我打印出整个销售总额和零售价格总额时,它不会给出正确的答案?我的方程式错了吗 public class RetailPrice { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int NumofItems; int items; double WS = 0; double MP

为什么当我打印出整个销售总额和零售价格总额时,它不会给出正确的答案?我的方程式错了吗

public class RetailPrice {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int NumofItems;
        int items;
        double WS = 0;
        double MP = 0;
        double RP;
        RP = (WS + (MP * WS));
        System.out.println("How many items do you want to process?: ");
        NumofItems = keyboard.nextInt();
        for (items = 1; items <= NumofItems; items++) {
            System.out.println("Enter the wholesale price of item " + items);
            WS = keyboard.nextDouble();
            System.out.println("Enter the markup percentage for item: " + items);
            MP = keyboard.nextDouble();
            System.out.printf("The retail price of item " + items + " is $%.2f" + (WS + (MP * WS)));
            System.out.println();
        }
        double totalWP = 0;
        totalWP = totalWP + WS;
        System.out.println("Total wholesale price for the " + items + " items is " + totalWP);
        double totalRP;
        totalRP = RP * NumofItems;
        System.out.println("Total retail price for the " + items + " items is " + totalRP);
    }
}

我已经移动了一些行以获得您想要的功能。我们还可以做很多其他的改进,但是首先需要处理逻辑

尝试运行此代码,并特别注意我移动的行。标记为//***想想我为什么会移动它们,它们在你放它们的地方做了什么,它们在我放它们的地方做了什么

import java.util.Scanner;

public class RetailPrice {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int NumofItems;
        int items;
        double WS = 0;
        double MP = 0;
        double RP;

        System.out.println("How many items do you want to process?: ");
        NumofItems = keyboard.nextInt();
        double totalWP = 0; // ***
        for (items = 1; items <= NumofItems; items++) {
            System.out.println("Enter the wholesale price of item " + items);
            WS = keyboard.nextDouble();
            System.out.println("Enter the markup percentage for item: " + items);
            MP = keyboard.nextDouble();
            System.out.println("The retail price of item " + items + " is $" + (WS + (MP * WS)));
            totalWP = totalWP + WS; // ***
        }
        System.out.println("Total wholesale price for the " + items + " items is " + totalWP);
        double totalRP;
        RP = (WS + (MP * WS)); // ***
        totalRP = RP * NumofItems;
        System.out.println("Total retail price for the " + items + " items is " + totalRP);
    }
}

你希望得到什么?你得到了什么?你做了什么来调试这个?我希望总共得到65.00美元,我得到45.00美元。我知道对于totalWP,我需要将所有3个完整的销售价格相加,但我无法计算出java可以接受的公式。请尝试在for循环中而不是之后进行相加。在循环之前初始化totalWP。如果用户输入3项,当java只能识别其中一项时,我如何将它们添加到一起?示例:项目1为10,项目2为10,项目3为10显然等于30,但我如何告诉java识别所有这些,并将它们相加,而不是识别1?