检测到阵列无序的元素时添加错误消息

时间:2016-11-27 15:42:45

标签: javascript java arrays

我必须创建代码,将两个数组与所有元素按顺序合并,从最低到最高。如果找到了乱序元素,我应该打印消息“错误:数组不正确”。我有所有代码工作,除了我不知道在哪里或如何添加此错误消息。

示例错误输出:

输入第一个数组的值,最多10000个值,输入零或负数以退出

4 五 7 2 -1

输入第二个数组的值,最多10000个值,输入零或负数以退出 3 3 3 3 3 3 0

First Array: 4 5 7 2

第二阵列: 3 3 3 3 3 3

错误:数组的顺序不正确

这是我的代码:

import java.util.Scanner;
import java.lang.Math;

class Main {


public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    int one[]= new int[10000];  
    int two[]= new int[10000];  
    int lengthShort=0;          
    int lengthLong=0;           
    int a =0;                   
    int b =0;
    System.out.println("Enter the values for the first array, up to 10000 values, enter zero or a negative number to quit");
    for(int i=0; i<one.length; i++){

        one[i] = scan.nextInt();
        a++;
        if(one[i]<=0){
            one[i]=0;
            break;
        }
    }
    int length1 = a-1;

    System.out.println("Enter the values for the second array, up to 10000 values, enter zero or a negative number to quit");
    for(int i=0; i<two.length; i++){

        two[i] = scan.nextInt();
        b++;
        if(two[i]<=0){
            two[i]=0;
            break;
        } 

    }


    int lengthTwo = b-1;


    int mergeOne[] = new int[length1];  
    for (int i = 0; i<mergeOne.length; i++){
        mergeOne[i]=one[i];
    }
    int mergeTwo[] = new int[lengthTwo];   
    for (int i = 0; i<mergeTwo.length; i++){
        mergeTwo[i]=two[i];
    }


    System.out.println("First Array:");

    for(int i=0; i<mergeOne.length; i++){
    System.out.print(mergeOne[i] + " ");
    }

    System.out.println("\nSecond Array:");

    for(int i=0; i<mergeTwo.length; i++){
    System.out.print(mergeTwo[i] + " ");
    }


    if(mergeOne.length<=mergeTwo.length){
        lengthLong = mergeTwo.length;
        lengthShort = mergeOne.length;
    }

    else if(mergeOne.length>=mergeTwo.length){
        lengthShort = mergeTwo.length;
        lengthLong = mergeOne.length;
    }

    int merged[] = new int[length1 + lengthTwo];


    for(int i = 0; i<lengthShort; i++){
        if(i==0){
            if(mergeOne[i]<=mergeTwo[i]){
                merged[i] = mergeOne[i];
                merged[i+1] = mergeTwo[i];
            }
            else if(mergeTwo[i]<=mergeOne[i]){
                merged[i] = mergeTwo[i];
                merged[i+1]= mergeOne[i];
            }
        }
        else if(i>0){
            if(mergeOne[i]<=mergeTwo[i]){
                merged[i+i] = mergeOne[i];
                merged[i+i+1] = mergeTwo[i];
            }
            else if(mergeTwo[i]<=mergeOne[i]){
                merged[i+i] = mergeTwo[i];
                merged[i+i+1]= mergeOne[i];
            }
        }
    }
    if(mergeOne.length<mergeTwo.length){
        for(int k=lengthShort; k<lengthLong; k++){
            merged[k]=mergeTwo[k];
        }
    }
    if(mergeOne.length>mergeTwo.length){
            for(int k=lengthShort; k<lengthLong; k++){
                merged[k]=mergeOne[k];
        }
    }

    for(int i = 0; i<merged.length; i++){
        if((i+1)==merged.length)       
            break;
        if(merged[i]>merged[i+1]){
            int temp = merged[i+1];
            merged[i+1]=merged[i];
            merged[i]= temp;

        }
    }

    System.out.println("\nMerged Array: ");

    for(int i = 0; i<merged.length; i++){
    System.out.print(merged[i] + " ");
    } 
}
}

2 个答案:

答案 0 :(得分:0)

如果要在发现错误时停止处理,则抛出异常。在程序的最根本部分(主程序或任何你称之为的程序)捕获该异常并显示异常的值。

如果您不希望处理停止 - 那就是您希望继续处理并寻找其他问题 - 那么您必须在执行处理的代码中显示错误。

这是一个命令行程序吗?如果是这样,那么System.out.println将向控制台输出您想要的任何字符串值。

答案 1 :(得分:0)

在JavaScript中,您可以实现一个isSorted()方法,该方法将返回一个类似于:

的布尔值

var isSorted = a => {
  var result = true;
  a.forEach((x,i,arr) => {
    if (i < arr.length && arr[i+1] < x) result = false;
  })
  return result;
}
console.log(isSorted([1,2,3,6,8,12]));
console.log(isSorted([1,2,3,-6,8,12]));