确定Java中的第二低和第二高值

时间:2015-08-11 08:40:50

标签: java sorting

这不仅在输入两个或多个相同值时有效,它将使输出全为零,而不是给出与第一个最高和第二个最高相同的结果。我们也不能使用数组和数组或任何排序函数。

例如,如果我输入4 4 2 2 1,它将使所有结果为0而不是给出:

first highest: 4
second highest:4
second lowest :2
first lowest :1

这是我的代码:

import java.util.Scanner;

public class Pres {

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.print("Enter 5 numbers: ");
    int fh = 0, sh = 0, sl = 0, fl = 0;
    int a = s.nextInt();
    int b = s.nextInt();
    int c = s.nextInt();
    int d = s.nextInt();
    int e = s.nextInt();
    if (a < b && a < c && a < d && a < e) {
        fl = a;
    }  if (b < a && b < c && b < d && b < e) {
        fl = b;
    }  if (c < a && c < b && c < d && c < e) {
        fl = c;
    }  if (d < a && d < b && d < c && d < e) {
        fl = d;
    }  if (e < a && e < b && e < c && e < d) {
        fl = e;
    }
    if (a > b && a > c && a > d && a > e) {
        fh = a;
    }  if (b > a && b > c && b > d && b > e) {
        fh = b;
    }  if (c > a && c > b && c > d && c > e) {
        fh = c;
    }  if (d > a && d > b && d > c && d > e) {
        fh = d;
    }  if (e > a && e > b && e > c && e > d) {
        fh = e;
    }
    //sh
    if (a > b && a > c && a > d && a < e) {
        sh = a;
    }  if (a > b && a > c && a > d && a < e) {
        sh = a;
    }  if (a < b && a > c && a > d && a > e) {
        sh = a;
    }  if (a > b && a < c && a > d && a > e) {
        sh = a;
    }  if (a > b && a > c && a < d && a > e) {
        sh = a;
    }
    if (b > a && b > c && b > d && b < e) {
        sh = a;
    }  if (b > a && b > c && b > d && b < e) {
        sh = b;
    }  if (b < a && b > c && b > d && b > e) {
        sh = b;
    }  if (b > a && b < c && b > d && b > e) {
        sh = b;
    }  if (b > a && b > c && b < d && b > e) {
        sh = b;
    }
    if (c > b && c > a && c > d && c < e) {
        sh = c;
    }  if (c > b && c > a && c > d && c < e) {
        sh = c;
    }  if (c < b && c > a && c > d && c > e) {
        sh = c;
    }  if (c > b && c < a && c > d && c > e) {
        sh = c;
    }  if (c > b && c > a && c < d && c > e) {
        sh = c;
    }
    if (d > b && d > c && d > a && d < e) {
        sh = d;
    }  if (d > b && d > c && d > a && d < e) {
        sh = d;
    }  if (d < b && d > c && d > a && d > e) {
        sh = d;
    }  if (d > b && d < c && d > a && d > e) {
        sh = d;
    }  if (d > b && d > c && d < a && d > e) {
        sh = d;
    }
    if (e > b && e > c && e > d && e < a) {
        sh = e;
    }  if (e > b && e > c && e > d && e < a) {
        sh = e;
    }  if (e < b && e > c && e > d && e > a) {
        sh = e;
    }  if (e > b && e < c && e > d && e > a) {
        sh = e;
    }  if (e > b && e > c && e < d && e > a) {
        sh = e;
    }
    //sl
    if (a < b && a < c && a < d && a > e) {
        sl = a;
    }  if (a > b && a > c && a > d && a < e) {
        sl = a;
    }  if (a < b && a > c && a > d && a > e) {
        sl = a;
    }  if (a > b && a < c && a > d && a > e) {
        sl = a;
    }  if (a > b && a > c && a < d && a > e) {
        sl = a;
    }
    if (b < a && b < c && b < d && b > e) {
        sl = a;
    }  if (b < a && b < c && b < d && b > e) {
        sl = b;
    }  if (b > a && b < c && b < d && b < e) {
        sl = b;
    }  if (b < a && b > c && b < d && b < e) {
        sl = b;
    }  if (b < a && b < c && b > d && b < e) {
        sl = b;
    }
    if (c < b && c < a && c < d && c > e) {
        sl = c;
    }  if (c < b && c < a && c < d && c > e) {
        sl = c;
    }  if (c > b && c < a && c < d && c < e) {
        sl = c;
    }  if (c < b && c > a && c < d && c < e) {
        sl = c;
    }  if (c < b && c < a && c > d && c < e) {
        sl = c;
    }
    if (d < b && d < c && d < a && d > e) {
        sl = d;
    }  if (d < b && d < c && d < a && d > e) {
        sl = d;
    }  if (d > b && d < c && d < a && d < e) {
        sl = d;
    }  if (d < b && d > c && d < a && d < e) {
        sl = d;
    }  if (d < b && d < c && d > a && d < e) {
        sh = d;
    }
    if (e < b && e < c && e < d && e > a) {
        sl = e;
    }  if (e < b && e < c && e < d && e > a) {
        sl = e;
    }  if (e > b && e < c && e < d && e < a) {
        sl = e;
    }  if (e < b && e > c && e < d && e < a) {
        sl = e;
    }  if (e < b && e < c && e > d && e < a) {
        sl = e;
    }
    if (a < fl) {
        sh = fl;
        fl = a;
    }
    if ((a > fl) && (a < sl)) {
        sl = a;
    }
    if (b < fl) {
        sl = fl;
        fl = b;
    }
    if ((b > fl) && (b < sl)) {
        sl = b;
    }
    if (c < fl) {
        sh = fl;
        fl = c;
    }
    if ((c > fl) && (c < sl)) {
        sh = c;
    }
    if (d < fl) {
        sl = fl;
        fl = d;
    }
    if ((d > fl) && (d < sl)) {
        sl = d;
    }
    if (e < fl) {
        sl = fl;
        fl = e;
    }
    if ((e > fl) && (e < sl)) {
        sl = e;
    }
    else
    fh = sh;
    fl = sh;
    sl = sh;
    sh = fl;


