编写一个方法calcDiscount,它接受两个参数:double price和表示折扣代码的char,并返回表示折扣金额的double
根据下表折扣代码计算折扣金额:A 5%D 10%N 15%E 20%
如果折扣代码不是A,D,N或E,则返回的折扣应为0.请注意,您不会返回折扣价,而是折扣的实际金额,因此调用calcDiscount以5.00和D作为参数应该得到5.00的答案
我尝试了代码,我认为它是我需要的说明,但我不能得到打印方法。
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);
}
您应该从main方法调用calcDiscount方法。 calcDiscount方法末尾的print语句不正确。