使用java程序运行时出错

时间:2016-06-09 17:11:09

标签: java

我收到了一个包含3位数字的列表,我正在尝试查看它们是否按降序排列。列表中的元素数量尚未确定,但我已将SENTINEL值设置为1000

以下错误仍在继续:

CompileRunTest: throwable = java.util.NoSuchElementException
java.util.NoSuchElementException

我的代码:

import java.util.Scanner ;

public class StrictDescending {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        final int SENTINEL = 1000 ;
        int firstValue = in.nextInt();
        while (firstValue < 1000)
        {
            int secondValue = in.nextInt() ;
            while(secondValue < 1000)
            {
                if(firstValue > secondValue)
                {
                    System.out.println("Yes, the list is in descending order.") ;
                }
                else
                {
                    System.out.println("No, the list is not in descending order.") ;
                }

                secondValue = in.nextInt();
            }

            firstValue = in.nextInt() ;
        }
    }

}

2 个答案:

答案 0 :(得分:1)

尝试将第一个切换到if语句,在firstValue = secondValue之前的行上添加secondValue = in.nextInt(); 并删除最后一个firstValue = in.nextInt();您需要弄乱您的print语句一点点。

由于你的程序流程并不合理,因为即使没有数字,你也会尝试从stdin中消费。

答案 1 :(得分:0)

编辑回答

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int previousValue = getInt(sc);
    while(Math.abs(previousValue) < 1000) {
        if (previousValue <= (previousValue = getInt(sc))) {
            System.out.println("No, the list is not in descending order.");
            break;
        }
        System.out.println("Yes, the list is in descending order.");
    }
}

public static int getInt(Scanner sc) {
    if (sc.hasNextInt()) {
        return sc.nextInt();
    }
    sc.next();
    System.out.println("Type only numbers, please.");
    return getInt(sc);
}

如果您希望在停止输入数字后打印消息(通过键入值&gt; =超过1000),以下可能是一个可能的解决方案(相等的值不支持降序):

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    Integer previousValue = getInt(sc);
    boolean descending = false;
    while(previousValue != null) {
        Integer currentValue = getInt(sc);
        if (currentValue != null) {
            if (previousValue > (previousValue = currentValue)) {
                if (!descending) { // if not yet true
                    descending = !descending; // set true
                }
            } else {
                descending = false; // to print: "No, ..."
                previousValue = null; // to break the loop
            }
        } else {
            previousValue = currentValue; // it's actually null
        }
    }
    System.out.println((descending)
        ? "Yes, the list is in descending order." 
        : "No, the list is not in descending order.");
}

public static Integer getInt(Scanner sc) {
    if (!sc.hasNextInt()) {
        sc.next();
        System.out.println("Type only numbers, please.");
        return getInt(sc);
    }
    int input = sc.nextInt();
    return (Math.abs(input) < 1000) ? input : null;
}