我正在制作一个程序而且我不明白该程序有什么问题,我试图让它工作,但每次我运行它并给出一个答案,它会增加一个零回答。救命。
import javax.swing.JOptionPane;
public class Furniture
{
public static void main(String args[])
{
String response_1 = JOptionPane.showInputDialog(null, "Type of Table - 1)Pine Wood, 2)Oak, 3)Mahogany");
int type_of_wood = Integer.parseInt(response_1);
String response_2 = JOptionPane.showInputDialog(null, "Size of table - 4)Small, or 5)Large");
int size = Integer.parseInt(response_2);
int price_of_wood = 0;
int price_of_table = 0;
switch(type_of_wood)
{
case 1:
price_of_wood = 100;
break;
case 2:
price_of_wood = 225;
break;
case 3:
price_of_wood = 310;
break;
case 4:
price_of_table = 0;
break;
case 5:
price_of_table = 35;
break;
default:
JOptionPane.showMessageDialog(null, "Unknown Number entered.");
}
JOptionPane.showMessageDialog(null, "The price is " + price_of_wood + price_of_table + " dollars");
}
}
答案 0 :(得分:1)
当您在int
中使用float
,String
等println()
等变量来显示输出时,它们会被连接而不是被添加或您尝试执行的任何操作。所以首先评估你要输出的表达式。试试这个,
int price = price_of_wood + price_of_table;
JOptionPane.showMessageDialog(null, "The price is " + price + " dollars");