Java 如何正确显示我的费率值?

Java 如何正确显示我的费率值?,java,Java,我终于让我的程序编译,没有任何错误,上半年是正确的总工资,退休扣减和净工资都显示为0。我在另一篇文章中看到,Java没有分析if块的逻辑,因此我编辑了代码,将rate指定为0,将if语句指定为ReturnRate。我现在得到错误“意外返回值”。如何根据用户的输入使程序具有适当的值 import java.util.*; public class AcmePay { public static void main(String[] args) throws Exception { Scan

我终于让我的程序编译,没有任何错误,上半年是正确的总工资,退休扣减和净工资都显示为0。我在另一篇文章中看到,Java没有分析if块的逻辑,因此我编辑了代码,将rate指定为0,将if语句指定为ReturnRate。我现在得到错误“意外返回值”。如何根据用户的输入使程序具有适当的值

import java.util.*;
public class AcmePay {
public static void main(String[] args) throws Exception {
    Scanner keyboard = new Scanner(System.in);
    double hours;
    int shift;
    int plan;
    double rate = 0;
    double overtimePay;
    double paycheck;
    //double retirement;
   // double retirement = paycheck - (paycheck * .03);
    //double netPay = paycheck - retirement;



    System.out.println("How many hours did you work this week?");
    hours = keyboard.nextDouble();
      if ( hours <= 40 )
      {
          paycheck = hours * rate;
      }
      else
      { 
          paycheck = (40 * rate) + ((hours - 40)) * (rate*1.5);

      }
 System.out.println("What shift did you work? 1, 2, or 3?");
    shift = keyboard.nextInt();
    if (shift == 1)
    {
        rate = 17;
        return rate;
    }
    else if (shift == 2)
    {
        rate = 18.50;
        return rate;
    }
    else if (shift == 3)
    {
        rate = 22;
        return rate;
    }
import java.util.*;
公共级AcmePay{
公共静态void main(字符串[]args)引发异常{
扫描仪键盘=新扫描仪(System.in);
双倍小时;
int移位;
int计划;
倍率=0;
双倍超期工资;
双倍工资;
//双重退休;
//双倍退休=工资支票-(工资支票*.03);
//双倍净付=工资支票-退休;
这个星期你工作了多少小时;
小时=键盘.nextDouble();

如果(小时打印速率,代码的最后部分可以如下所示:

shift = keyboard.nextInt();
if (shift == 1) {
    rate = 17;
} else if (shift == 2) {
    rate = 18.50;
} else if (shift == 3) {
    rate = 22;
}
System.out.println("Rate = " + rate);
private static double rateForShift(int shift) {
    if (shift == 1) {
        return 17;
    } else if (shift == 2) {
        return 18.50;
    } else if (shift == 3) {
        return 22;
    }
    return 0;
}
public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    System.out.println("How many hours did you work this week?");
    double hours = keyboard.nextDouble();

    System.out.println("What shift did you work? 1, 2, or 3?");
    int shift = keyboard.nextInt();

    double paycheck = calculatePayCheck(rateForShift(shift), hours);
    System.out.println("Paycheck = $" + paycheck);
}

private static double calculatePayCheck(double rate, double hours) {
    if (hours <= 40) {
        return hours * rate;
    } else {
        return (40 * rate) + ((hours - 40) * (rate * 1.5));
    }
}

private static double rateForShift(int shift) {
    if (shift == 1) {
        return 17;
    } else if (shift == 2) {
        return 18.50;
    } else if (shift == 3) {
        return 22;
    }
    return 0;
}
i、 e.删除return语句,然后在末尾打印rate。不能从
void
方法(如
main()
)返回值,因此会出现错误

如果要使用单独的方法计算费率,可以执行以下操作:

shift = keyboard.nextInt();
if (shift == 1) {
    rate = 17;
} else if (shift == 2) {
    rate = 18.50;
} else if (shift == 3) {
    rate = 22;
}
System.out.println("Rate = " + rate);
private static double rateForShift(int shift) {
    if (shift == 1) {
        return 17;
    } else if (shift == 2) {
        return 18.50;
    } else if (shift == 3) {
        return 22;
    }
    return 0;
}
public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    System.out.println("How many hours did you work this week?");
    double hours = keyboard.nextDouble();

    System.out.println("What shift did you work? 1, 2, or 3?");
    int shift = keyboard.nextInt();

    double paycheck = calculatePayCheck(rateForShift(shift), hours);
    System.out.println("Paycheck = $" + paycheck);
}

private static double calculatePayCheck(double rate, double hours) {
    if (hours <= 40) {
        return hours * rate;
    } else {
        return (40 * rate) + ((hours - 40) * (rate * 1.5));
    }
}

private static double rateForShift(int shift) {
    if (shift == 1) {
        return 17;
    } else if (shift == 2) {
        return 18.50;
    } else if (shift == 3) {
        return 22;
    }
    return 0;
}
此方法返回一个
double
,因此现在您可以(并且必须)使用
return
语句

您可以使用以下命令从主方法调用它:

double rate = rateForShift(shift);
将代码拆分为如下重点方法是一个好主意,因为这样更易于阅读和使用


我认为您的代码中有一个“逻辑”错误,因为您正在使用
rate
变量来计算
paycheck
,但是
rate
变量在您使用时总是
0
。在计算薪资金额之前,您可能应该问这两个问题

完整的程序如下所示:

shift = keyboard.nextInt();
if (shift == 1) {
    rate = 17;
} else if (shift == 2) {
    rate = 18.50;
} else if (shift == 3) {
    rate = 22;
}
System.out.println("Rate = " + rate);
private static double rateForShift(int shift) {
    if (shift == 1) {
        return 17;
    } else if (shift == 2) {
        return 18.50;
    } else if (shift == 3) {
        return 22;
    }
    return 0;
}
public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    System.out.println("How many hours did you work this week?");
    double hours = keyboard.nextDouble();

    System.out.println("What shift did you work? 1, 2, or 3?");
    int shift = keyboard.nextInt();

    double paycheck = calculatePayCheck(rateForShift(shift), hours);
    System.out.println("Paycheck = $" + paycheck);
}

private static double calculatePayCheck(double rate, double hours) {
    if (hours <= 40) {
        return hours * rate;
    } else {
        return (40 * rate) + ((hours - 40) * (rate * 1.5));
    }
}

private static double rateForShift(int shift) {
    if (shift == 1) {
        return 17;
    } else if (shift == 2) {
        return 18.50;
    } else if (shift == 3) {
        return 22;
    }
    return 0;
}
publicstaticvoidmain(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
这个星期你工作了多少小时;
双倍小时=键盘.nextDouble();
System.out.println(“你是什么班次的?1、2或3?”);
int shift=keyboard.nextInt();
双倍工资=calculatePayCheck(班次费率,小时);
System.out.println(“工资支票=$”+工资支票);
}
私人静态双重计算检查(双倍费率,双倍小时){

如果(小时打印速率,代码的最后部分可以如下所示:

shift = keyboard.nextInt();
if (shift == 1) {
    rate = 17;
} else if (shift == 2) {
    rate = 18.50;
} else if (shift == 3) {
    rate = 22;
}
System.out.println("Rate = " + rate);
private static double rateForShift(int shift) {
    if (shift == 1) {
        return 17;
    } else if (shift == 2) {
        return 18.50;
    } else if (shift == 3) {
        return 22;
    }
    return 0;
}
public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    System.out.println("How many hours did you work this week?");
    double hours = keyboard.nextDouble();

    System.out.println("What shift did you work? 1, 2, or 3?");
    int shift = keyboard.nextInt();

    double paycheck = calculatePayCheck(rateForShift(shift), hours);
    System.out.println("Paycheck = $" + paycheck);
}

private static double calculatePayCheck(double rate, double hours) {
    if (hours <= 40) {
        return hours * rate;
    } else {
        return (40 * rate) + ((hours - 40) * (rate * 1.5));
    }
}

private static double rateForShift(int shift) {
    if (shift == 1) {
        return 17;
    } else if (shift == 2) {
        return 18.50;
    } else if (shift == 3) {
        return 22;
    }
    return 0;
}
i、 e.删除return语句,然后在末尾打印rate。不能从
void
方法(如
main()
)返回值,因此会出现错误

如果要使用单独的方法计算费率,可以执行以下操作:

shift = keyboard.nextInt();
if (shift == 1) {
    rate = 17;
} else if (shift == 2) {
    rate = 18.50;
} else if (shift == 3) {
    rate = 22;
}
System.out.println("Rate = " + rate);
private static double rateForShift(int shift) {
    if (shift == 1) {
        return 17;
    } else if (shift == 2) {
        return 18.50;
    } else if (shift == 3) {
        return 22;
    }
    return 0;
}
public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    System.out.println("How many hours did you work this week?");
    double hours = keyboard.nextDouble();

    System.out.println("What shift did you work? 1, 2, or 3?");
    int shift = keyboard.nextInt();

    double paycheck = calculatePayCheck(rateForShift(shift), hours);
    System.out.println("Paycheck = $" + paycheck);
}

private static double calculatePayCheck(double rate, double hours) {
    if (hours <= 40) {
        return hours * rate;
    } else {
        return (40 * rate) + ((hours - 40) * (rate * 1.5));
    }
}

private static double rateForShift(int shift) {
    if (shift == 1) {
        return 17;
    } else if (shift == 2) {
        return 18.50;
    } else if (shift == 3) {
        return 22;
    }
    return 0;
}
此方法返回一个
double
,因此现在您可以(并且必须)使用
return
语句

您可以使用以下命令从主方法调用它:

double rate = rateForShift(shift);
将代码拆分为如下重点方法是一个好主意,因为这样更易于阅读和使用


我认为您的代码中有一个“逻辑”错误,因为您正在使用
rate
变量来计算
paycheck
,但是
rate
变量在您使用时总是
0
。在计算薪资金额之前,您可能应该问这两个问题

完整的程序如下所示:

shift = keyboard.nextInt();
if (shift == 1) {
    rate = 17;
} else if (shift == 2) {
    rate = 18.50;
} else if (shift == 3) {
    rate = 22;
}
System.out.println("Rate = " + rate);
private static double rateForShift(int shift) {
    if (shift == 1) {
        return 17;
    } else if (shift == 2) {
        return 18.50;
    } else if (shift == 3) {
        return 22;
    }
    return 0;
}
public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    System.out.println("How many hours did you work this week?");
    double hours = keyboard.nextDouble();

    System.out.println("What shift did you work? 1, 2, or 3?");
    int shift = keyboard.nextInt();

    double paycheck = calculatePayCheck(rateForShift(shift), hours);
    System.out.println("Paycheck = $" + paycheck);
}

private static double calculatePayCheck(double rate, double hours) {
    if (hours <= 40) {
        return hours * rate;
    } else {
        return (40 * rate) + ((hours - 40) * (rate * 1.5));
    }
}

private static double rateForShift(int shift) {
    if (shift == 1) {
        return 17;
    } else if (shift == 2) {
        return 18.50;
    } else if (shift == 3) {
        return 22;
    }
    return 0;
}
publicstaticvoidmain(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
这个星期你工作了多少小时;
双倍小时=键盘.nextDouble();
System.out.println(“你是什么班次的?1、2或3?”);
int shift=keyboard.nextInt();
双倍工资=calculatePayCheck(班次费率,小时);
System.out.println(“工资支票=$”+工资支票);
}
私人静态双重计算检查(双倍费率,双倍小时){

如果(hoursELEVATE)涵盖了代码,那么我将涵盖理论

在Java(以及许多其他编程语言)中,方法是一个代码块,它可以接受也可以不接受输入,也可以不提供输出。方法是否提供输出可以通过分析方法定义来确定。如果是基元类型(例如,
int
double
)或对象(例如,
Scanner
)用于方法定义,则该方法将
返回该类型作为输出。如果在方法定义中使用关键字
void
,则该方法将不返回任何输出

需要理解的一个关键点是:一旦方法被告知
返回值,该方法将终止。因此,您不能在
返回
语句后包含任何代码,否则编译器将对您生气,并因您包含“无法访问的代码”而对您大喊大叫

现在,将此应用于具体情况。Java中的
main
方法使用关键字
void
表示它不会返回任何类型的输出。
因此,
返回率;
不合适,原因有二:

  • 返回双精度值确实是某种输出,这与
    main
    的方法定义相矛盾,其中
    main
    通过关键字
    void
    设置为不返回输出
  • return rate;
    将导致您的程序立即终止。假设ELEVATE对您应该如何重新排序代码是正确的,那么在您的答案中留下一条
    return
    语句将使您的代码无法继续计算您的工资支票,从而导致进一步的问题

  • 旁注:不返回输出的方法仍然可以使用
    return
    关键字,但不能与任何类型的值或对象一起使用。例如,以下方法有效,因为
    return
    不与任何类型的值配对