2014-08-31 25 views
-4

這是我迄今爲止的代碼。我需要它從一個Int轉換爲雙精度

我需要if語句的最後轉換爲雙

public static void main(String[] args) { 

    double skill = 0; double hours = 0; double overTime= 0.5; int insurance =0; 

    String[] choices = {"Medical Insurance", "Dental Insurance", "Long-Term Disability  Insurance"}; 

     double MedicalInsuranceCount = 32.50; 
     double DentalInsuranceCount = 20.00; 
     double LongTermDisabilityInsuranceCount = 10.00 

    if (skill >= 20.00 && skill <=22.00) 

    insurance = JOptionPane.showOptionDialog(
      null       // Center in window.// prompts the user to select a button 
      , "Types of Insurance options" // Message 
      , "Insurance option"    // Title in titlebar 
      , JOptionPane.YES_NO_OPTION  // Option type 
      , JOptionPane.PLAIN_MESSAGE  // messageType 
      , null       // Icon (none) 
      , choices       // Button text as above. 
      , "None of your business"   // Default button's label 
    );     

    switch (insurance) 

      { 
       case 0: 
        MedicalInsuranceCount++; 
        break; 
       case 1: 
        DentalInsuranceCount++; 
        break; 
       case 2: 
        LongTermDisabilityInsuranceCount++; 
        break; 
       default: 
        //... If we get here, something is wrong. Defensive programming. 
        JOptionPane.showMessageDialog(null, "Unexpected response " + insurance); 
      } 

// this is where the problem is I think. 

    if(insurance == 0) 
     insurance = Math.round((float)MedicalInsuranceCount); 

    else if(insurance == 1) 
     insurance = Math.round((float)DentalInsuranceCount); 
    else if(insurance == 2) 
     insurance = Math.round((float)LongTermDisabilityInsuranceCount); 
    System.out.println(insurance); 
    } 
} 

香港專業教育學院嘗試使用Double.PasreDouble但失敗和Math.round(浮動)是工作

+0

它是什麼,輸出什麼是你期待的,什麼是錯'Math.round(浮動)'是否行得通? – BitNinja 2014-08-31 20:09:50

+0

您正在使用「保險」變量用於多種用途。首先,你需要整理出來,然後我們可以幫你(如果仍然有必要的話) – ljgw 2014-08-31 20:14:06

+0

你的第一個if block將永遠不會**,因爲當if塊出現時,技能總是0。 – 2014-08-31 22:07:24

回答

2

你的唯一的事情都沒有明確申明insurance是什麼類型的,但也有2個不同的簽名Math#round

如果您insurance變量聲明爲int,你將不能夠到long分配到它(不兼容的類型)。

+0

切換案例需要是一個Int,即時嘗試將「牙科保險計數」的值設置爲保險,以便它可以成爲我以後使用的值...是否有可能採用不同的方式而不是切換案例呢? – 2014-08-31 20:20:44

+0

爲您的switch語句使用不同的變量。您不想在程序中混淆它的含義,因爲這會使其無法讀取。 – Makoto 2014-08-31 20:21:31

+0

好吧我會試試看。謝謝 – 2014-08-31 20:23:32

1

一個問題,你的代碼表明的是,你使用的insurance變量做太多的事情,包括舉辦邏輯數據 - 保險什麼類型是我們處理和浮點數字數據 - 保險費用多少不要這樣做。爲單獨的功能使用單獨的變量。

另外我有一個偷偷摸摸的懷疑,你的不是一個數字的問題舍入,而是一個顯示的格式化數字問題。如果是這樣,那麼集中精力四捨五入數字和更多的顯示格式正確的輸出。 NumberFormat類型的對象可以幫助你做到這一點。例如...

import java.text.DecimalFormat; 
import java.text.NumberFormat; 

public class Foo5 { 
    public static void main(String[] args) { 
     double insurance = 32.50; 

     NumberFormat decimalFormat = new DecimalFormat("0.00"); 
     NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); 

     System.out.println("Insurance is: " + decimalFormat.format(insurance)); 
     System.out.println("Insurance in dollars is: " + currencyFormat.format(insurance)); 
    } 
} 

,輸出:

Insurance is: 32.50 
Insurance in dollars is: $32.50