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 >=1;
行上的错误我不知道该怎么做,我一直在尝试改变一些事情,但我一直得到同样的错误!!!!
答案 0 :(得分:0)
您正在寻找的答案(我推测)是测试OTHou是否大于1,将其添加到TotalPay。要实现此目的,请使用
替换TotalPay = TotalPay + OTHours&gt; = 1if(OTHours>=1){
TotalPay = gPay + OTHour;
}
答案 1 :(得分:0)
您正在组合两种不兼容的表达类型。
Totalpay = gpay + OThour >=1;
使用操作顺序,&#34; gpay + OThour&gt; = 1&#34;是两个表达式:
gpay + OThour : (the sum of two doubles)
(result of gpay + OThour) >= 1 : (a boolean expression)
这样的结果是一个布尔答案(真/假),它不能作为双重解析,因此它不能存储在TotalPay中。
其次,您的结果将不会如预期的那样 - 您使用的变量(gpay)已初始化为0且永远不会更改。
答案 2 :(得分:0)
@purpleorb和@ Jaboyc已经解释了为什么会抛出错误。我将谈论一个更全面的解决方案。
这个有几个问题
您可以将代码更改为以下内容以解决这些问题:
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");
}
这样做是在用户将值输入gpay = Regpay * hours
后进行计算Totalpay = OThour + gpay
和hours
。此外,这通过检查小时数是否大于40来简化if语句结构,然后执行所需的计算。