[英]while loop in Java code for netbeans
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);
}
}
Everything works fine up until the while loop... somehow I cant get it to work for me. 一切正常,直到while循环为止...以某种方式,我无法让它为我工作。 When I enter 'No' or 'no' at the end, the loop keeps running and doesn't stop until I enter space for string questions.
当我在最后输入“否”或“否”时,循环将继续运行,直到我输入空格以回答字符串问题时,循环才会停止。 It just gives me the following error at the end:
最后只给了我以下错误:
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)
Try: 尝试:
while(!s4.equals("stop")){
String is an object, not a primitive type so you have to use .equals instead of ==. 字符串是一个对象,而不是原始类型,因此您必须使用.equals而不是==。
Use equals method. 使用equals方法。 Strings shouldn't be compared with == or != operator.
字符串不应与==或!=运算符进行比较。
while (!"stop".equalsIgnoreCase(s4))
Try this. 尝试这个。
Please refer this SO thread for more understanding : Java String.equals versus == 请参考此SO线程以获得更多理解: Java String.equals与==
I belive the following line produces the exception error: 我相信以下行会产生异常错误:
double d2=Double.parseDouble(s6);
To clarify this, tell us what line is the line 66
on you main method 为了澄清这一点,请告诉我们您主要方法上的第
66
行是哪一行
this happens because the value of s6
is null
. 这是因为
s6
值为null
。 try to make sure this is sorted first. 尝试确保将其排在第一位。
Also as everyone else said, use equals
method to check String
equality. 也正如其他人所说,使用
equals
方法检查String
相等。 <--(this won't produce Exception error but it makes your program go wrong semantically). <-(这不会产生Exception错误,但会使您的程序在语义上出错)。
Why not use String equals method instead of ==?.For == to work, you would need to call intern() method on s4 which would change the reference to point to String pool. 为什么不使用String equals方法而不是== ?.为了使==起作用,您需要在s4上调用intern()方法,这将更改对指向字符串池的引用。
while(!s4.equals("stop")) 而(!s4.equals( “停止”))
Thank you so much everyone for telling me about the .equals method. 非常感谢每个人告诉我有关.equals方法的信息。 I had forgotten about that.
我已经忘记了。 I submitted my work today after thinking it over and had everything working before I did so...
经过深思熟虑后,我今天提交了我的作品,并在此之前完成了所有工作。
package mainapp;
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);
int count=0;
while(count>=0){
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);
count++;
String s8=JOptionPane.showInputDialog("Would you like to add more items to your cart?");
if(s8.contains("Yes")|| s8.contains("yes")){
}
else{
String s7=JOptionPane.showInputDialog("Please enter 'Stop' if you are done adding items to your cart");
if (s7.contains("stop")|| s7.contains("Stop")){
count=-1;
}
}
}
JOptionPane.showMessageDialog(null,
cart.toString(),
"Cart Details",
JOptionPane.INFORMATION_MESSAGE);
}
}
I had tried using the .equals() method, it didn't work for me. 我曾尝试使用.equals()方法,但对我而言不起作用。 I think I wasn't writing the loop using the correct format;
我认为我没有使用正确的格式编写循环; with the 'count' and all.
与“计数”和所有。 The 'for' was becoming a disaster too.
“ for”也正成为一场灾难。 So I wrote it differently in a way that made sense to me and it ended up working... thanks again to all those who responded!
因此,我以对我有意义的方式写了不同的文章,并且最终奏效了……再次感谢所有做出回应的人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.