看到题目,只能说你太懒了,哈哈

解决方案 »

  1.   


    import java.math.*;
    import java.util.*;public class CalculateGratuity {    public static void main(final String[] args) {
            try {
                boolean calculateResult = calculateGratuity();
                if (!calculateResult) {
                    System.out.println(ErrorMessage);
                }
            } catch (Exception e) {
                System.out.println(ErrorMessage);
            }
        }    public static boolean calculateGratuity() {
            boolean returnValue = false;
            Scanner scanner = new Scanner(System.in);        try {
                System.out.print("Enter the subtotal and a gratuity rate:");
                String scannerNextLine = scanner.nextLine();
                String[] scannerNextLineArray = scannerNextLine.split(" ");            if (scannerNextLineArray.length == 2) {
                    BigDecimal subtotal = new BigDecimal(scannerNextLineArray[0]);
                    BigDecimal gratuityRate = new BigDecimal(scannerNextLineArray[1]);                // gratuity = subtotal * gratuityRate / 100
                    BigDecimal gratuity
                            = subtotal.multiply(gratuityRate).divide(BigDecimal.valueOf(100.0)).
                            setScale(2, BigDecimal.ROUND_HALF_UP).stripTrailingZeros();                // total = subtotal + gratuity
                    BigDecimal total
                            = subtotal.add(gratuity).
                            setScale(2, BigDecimal.ROUND_HALF_UP).stripTrailingZeros();                System.out.println("The gratuity is " + gratuity + " and total is " + total);                returnValue = true;
                }
            } catch (Exception e) {
                throw (e);
            }        return returnValue;
        }    private static final String ErrorMessage = "Cann't Calculate the Gratuity!";
    }
    输入输出1:(原题的正常值)
    Enter the subtotal and a gratuity rate:15.69 15
    The gratuity is 2.35 and total is 18.04输入输出2:(楼主给出的正常值)
    Enter the subtotal and a gratuity rate:10 15
    The gratuity is 1.5 and total is 11.5输入输出3:(缺少一个值的时候)
    Enter the subtotal and a gratuity rate:10
    Cann't Calculate the Gratuity!输入输出4:(输入值是字母的时候)
    Enter the subtotal and a gratuity rate:subtotal gratuity
    Cann't Calculate the Gratuity!
      

  2.   

    大牛在这,抓住他别让他走
      

  3.   

     楼主 哎 真心不想说哦