2014-10-02 104 views
-1
import java.util.Scanner; 
import javax.swing.JOptionPane; 

public class moneyRate { 

    public static void main(String[] args) { 
     //Get Inputs 
     Scanner input = new Scanner(System.in); 
     JOptionPane.showInputDialog("How many old pounds? "); 
     double oldPounds = input.nextDouble(); 
     JOptionPane.showInputDialog("How many old shillings? "); 
     double oldShillings = input.nextDouble(); 
     JOptionPane.showInputDialog("How many old pennies? "); 
     double oldPennies = input.nextDouble(); 

     input.close(); 

     //New Pounds calc 
     double newPounds = ((oldPounds*160.80) + (oldShillings*8.04) + (oldPennies*0.67)); 

     System.out.print("Your old pounds shillings and pennies are equal to £4" 
       + "" + newPounds + "."); 
    } 
} 

在編程類,我們被要求做一個程序,它會告訴他們的老英鎊先令便士多少是當今萬鎊身價的用戶。我已經完全使用控制檯作爲程序的輸入和輸出,但現在當我嘗試使用JOptionPane來做到這一點時,向用戶展示小型彈出框不起作用。當我運行任務時,只有第一個彈出窗口顯示,程序剛剛結束,沒有任何形式的錯誤消息。我假設這是一個簡單的語法錯誤,但我無法發現它。JOptionPane.showInputDialog只顯示了一次

如果有人察覺錯誤,請幫助我,謝謝:)

+0

你爲什麼不從joption窗格中輸入內容 – 2014-10-02 07:52:23

回答

1

您使用JOptionPaneScanner引起的問題的方法。

JOptionPane.showInputDialog("How many old pounds? "); // display this 
double oldPounds = input.nextDouble(); // then it wait for scanner input 

現在你的程序將在那裏等待控制檯的輸入。您需要更改您的代碼,如下所示

double oldPounds = Double.parseDouble(JOptionPane.showInputDialog("How many old pounds? ")); 
double oldShillings = Double.parseDouble(JOptionPane.showInputDialog("How many old shillings? ")); 
double oldPennies = Double.parseDouble(JOptionPane.showInputDialog("How many old pennies? ")); 

double newPounds = ((oldPounds*160.80) + (oldShillings*8.04) + (oldPennies*0.67)); 

System.out.print("Your old pounds shillings and pennies are equal to £4" 
      + "" + newPounds + "."); 
1

你在那裏做什麼?顯示從命令行讀取的對話框後。您應該從inputDialog中獲取值。

0

Ruchira answer is complete ..只是注意到 JOptionPane.showInputDialog() 返回一個字符串。

這就是爲什麼你需要把他放在代碼中的Double.parseDouble轉換。