简体   繁体   中英

incompatible types: unexpected return value

i'm new to java and am trying to return the value of commission to be printed out in the last line. but i keep getting the incompatible types: unexpected return value error.

  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);
    }
}

You do not need to return values in the main method. Just initilize commission to 0 when you declare it and remove the return.

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);
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);
}}

you are returning inside main method.so its showing error. if you want to return values, you should use like this,

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);

        }

Note: This program no need to return the value. you can do your business inside main method.

issues fixed: class name not capitalized commission never initialized return removed

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM