我是java的新手,我试图在最后一行返回要打印的佣金值。但我一直得到不兼容的类型:意外的返回值错误。
import java.util.Scanner;
public class retail {
public static void main (String[] args){
char code;
double commission;
String enumber;
double retail_price=0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter employee number: ");
enumber= scan.nextLine();
System.out.println("Enter retail price: ");
retail_price= scan.nextDouble();
System.out.println("Enter code:");
code=scan.next().charAt(0);
if (code == 'A'){ commission = (retail_price/100)*6;}
else if (code == 'a') {commission = (retail_price/100)*6;}
else if (code == 'B') {commission = (retail_price/100)*8;}
else if (code == 'b') {commission = (retail_price/100)*8;}
else if (code == 'C') {commission = (retail_price/100)*10;}
else if (code == 'c') {commission = (retail_price/100)*10;}
else{System.out.println("Invalid code");}
return commission;
System.out.println("Employee number: "+enumber);
System.out.println("Retail price: "+retail_price);
System.out.println("Commission: "+commission);
}
}
答案 0 :(得分:0)
您不需要在main方法中返回值。只需在声明时将佣金初始化为0并删除退货。
RFLAGS
bits 32-63 Reserved
答案 1 :(得分:0)
你正在main方法中返回。所以显示错误。如果你想返回值,你应该这样使用,
private static double setValue(char code,double commission,double retail_price){
if (code == 'A'){ commission = (retail_price/100)*6;}
else if (code == 'a') {commission = (retail_price/100)*6;}
else if (code == 'B') {commission = (retail_price/100)*8;}
else if (code == 'b') {commission = (retail_price/100)*8;}
else if (code == 'C') {commission = (retail_price/100)*10;}
else if (code == 'c') {commission = (retail_price/100)*10;}
else{System.out.println("Invalid code");}
return commission;
}
public static void main(String ...args){
char code;
double commission = 0;
String enumber;
double retail_price=0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter employee number: ");
enumber= scan.nextLine();
System.out.println("Enter retail price: ");
retail_price= scan.nextDouble();
System.out.println("Enter code:");
code=scan.next().charAt(0);
commission = setValue(code, commission, retail_price);
System.out.println("Employee number: "+enumber);
System.out.println("Retail price: "+retail_price);
System.out.println("Commission: "+commission);
}
注意: 该程序无需返回值。你可以在main方法中开展业务。
答案 2 :(得分:0)
问题已解决: 类名未大写 佣金从未初始化 退回
import java.util.Scanner;
// Classes should allways be capitalized
public class Retail
{
public static void main (String[] args)
{
char code;
double commission = 0;
String enumber;
double retail_price=0;
Scanner scan = new Scanner(System.in);
System.out.println("Enter employee number: ");
enumber= scan.nextLine();
System.out.println("Enter retail price: ");
retail_price= scan.nextDouble();
// added user guidance of whats expected
System.out.println("Enter code: A,B,C");
code=scan.next().charAt(0);
if (code == 'A'){ commission = (retail_price/100)*6;}
else if (code == 'a') {commission = (retail_price/100)*6;}
else if (code == 'B') {commission = (retail_price/100)*8;}
else if (code == 'b') {commission = (retail_price/100)*8;}
else if (code == 'C') {commission = (retail_price/100)*10;}
else if (code == 'c') {commission = (retail_price/100)*10;}
else{System.out.println("Invalid code");}
System.out.println("Employee number: "+enumber);
System.out.println("Retail price: "+retail_price);
System.out.println("Commission: "+commission);
}
}