首页 > 解决方案 > 赋值的左边一定是变量?

问题描述

我正在尝试进行一项涉及编写以下程序的 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);

        }

    }

标签: javaif-statement

解决方案


你好,if else 语句中的朋友,if 可以检查条件,else 不能检查条件,它只执行语句。所以如果你想在 if 后面检查 else 中的条件,你必须改变它到下面的代码

 else if (employeetype == 2)

推荐阅读