请检查是否存在语法错误或者数组越界非法访问等情况

总提示 请检查是否存在语法错误或者数组越界非法访问等情况
请问哪个大牛知道这个代码到底是哪里有语法错误了?

import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int goodsNum = 0;//要购买的商品数量n
        int otherDiscountNum = 0;//满减优惠方式数量m
        double totalPrice = 0;
        double discountPrice = 0;
        int goodsSize = 0;
        int discountSize = 0;

        System.out.println("请输入商品数量和满减优惠方案的数量");
        while (scanner.hasNext()) {
            int number1 = 0, number2 = 0;
            try {
                number1 = Integer.valueOf(scanner.next().trim());
                number2 = Integer.valueOf(scanner.next().trim());
            } catch (Exception e) {
                System.out.printf("%s%n", e.getMessage());
                continue;
            }

            if (goodsNum == 0) {
                if (number1 > 10 || number1 < 1 || number2 > 10 || number2 < 1) {
                    continue;
                }
                goodsNum = number1;
                otherDiscountNum = number2;
            } else if (goodsSize < goodsNum) {
                if (number1 > 100 || number1 < 1) {
                    continue;
                }
                if (number2 > 1 || number2 < 0) {
                    continue;
                }
                totalPrice += number1;
                if (number2 == 1) {
                    discountPrice += number1 * 0.8;
                } else {
                    discountPrice += number1;
                }
                goodsSize++;
            } else if (discountSize < otherDiscountNum) {
                if (number1 <= number2) {
                    continue;
                }
                if (number1 > 1000 || number1 < 1 || number2 > 1000 || number2 < 1) {
                    continue;
                }
                if (totalPrice >= number1) {
                    if (totalPrice - number2 < discountPrice) {
                        discountPrice = totalPrice - number2;
                    }
                }
                discountSize++;
            }
            if (goodsSize == goodsNum && discountSize == otherDiscountNum) {
                System.out.printf("%#.2f%n", discountPrice);
                scanner.close();
                System.exit(-1);
                return;
            }
            if (goodsSize < goodsNum) {
                System.out.println("请输入下一个商品:");
            } else if (discountSize < otherDiscountNum) {
                System.out.println("请输入下一个满减优惠方案:");
            }
        }
        scanner.close();
        System.exit(-1);
    }
}
#笔试题目#
全部评论
import java.text.DecimalFormat; import java.util.Scanner; public class Main {   public static void main(String[] args) {     Scanner sc = new Scanner(System.in);     int n=sc.nextInt();     int m=sc.nextInt();     double discountSum=0;     double totalCut=0;     double countOldSum=0;     double totalCutPrice=0;     for(int i=1;i<=n;i++){       double price = sc.nextDouble();       int isDiscout=sc.nextInt();       if(isDiscout==0){         discountSum+=price;       }else {         discountSum+=price*0.8;       }       countOldSum+=price;     }     totalCutPrice=countOldSum;     for(int i=1;i<=m;i++){        double reachTotal= sc.nextInt();        double count=sc.nextInt();       double cutPrice=0;        if(countOldSum>=reachTotal){          cutPrice=count;        }       totalCutPrice=Math.min(totalCutPrice,countOldSum-cutPrice);     }     double min = Math.min(discountSum, totalCutPrice);     String mins = new DecimalFormat("#.00").format(min);     System.out.println(mins);   } }
点赞 回复 分享
发布于 2018-06-06 11:34
楼主不要打印多余的字符啊。不然会答案错误的。题目让你输出什么就输出什么。
点赞 回复 分享
发布于 2018-06-06 11:40

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务