    System.out.println("First highest  :" + fh);
    System.out.println("Second highest :" + sh);
    System.out.println("First lowest   :" + fl);
    System.out.println("Second lowest  :" + sl);

3 个答案:

答案 0 :(得分:1)

对于这类问题,此代码到目前为止很复杂。尽量缩短时间以简化操作 - 这也可以使调试变得更加容易:

int[] inp = new int[5];

//read input into array here

//here we have two options: 
//either use the javaAPI to sort the array:
Arrays.sort(inp);

int min2 = inp[1];//second smallest element
int max2 = inp[3];//second highest element

//search for the value on your own:
int min = Integer.MAX_VALUE;//searching for a minimum -> greatest possible value
int min2 = min;

int max = Integer.MIN_VALUE;
int max2 = max;

for(int i : inp){
    if(i < min){
        min2 = min;
        min = i;
    }else if(i < min2){
        min2 = i;
    }

    if(i > max){
        max2 = max;
        max = i;
    }else if(i < max2){
        max2 = i;
    }
} 

可能代码的这一部分是问题所在:

else
fh = sh;

fl = sh;
sl = sh;
sh = fl;

由于没有括号,最后3行将始终执行,与条件是否满足无关。

答案 1 :(得分:1)

public class Num {

public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.print("Enter 5 numbers: ");
    int fh = 0, sh = 0, sl = 0, fl = 0;
    int difference1 = 1;
    int difference2 = 1;
    boolean t1=false, t2=false;
    int temp = 0, temp1, temp2;
    int a = s.nextInt();//for first number
    fh = sh = sl = fl = a;
    for (int i = 2; i <= 5; i++) {
        int b;
        t2=false;
        t1=false;
        b = s.nextInt();
        if (b > fh) {
            sh=fh;
            fh = b;
            t1 = true;
        }
        if (b < fl) {
            sl=fl;
            fl = b;
            t2 = true;
        }
        if (t2 == false) {
            temp1 = b - fh;     
            if (temp1 < difference1) {
                difference1 = temp1;
                sl = b;
            }
        }
        if (t1 == false) {
             temp2 = b - fl;
            if (temp2 > difference2) {
                difference2 = temp2;
                sh = b;
            }
        }
    }
    System.out.println("First lowest   :" + fl);
    System.out.println("Second lowest  :" + sl);
    System.out.println("Second highest :" + sh);
    System.out.println("First highest  :" + fh);

}

}

这段代码工作 我试用这个代码4个小时 我更改了我的代码plz删除评论自爆

答案 2 :(得分:1)

由于您不想使用数组和排序,因此稍微硬编码的解决方案对您有用。基本思想是两次解析输入,并将它们传递给一组硬编码的if()条件以找到最高数字。在第一步中找到最高的数字并为其分配Integer.MIN_VALUE的值,以便在输入的第二步中通过同一组if()条件时,不考虑第一个最高值,现在确定的最高值将是第二高值。以下是查找最高和第二高数字的工作代码。您可以将其扩展为查找最低和最低的数字。

public class Test {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("Enter 5 numbers: ");
        int fh = 0, sh = 0, sl = 0, fl = 0;
        int a = s.nextInt();
        int b = s.nextInt();
        int c = s.nextInt();
        int d = s.nextInt();
        int e = s.nextInt();

        for (int i = 1; i < 3; i++) {

            if (a > b && a > c && a > d && a > e) {
                // a is highest
                if (i == 1) {
                    //for first iteration, since a is highest store its value in fh and assing it value Integer.MIN_VALUE
                    fh = a;
                    a = Integer.MIN_VALUE;
                    continue;
                }
                if (i == 2) {
                   //for second iteration, now highest number will be the second highest so store it in sh. Similar for all other inputs.
                    sh = a;
                    continue;
                }
            } else if (b > c && b > d && b > e) {
                // b is highest
                if (i == 1) {
                    fh = b;
                    b = Integer.MIN_VALUE;
                    continue;
                }
                if (i == 2) {
                    sh = b;
                    continue;
                }

            } else if (c > d && c > e) {
                // c is highest
                if (i == 1) {
                    fh = c;
                    c = Integer.MIN_VALUE;
                    continue;
                }
                if (i == 2) {
                    sh = c;
                    continue;
                }

            } else if (d > e) {
                // d is highest
                if (i == 1) {
                    fh = d;
                    d = Integer.MIN_VALUE;
                    continue;
                }
                if (i == 2) {
                    sh = d;
                    continue;
                }

            } else {
                // e is highest
                if (i == 1) {
                    fh = e;
                    e = Integer.MIN_VALUE;
                    continue;
                }
                if (i == 2) {
                    sh = e;
                    continue;
                }

            }

        }
        s.close();
        System.out.println("First highest is:- " + fh);
        System.out.println("Second highest is:- " + sh);
    }

}
相关问题