【问题标题】:Incompatible types: boolean cannot be converted to double (error)不兼容的类型:布尔值不能转换为双精度(错误)
【发布时间】:2015-11-05 19:37:24
【问题描述】:
import javax.swing.JOptionPane;

import java.util.Scanner;

public class pay{

static Scanner sc;

public static void main (String[]args)

{
  double Regpay = 8;
  double OThour = Regpay * 1.5;
  double hours = 0;
  double gpay = Regpay * hours;
  double Totalpay = OThour + gpay;

  Scanner input = new Scanner(System.in);
  System.out.println("Normal pay for hours worked less than 40hrs will be, £8.00 per hour");
  System.out.println("If workers work more than 40hrs, their pay will be time and a half");
  System.out.println("please enter your name");
  String name = sc.nextLine();

  System.out.println("How many hours did you work this month");
  hours = sc.nextDouble();

  System.out.println("How many hours overtime did you work");
  OThour = sc.nextDouble();




  if (hours<=40){
      Totalpay = gpay;
      System.out.print("You are not entitled to overtime pay");
  }else{
      if (hours>=41)
      Totalpay = gpay + OThour >=1;   
    }
  }        
}

我在Totalpay = gpay + OThour &gt;=1; 线上收到错误,我不知道该怎么办,我一直在尝试改变,但我一直收到同样的错误!!!!

【问题讨论】:

  • 你想用那条线完成什么?您似乎正在比较 gpay + OThour 以查看它是否大于或等于 1
  • gpay + OThour &gt;= 1 检查 gpay + OThour 是否大于或等于 1 并返回 truefalse,不能将其添加到 double,因为它不是号码。

标签: java boolean double bluej


【解决方案1】:

您正在寻找的答案(我假设)是测试 OTHoure 是否大于 1,将其添加到 TotalPay。为此,请将 TotalPay = TotalPay + OTHours >=1 替换为

if(OTHours>=1){
    TotalPay = gPay + OTHour;
}

【讨论】:

  • 大部分工作是和我的老师一起完成的,但目的是将加班时间 (OThours) 添加到 gpay 中,以获得当月支付给员工的总金额。目的还在于,如果员工工作 41 小时或以上,那么他在 40 小时内工作的每一小时都应获得 8 x 1.5 的报酬
【解决方案2】:

您正在组合两种不兼容的表达式类型。

Totalpay = gpay + OThour >=1;

使用运算顺序,“gpay + OThour >=1”是两个表达式:

gpay + OThour : (the sum of two doubles)

(result of gpay + OThour) >= 1  : (a boolean expression)

这个结果是一个布尔答案(真/假),它不能被解析为双精度,所以它不能存储在 TotalPay 中。

其次,您的结果不会像预期的那样 - 您正在使用的变量 (gpay) 已初始化为 0 并且永远不会更改。

【讨论】:

  • 我应该将 0 更改为什么值?
【解决方案3】:

@purpleorb 和@Jaboyc 已经解释了抛出错误的原因。我将讨论一个更全面的解决方案。

这有几个问题

  1. 在获取用户输入之前查找变量的值
  2. 布尔值被分配给双重问题(正如您所提到的)
  3. 不必要的 if 语句结构

您可以将代码更改为如下来解决这些问题:

  Scanner input = new Scanner(System.in);
  System.out.println("Normal pay for hours worked less than 40hrs will be, £8.00 per hour");
  System.out.println("If workers work more than 40hrs, their pay will be time and a half");
  System.out.println("please enter your name");
  String name = input.nextLine();

  double hours = 0;
  System.out.println("How many hours did you work this month");
  hours = input.nextDouble();

  double Regpay = 8;
  double OThour = Regpay * 1.5;
  double gpay = Regpay * hours;
  double Totalpay = OThour + gpay;

  if (hours>40){
      Totalpay = gpay + (OThour- 1) * (hours - 40);   
  }else{
      Totalpay = gpay;
      System.out.print("You are not entitled to overtime pay");
    }

它的作用是在用户向hours 输入值后计算gpay = Regpay * hoursTotalpay = OThour + gpay。此外,通过检查小时数是否大于 40,然后执行所需的计算,这简化了 if 语句结构。

【讨论】:

  • 欢迎您!如果您发现某个答案有帮助,并且回答了您的问题,请记得点击绿色复选标记将其标记为已接受答案!
猜你喜欢
  • 1970-01-01
  • 2015-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-07
  • 2016-08-25
  • 1970-01-01
  • 2022-01-10
相关资源
最近更新 更多