编写一个方法calcDiscount,该方法接受两个参数:双倍价格和代表折扣代码的字符,并返回代表折扣金额的双精度
根据下表的折扣代码计算折扣金额: 5% D 10% N 15% E 20%
如果折扣代码不是A,D,N或E,则返回的折扣应为0。请注意,您返回的不是折扣价,而是折扣的实际金额,因此请使用5.00和D调用calcDiscount因为参数应得到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());
}
}
答案 0 :(得分:0)
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);
}
您应该从主方法中调用calcDiscount方法。 calcDiscount方法末尾的打印语句不正确。