简体   繁体   中英

My method calcDiscount will not print out

Write a method calcDiscount that accepts two parameters: a double price and a char representing a discount code and returns a double representing the amount of the discount

Compute the amount of the discount given the following table of discount codes: A 5% D 10% N 15% E 20%

If the discount code is not A, D, N, or E, the discount returned should be 0. Note that you are NOT returning the discounted price, but the actual amount of the discount, so calling calcDiscount with 5.00 and D as arguments should result in an answer of 5.00

I tried the code, and I think its what I need per the instructions, but I cant get the method to print out.

package edu.ilstu;

import java.util.Scanner;

public class ClassOne {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter a price");
        double price = scan.nextDouble();
        System.out.println("Enter a discount code");
        char c = scan.next().charAt(0);
    }

    public static double calcDiscount(double price, char c) {
        switch (c) {
            case 'A':
                double a = (price * .05);
                return a;

            case 'D':
                double d = (price * .10);
                return d;
            case 'N':
                double n = (price * .15);
                return n;

            case 'E':
                double e = (price * .20);
                return e;

            default:
                double z = 0;
                return z;
        }
        System.out.println(ClassOne.calcDiscount());
    }
}
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter a price");
        double price = scan.nextDouble();
        System.out.println("Enter a discount code");
        char c = scan.next().charAt(0);
        double discount = calcDiscount(price, c);
        System.out.println("Discount: " + discount);
    }

You should call the calcDiscount method from your main method. The print statement at the end of the calcDiscount method is incorrect.

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