似乎我的輸入框似乎出現了兩次,即使他們不應該太。似乎無法弄清楚爲什麼。將是巨大的,如果有人可以幫助:)程序似乎沒有明顯的原因循環
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
我理解,這是一個非常基本的程序,但我或多或少只是想通過59。基本的東西,學習java。所以希望如果我能理解如何讓一個簡單的程序運行,我可以繼續前進。
Nadim,我想提出一些脫離主題的建議...... 1)使用有意義的變量名稱。儘管'c'和'f'幾乎沒有問題*,'x','y'和'z'確實很糟糕。稱他們爲'celcius','fahrenheit','input','type'等等。2)當你發佈到SO後,在發佈後檢查你的代碼格式 - 你有很多多餘的空白行。 *一條*空行就夠了。您也不需要在方法簽名和開放大括號之間留空行。嘗試構建已建立的佈局樣式。 –
您不需要在標題中加入「[已解決]」 - 只需[接受答案](http://meta.stackexchange.com/a/5235/185290)。 –
@Greg Kopff好的,謝謝你的建議:) – Nadim