比较IF语句中的多个整数,Java [英] Comparing multiple integers in IF statement, Java

查看:114
本文介绍了比较IF语句中的多个整数,Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编码很陌生。我目前正在使用Deitel的Java:如何编程,其中一个早期练习让我感到难过。

I am brand new to coding. I'm currently using "Java:How to program" by Deitel, and one of the early exercises has me stumped.

它要求我使用简单的第1章if语句比较5个整数(由用户输入)并显示最大值和最小值。我仅限于第一章中使用的语言,因此我假设我可以在if语句后面的括号中列出int值。类似于:

It is asking me to use simple, chapter 1 if statements to compare 5 integers (input by the user) and display both the largest and the smallest. I am limited to the language used in the first chapter, so I made the assumption that I could list int values in the parenthetical after the if statement. Something like:

int firstInt;
int secondInt;
int thirdInt;
int fourthInt;
int fifthInt;

if (first Int> secondIt, thirdInt, fourthInt, fifthInt)

等等但这是一个语法错误。有谁知道如何同时比较多个值?谷歌搜索被证明没有帮助,因为所有答案都有一些我不理解的复杂解决方案。

etc. but this is a syntax error. Does anyone know how to compare multiple values simultaneously? A google search proves to be no help since all the answers have some convoluted solution that I don't understand.

如果我尝试这样的事情:

If I try something like this:

     if (one > two)
     System.out.println("%d is the largest number!", one);

     if (one > three)

等。对于所有1 - 5,我遇到的问题是当我继续使用if语句1> 2,3,4,5 2> 1,3,4,5等时,每个整数都会有1个为真,除非是

etc. for all 1 - 5, The issue I run into is when I continue with if statements 1>2,3,4,5 2>1,3,4,5 etc. then there will be 1 true for each integer regardless except for the smallest number, and it will display 4 outputs, when all I'm after is the larges and the smallest.

我很难过,只有最小的数字,它会显示4个输出。

I'm stumped.

推荐答案

表达式:

firstInt > secondInt, thirdInt, fourthInt, fifthInt

无效,因为Java是一种计算机语言而不是一种自然语言(比如英语)。

is not valid because Java is a computer language rather than a natural language (like English).

如果你想这样做,你需要的是的结合,具有多个完整条件(使用 a,b,c,d,e 以最小化代码):

If you want to do it that way, what you need is the and conjunction, with multiple complete conditions (using a,b,c,d,e to minimise the code):

if ((a >= b) && (a >= c) && (a >= d) && (a >= e)) { // a >= b,c,d,e
    System.out.println ("Largest is: " + a);
} else if ((b >= c) && (b >= d) && (b >= e)) {      // b >= c,d,e
    System.out.println ("Largest is: " + b);
} else if ((c >= d) && (c >= e)) {                  // c >= d,e
    System.out.println ("Largest is: " + c);
} else if (d >= e) {                                // d >= e
    System.out.println ("Largest is: " + d);
} else {                                            // e > d
    System.out.println ("Largest is: " + e);
}

请记住,一旦你决定 a (或后续步骤中的任何其他内容)不是最大的,您永远不需要再次检查,因为其中一个比它大。

keeping in mind that, once you decide a (or any of the others in later steps) isn't the largest, you never need to check it again, as one of the others will be larger than it.

或者你可以使用Java标准的设施 Math package,其中 Math.max(a,b)将为您提供 a b

Or you can use the facilities of the Java standard Math package, where Math.max(a,b) will give you the largest of the values from a and b:

int largest = Math.max(a,Math.max(b,Math.max(c,Math.max(d,e))));

在这里,您将所有五个数字中最大的一个定义为该对的最大值 {a,maximum-of-bcde} 然后使用类似的方法计算 maximum-of-bcde 作为集合的最大值 {b,maximum-of-cde} 依此类推。

Here, you define the largest of all five numbers as the maximum of the pair {a, maximum-of-bcde} then use a similar method to work out maximum-of-bcde as the maximum of the set {b, maximum-of-cde} and so on.

或者换句话说,从中选择最大的 {d,e} 并将其称为 x 。然后从 {c,x} 中选择最大值,并将其命名为 y 。然后是来自 {b,y} 的最大值,并将其称为 z 。最后来自 {a,z} 的最大值,这是你五个中最大的值。

Or, in other words, choose the largest from {d,e} and call it x. Then choose the largest from {c,x} and call it y. Then the largest from {b,y} and call it z. Finally the largest from {a,z} and that's your largest value of the five.

尽管如此老实说,如果你是介绍性文本的第一章,它可能还没有在Java标准库中覆盖太多。

Though, to be honest, if you're on chapter one of an introductory text, it probably hasn't covered much in the Java standard libraries yet.

但是,如果您正在使用一种方法可能更容易让初学者理解流程,我认为您不能超越这个:

But, if you're after a method where it's perhaps easier for a beginner to understand the flow, I don't think you can go past this one:

int maxOfFive (int a, int b, int c, int d, int e) {
    int largest = a;
    if (b > largest) largest = b;
    if (c > largest) largest = c;
    if (d > largest) largest = d;
    if (e > largest) largest = e;
    return largest;
}

这是人们会做的最大值的基本选择它,逐个扫描列表,只记住哪个值最高,然后返回。

This is basic selection of the largest value as a person would do it, scanning the list one by one and just remembering which value was the highest, then returning that.

或者,如果你还没有学习过函数(并恢复原始变量),只需实现相同的代码,无需函数调用:

Or, if you haven't learned about functions yet (and reverting to your original variables), just implement the same code without a function call:

int largest = firstInt;
if (secondInt > largest) largest = secondInt;
if (thirdInt  > largest) largest = thirdInt;
if (fourthInt > largest) largest = fourthInt;
if (fifthInt  > largest) largest = fifthInt;
System.out.println ("Largest is: " + largest);

我相信这可能只是使用了你已经表明过的东西(变量,单个 - condition if statement和output)。

I believe that's probably only using stuff you've already indicated that you know about (variables, single-condition if statement, and output).

这篇关于比较IF语句中的多个整数,Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