java - 赋值的左边一定是变量?
问题描述
我正在尝试进行一项涉及编写以下程序的 uni 活动。我一直在第 27 行收到错误else (employeetype == 2) {
,错误如下The left-hand side of an assignment must be a variable
Syntax error, insert "AssignmentOperator Expression" to complete Assignment
Syntax error, insert ";" to complete Statement
。
我是 java 新手,并不真正理解我在这里做错了什么,因为相同的赋值在 if 语句中正常工作,但在 else 中没有。
有人可以帮我吗?
import java.util.*;
public class TaskFive {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Please type 1 if you are hourly rated or 2 if you are a salesperson.");
double employeetype = console.nextDouble();
double pay;
if (employeetype == 1) {
System.out.println("Please enter your hourly rate:");
double hourly = console.nextDouble();
System.out.println("Please enter the total hours you worked:");
double hoursworked = console.nextDouble();
if (hoursworked > 40) {
double overtimepay = hoursworked - 40;
pay = (overtimepay * (hourly * 1.5)) + (40 * hourly);
}
else {
pay = (hoursworked * hourly);
}}
else (employeetype == 2) {
System.out.println("Please enter your commission rate as a decimal:");
double comrate = console.nextInt();
System.out.println("Please enter your sales value:");
double sales = console.nextInt();
pay = 500 + (sales * comrate);
}
double tax = (pay*.3);
double netsalary = pay - tax;
System.out.println("Tax: " + tax);
System.out.println("Net Salary: " + netsalary);
}
}
解决方案
你好,if else 语句中的朋友,if 可以检查条件,else 不能检查条件,它只执行语句。所以如果你想在 if 后面检查 else 中的条件,你必须改变它到下面的代码
else if (employeetype == 2)
推荐阅读
- vba - 当 DocVariable 字段包含在 TextBox 中时,如何使用 VBA 用字符串值填充 DocVariable
- postgresql - 将包含列表的记录值与 Postgres 中的列值进行比较
- java - Java、Apache POI、XSSFWorkbook 极慢
- javascript - ReactJs 函数式组件将函数添加到 eventListener 然后调用它
- python - 如何从 django 的数据库中获取下一个可用对象或主键
- go - 处理动态字段时如何在 Go 中解组
- javascript - 如何使用角度打字稿按日期将数据导出到 csv
- python - 文件脚本编程我试图设置我的文件脚本程序来打开我的 CWD
- swift - 如何将参数传递给快速完成的函数
- python - struct.unpack 返回非常大的值 python