![](/img/trans.png)
[英]exception while sending mail through java code in netbeans while not in eclipse
[英]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);
}
}
一切正常,直到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")){
字符串是一个对象,而不是原始类型,因此您必须使用.equals而不是==。
使用equals方法。 字符串不应与==或!=运算符进行比较。
我相信以下行会产生异常错误:
double d2=Double.parseDouble(s6);
为了澄清这一点,请告诉我们您主要方法上的第66
行是哪一行
这是因为s6
值为null
。 尝试确保将其排在第一位。
也正如其他人所说,使用equals
方法检查String
相等。 <-(这不会产生Exception错误,但会使您的程序在语义上出错)。
为什么不使用String equals方法而不是== ?.为了使==起作用,您需要在s4上调用intern()方法,这将更改对指向字符串池的引用。
而(!s4.equals( “停止”))
非常感谢每个人告诉我有关.equals方法的信息。 我已经忘记了。 经过深思熟虑后,我今天提交了我的作品,并在此之前完成了所有工作。
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);
}
}
我曾尝试使用.equals()方法,但对我而言不起作用。 我认为我没有使用正确的格式编写循环; 与“计数”和所有。 “ for”也正成为一场灾难。 因此,我以对我有意义的方式写了不同的文章,并且最终奏效了……再次感谢所有做出回应的人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.