【发布时间】:2012-06-04 02:22:41
【问题描述】:
我的输入框似乎出现了两次,尽管它们不应该出现。似乎无法弄清楚为什么。如果有人能帮忙就好了:)
int x; //temperature input
int y; //temperature type
int z; //temperature convert
int c; //temperature celsius
int f; //temperature fahrenheit
public void init()
{
setSize(500, 500);
Container c = getContentPane();
c.setBackground(Color.BLUE);
}
public void paint (Graphics g)
{
super.paint(g);
g.setFont(new Font("Veranda", Font.PLAIN, 20));
g.setColor(Color.BLACK);
String number = JOptionPane.showInputDialog("What temperature would you like to convert? (input # of degrees)");
x = Integer.parseInt(number);
String number2 = JOptionPane.showInputDialog("What temperature type are you inputting? 1. Fahrenheit 2. Celsius");
y = Integer.parseInt(number2);
if (y==1)
{
c=(5/9)*(f-32);
g.drawString("Your temperature of" + x + "is" + y + "Celsius", 250, 100);
}//end if
if (y==2)
{
f=(9/5)*c+32;
g.drawString("Your temperature of" + x + "is" +y + "Fahrenheit", 250, 100);
}//end if
}//end paint
我知道这是一个非常基本的程序,但我或多或少只是试图通过盯着基本的东西来学习 java。所以希望如果我能理解如何让一个简单的程序运行起来,我就可以继续前进。
【问题讨论】:
-
Nadim,我想提出一些题外话... 1) 使用有意义的变量名。虽然
c和f勉强可以,但x、y和z真的很糟糕。打电话给他们celcius、fahrenheit、input、type等。 2) 当您发布到 SO 时,请在发布后检查您的代码格式 - 您有很多多余的空行。 一个空行总是足够的。在方法签名和左大括号之间也不需要空行。尝试对已建立的布局样式进行模式化。 -
您无需在标题中添加“[已解决]” - 只需 accept an answer。
-
@Greg Kopff 好的,感谢您的有用建议 :)
标签: java loops if-statement