java程序出现运行时错误
我得到了一个由3位数字组成的列表,我想看看它们是否按降序排列。列表中的元素数量尚未确定,但我已将我的java程序出现运行时错误,java,Java,我得到了一个由3位数字组成的列表,我想看看它们是否按降序排列。列表中的元素数量尚未确定,但我已将我的SENTINEL值设置为1000 但以下错误仍在发生: CompileRunTest: throwable = java.util.NoSuchElementException java.util.NoSuchElementException 我的代码: import java.util.Scanner ; public class StrictDescending { public
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() ;
}
}
}
import java.util.Scanner;
公共课{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
最终int哨兵=1000;
int firstValue=in.nextInt();
而(第一个值<1000)
{
int secondValue=in.nextInt();
而(第二个值<1000)
{
如果(第一个值>第二个值)
{
System.out.println(“是的,列表按降序排列”);
}
其他的
{
System.out.println(“不,列表不是按降序排列的。”);
}
secondValue=in.nextInt();
}
firstValue=in.nextInt();
}
}
}
尝试将第一个while切换到if语句,在secondValue=in.nextInt()之前的行中添加firstValue=secondValue
;
并删除.nextInt()中的最后一个firstValue=代码>您还需要稍微处理一下打印语句
事实上,您的程序流程没有什么意义,因为即使没有剩余的数字,您也会尝试使用stdin。已编辑的答案
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);
}
publicstaticvoidmain(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
int-previousValue=getInt(sc);
while(数学绝对值(以前的值)<1000){
如果(previousValue=1000),则以下可能是一种解决方案(相等的值不支持降序):
publicstaticvoidmain(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
整数previousValue=getInt(sc);
布尔下降=假;
while(上一个值!=null){
整数currentValue=getInt(sc);
if(currentValue!=null){
如果(previousValue>(previousValue=currentValue)){
if(!descending){//if尚未为true
descending=!descending;//设置为true
}
}否则{
descending=false;//要打印:“否,…”
previousValue=null;//中断循环
}
}否则{
previousValue=currentValue;//它实际上是空的
}
}
System.out.println((降序)
?“是的,列表按降序排列。”
:“不,列表不是按降序排列的。”);
}
公共静态整数getInt(扫描程序sc){
如果(!sc.hasnetint()){
sc.next();
System.out.println(“请只键入数字”);
返回getInt(sc);
}
int input=sc.nextInt();
返回(数学abs(输入)<1000)?输入:空;
}
你的代码没问题,再试一次,如果没有更多的输入,错误会被抛出。你如何测试它?145 130 125 100 1000和145 130 130 125 1000这是两个不同的给定输入为什么要声明SENTINEL=1000;
如果你没有在任何地方使用它?不要破坏你的帖子。好的,我为输入145 130 125 100做了这些1000,它给了我是的,列表按降序排列(重复3次)我会让你算出那部分的。我不能帮你做所有的家庭作业;)我在试着比较x数量的数字,x值不是给定的只是一个数字列表,但你的意思是当下一个整数大于或等于上一个整数时它停止了吗?或者可能我不理解这个问题。我需要更多的细节。@javahelp对不起。你写了3位数的nu成员,而不是3个数字”)谢谢你的时间,所以给我一个三位数整数的列表,我需要确保列表严格按照降序排列,如果一个数字不是降序排列,它应该停止并打印出来否,数字不是降序排列,相等的连续数字也不是降序排列
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;
}