【问题标题】:I need it to convert from an Int to a double [closed]我需要它从 Int 转换为 double [关闭]
【发布时间】:2014-10-25 02:10:44
【问题描述】:

这是我目前拥有的代码。

我需要将最后一个 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(float) 是唯一有效的方法

【问题讨论】:

  • 它输出了什么,你期待什么,如果它有效,Math.round(float) 有什么问题?
  • 您将insurance 变量用于多种用途。首先你需要解决这个问题,然后我们可以帮助你(如果仍然需要)
  • 你的第一个 if 块将永远运行,因为当 if 块出现时,技能将始终为 0。

标签: java swing int double


【解决方案1】:

您没有明确说明insurance 是什么类型,但Math#round 有两个不同的签名:

如果您的insurance 变量被声明为int,您将无法将long 分配给它(不兼容的类型)。

【讨论】:

  • 开关盒需要是一个 Int,我试图将“牙科保险计数”的值设置为保险,这样它就可以成为我以后使用的值...可能有不同的方式而不是开关盒呢?
  • 为您的 switch 语句使用不同的变量。你不想在你的程序中混淆它的含义,因为这会使其不可读。
  • 好吧,我试试看。谢谢
  • @TonyConnorMalynn 尝试为每个变量声明写一个简短、简单、准确的注释,准确说明变量的含义。如果您不能这样做,请重新考虑如何使用该变量。
【解决方案2】:

您的代码显示的一个问题是,您使用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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 2011-05-10
    相关资源
    最近更新 更多