java - Netbeans 的 Java 代码中的 while 循环

标签 java loops netbeans while-loop

import java.util.ArrayList;
import javax.swing.JOptionPane;

public class MainApp {

    public static void main(String[] args) {

        ArrayList<Product> cart = new ArrayList<Product>();

        String s1 = JOptionPane
                .showInputDialog("Please enter the quantity of the product you have selected");

        int r = Integer.parseInt(s1);

        String s2 = JOptionPane
                .showInputDialog("Please enter the name of the product");

        String s3 = JOptionPane
                .showInputDialog("Please enter the price of the product");
        double d1 = Double.parseDouble(s3);

        for (int i = 0; i < r; i++) {

            Product p1 = new Product();
            p1.setName(s2);
            p1.setPrice(d1);

            cart.add(p1);

        }

        double total_price = 0;
        for (int i = 0; i < cart.size(); i++) {
            total_price = total_price + cart.get(i).getPrice();
        }

        JOptionPane.showMessageDialog(null, cart.toString()
                + "The total price of all the products in the cart is : "
                + total_price + " KD", "Shopping Information",
                JOptionPane.INFORMATION_MESSAGE);

        cart.removeAll(cart);

        JOptionPane.showMessageDialog(null, "Thank you for shopping with us! ",
                "Prompt", JOptionPane.INFORMATION_MESSAGE);

        String s4 = JOptionPane
                .showInputDialog("Would you like to add items to your cart");

        while (s4 != "stop") {

            String s5 = JOptionPane
                    .showInputDialog("Please enter the name of the product");

            String s6 = JOptionPane
                    .showInputDialog("Please enter the price of the product");
            double d2 = Double.parseDouble(s6);

            Product p2 = new Product();

            p2.setName(s5);
            p2.setPrice(d2);

            cart.add(p2);

            String s7 = JOptionPane
                    .showInputDialog("Would you like to add more items?");
            if (s7 == "No" || s7 == "no")
                s4 = "stop";

        }

        JOptionPane.showMessageDialog(null, cart.toString(), "Cart Details",
                JOptionPane.INFORMATION_MESSAGE);

    }

}

一切工作正常,直到 while 循环......不知怎的,我无法让它为我工作。当我在末尾输入“否”或“否”时,循环将继续运行,直到我为字符串问题输入空格为止。它只是在最后给了我以下错误:

Exception in thread "main" java.lang.NullPointerException
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1008)
    at java.lang.Double.parseDouble(Double.java:540)
    at mainapp.MainApp.main(MainApp.java:66)
Java Result: 1
BUILD SUCCESSFUL (total time: 41 seconds)

最佳答案

尝试:

while(!s4.equals("stop")){ 

String 是一个对象,而不是原始类型,因此您必须使用 .equals 而不是 ==。

关于java - Netbeans 的 Java 代码中的 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26405333/

相关文章:

c++ - 在 C++ 中,我想创建一个循环,不断检查文件的大小,并在文件大小发生变化时执行某些操作

Java 小程序无法在 Django 开发环境中加载( View 变量未提供给模板)

java - 当 System.nanoTime() 在调用之间返回相同的值时如何处理?

Apache rewriterule 将文件夹重定向到子文件夹而无需循环

java - 如何从二维数组(java)中打印出奇数索引元素?

java - 如何在 WEB-INF (Netbeans) 中的 JSP 页面中使用样式表 (CSS)

Java-netbeans 8.1 : Unrecognized file?

java - 显示 java-libpst 的 PSTMessage 的简单方法

java - JFormattedTextField 删除用户输入

java - Beans 在 NetBeans 中绑定(bind) JTable