简体   繁体   English

如何在java中找到5个整数的中位数

[英]How to find the median of 5 integers in java

So I'm new to programming and my teacher wants us to find the medium using if statements (I've asked him but he's not really helping) we haven't learned scanner, loops, arrays, etc.. so all I know is bufferedReader, import.java.io & the basics .所以我是编程新手,我的老师希望我们找到使用 if 语句的媒介(我问过他,但他并没有真正帮助)我们还没有学过扫描仪、循环、数组等......所以我所知道的是bufferedReader、import.java.io 和基础知识。 I've written out the if statements, but I'm still confused as to how to get it to output the median.我已经写出了 if 语句,但我仍然对如何让它输出中位数感到困惑。 The numbers are 1-5 so I know that the median is 3.. Here's what I have for the if statements.数字是 1-5,所以我知道中位数是 3.. 这是我对 if 语句的了解。

        if (num1<num2) 
            System.out.println(num1 + "is less than" + num2);
        if (num1<num3)
            System.out.println(num1 + "is less than" + num3);
        if (num1<num4)
            System.out.println(num1 + "is less than" + num4);
        if (num1<num5)
            System.out.println(num1 + "is less than" + num5);
        if (num2>num1)
            System.out.println(num2 + "is greater than" + num1);    
        if (num2<num3)
            System.out.println(num2 + "is less than" + num3);
        if (num2<num4)
            System.out.println(num2 + "is less than" + num4);
        if (num1<num5)
            System.out.println(num1 + "is less than" + num5);
        if (num3>num1)
            System.out.println(num3 + "is greater than" + num1);
        if (num3>num2)
            System.out.println(num3 + "is greater than" + num2);
        if (num3<num4)
            System.out.println(num3 + "is less than" + num4);
        if (num3<num5)
            System.out.println(num3 + "is less than" + num5);
        if (num4>num1)
            System.out.println(num4 + "is greater than" + num1);
        if (num4>num2)
            System.out.println(num4 + "is greater than" + num2);
        if (num4>num3)
            System.out.println(num4 + "is greater than" + num3);
        if (num4<num5)
            System.out.println(num4 + "is less than" + num5);
        if (num5>num1)
            System.out.println(num5 + "is greater than" + num1);
        if (num5>num2)
            System.out.println(num5 + "is greater than" + num1);
        if (num5>num3)
            System.out.println(num5 + "is greater than" + num3);
        if (num5>num4)
            System.out.println(num5 + "is greater than" + num4);

I don't want to give you the answer;我不想给你答案; but here are a few tips.但这里有一些提示。

if statements can take multiple conditions within the brackets such as: if语句可以在括号内采用多个条件,例如:

if(a>b && b < 10){...}

You can also nest if statements within each other:您还可以嵌套if语句:

if(a>b){
   if(b<10){...}
}

And finally you can have an else condition, which will be executed when the condition is not met最后你可以有一个else条件,当条件不满足时会执行

if(a>b){
   ...
} else {
   ...
}

Of course these can all be combined together, nested ifs with multiple conditions and ifs nested into else blocks.当然,这些都可以组合在一起,具有多个条件的嵌套 if 和嵌套到 else 块中的 if。

As I said, I don't want to give you the answer to your question as I don't believe that would be the most effective way of learning how to do this;正如我所说,我不想回答你的问题,因为我认为这不是学习如何做到这一点的最有效方法; but hopefully this is somewhat insightful and meaningful to your knowledge and understanding.但希望这对您的知识和理解有一定的洞察力和意义。

What if you have 555 numbers?如果你有 555 个号码怎么办? Would you write again all possible combinations?你会再写一遍所有可能的组合吗? That would literally take days.这实际上需要几天时间。

Consider using some data structures, for example use array to store your numbers:考虑使用一些数据结构,例如使用数组来存储您的数字:

int[] array = new int[] {1, 2, 3, 4, 5};

The median would be element in the middle, 3.中位数将是中间的元素,3。

int[] array2 = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9};

The median would be 5 here.这里的中位数是 5。

To find median (let's assume the numbers are always in order) you can do something like this:要找到中位数(假设数字总是按顺序排列),您可以执行以下操作:

  1. Find out what is the size of array (how many elements you have)找出数组的大小(你有多少个元素)
  2. If size is odd, then the median is element in the middle如果大小为奇数,则中位数是中间的元素
  3. If size is even, then the median is a sum of two middle elements divided by 2如果大小为偶数,则中位数是两个中间元素的总和除以 2

Example using array:使用数组的示例:

 int[] array = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 int size = array.length;
    if (size % 2 == 1) { // odd
        System.out.println("Median: : " + array[size / 2]);
    } else { // even
        int sum = array[size / 2 - 1] + array[size / 2];
        System.out.println("Median: : " + (float) sum / 2);
    }

However, I have a feeling that even though this code is very simple, it can still be quite incomprehensible to you.但是,我有一种感觉,即使这段代码非常简单,您仍然可能无法理解。 I would strongly advise that you read about arrays and collections in Java - just to save you some time.我强烈建议您阅读 Java 中的数组和集合 - 只是为了节省一些时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM