比较IF语句中的多个整数,Java
我是编码的新手。 我目前正在使用Deitel的“Java:如何编程”,其中一个早期的练习令我难过。
它要求我使用简单的第1章if语句来比较5个整数(由用户输入)并显示最大值和最小值。 我仅限于第一章中使用的语言,因此我假设我可以在if语句后面的括号中列出int值。 就像是:
int firstInt; int secondInt; int thirdInt; int fourthInt; int fifthInt; if (first Int> secondIt, thirdInt, fourthInt, fifthInt)
等等,但这是一个语法错误。 有谁知道如何同时比较多个值? 谷歌搜索被certificate没有帮助,因为所有答案都有一些我不理解的复杂解决方案。
如果我尝试这样的事情:
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个为真除了最小的数字,它将显示4个输出,当我所追求的是大和最小。
我很难过。
表达方式:
firstInt > secondInt, thirdInt, fourthInt, fifthInt
是无效的,因为Java是一种计算机语言而不是一种自然语言(如英语)。
如果你想这样做,你需要的是and
连接,有多个完整的条件(使用a,b,c,d,e
来最小化代码):
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
) 不是最大的,你永远不需要再次检查,因为其中一个将比它大。
或者您可以使用Java标准Math
包的工具,其中Math.max(a,b)
将为您提供a
和b
最大的值:
int largest = Math.max(a,Math.max(b,Math.max(c,Math.max(d,e))));
在这里,您将所有五个数中最大的{a, maximum-of-bcde}
定义为对的最大值{a, maximum-of-bcde}
然后使用类似的方法计算出maximum-of-bcde
作为集合的最大值{b, maximum-of-cde}
等等。
或者,换句话说,从{d,e}
选择最大值并将其命名为x
。 然后从{c,x}
选择最大值并将其命名为y
。 然后是{b,y}
最大的并称之为z
。 最后来自{a,z}
的最大值,这是你五个中最大的值。
但是,老实说,如果您正在阅读介绍性文本的第一章,那么Java标准库中可能还没有涵盖太多内容。
但是,如果你正在采用一种方法,让初学者更容易理解这个流程,我认为你不能超越这个:
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; }
这是人们做的最大值的基本选择,逐个扫描列表,只记住哪个值最高,然后返回。
或者,如果您尚未学习函数(并恢复到原始变量),只需在没有函数调用的情况下实现相同的代码:
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);
我相信这可能只是使用你已经表明过你所知道的东西(变量,单一条件if
语句和输出)。
如果你知道要比较使用数组的整数数量,我建议你。 以下是示例代码 –
这不是真正的答案,因为我没有使用if语句。 虽然这是获得你想要的东西的一种方式。 因为你是java的新手,想分享我的方法。
import java.util.Arrays; public class JavaTest { public static void main(String[] args) { int values[] = {2,6,10,4,8}; Arrays.sort(values); System.out.println("Min value : "+values[0]); System.out.println("Max value : "+values[values.length-1]); } }
和java一起度过愉快的时光。
使用多个整数来存储数字确实有效但是它不是编码方式。 你应该至少使用一个数组。
arrays速成课程:
Array是一组具有相似数据类型的变量。
要创建一个数组:
[] arrayName = new [size]; //Example int[] myValues = new int[5]; //create 5 int variables. myValues[0]; //is similar to your firstInt myValues[1]; //is similar to your secondInt myValues[2]; //is similar to your thirdInt myValues[3]; //is similar to your forthInt myValues[4]; //is similar to your fifthInt
使用数组,您只需使用for循环来遍历变量。 同时这样做(我猜你的意思是迭代),你需要数组 。
解决方案1:
//To find the largest int largest = array[0]; for(int x=0; x<5; x++) if (array[x] > largest) largest = array[x]; //To find the smallest int smallest= array[0]; for(int x=0; x<5; x++) if (array[x] < smallest) smallest= array[x];
或者,不使用循环和数组(不是那么优雅但可能是你想要的):使用Math.max()
或Math.min()
。
解决方案2:
int largest = Math.max(var1, var2); largest = Math.max(largest, var3); largest = Math.max(largest, var4); largest = Math.max(largest, var5);
解决方案3:
最后, 不使用循环和数组 。
int largest = 0; if(firstInt > secondInt) largest = firstInt else largest = secondInt; if(thirdInt > largest) largest = thirdInt; if(forthInt > largest) largest = forthInt; if(fifthInt > largest) largest = fifthInt;
要找到最小的,只需将运算符从>
更改为<
。
比较多个if语句的唯一方法是使用:
if(integer > 1 && integer > 2 && integer > 3) //etc
否则你可以:
if(integer>1) if(integer>2) //etc
您不能使用逗号在if语句中列出多个整数
您可以使用以下方法:
if (one > two && one > three) System.out.println("%d is the largest number!", one);
或者最好的解决方案,在数组中填充您的值,然后迭代它们,如:
for(int i=1; i
我相信你要做的是
if (i1 > i2 && i1 > i3 && i1 > i4 && i1 > i5) { // i1 is the largest }
但它看起来并不优雅。 通常我们希望在List
或数组中存储多个整数,并通过对其进行排序或遍历列表来查找最大值。
创建一个给定5个数字的数组,并尝试这样做:
int numbers[] = new int[]{1,2,3,4,5}; //assign first element of an array to largest and smallest int smallest = numbers[0]; int largetst = numbers[0]; for(int i=1; i< numbers.length; i++) { if(numbers[i] > largetst) largetst = numbers[i]; else if (numbers[i] < smallest) smallest = numbers[i]; } System.out.println("Largest Number is : " + largetst); System.out.println("Smallest Number is : " + smallest);
根据您在赋值后使用变量的方式,您还可以创建一个数组并对其进行排序
int firstInt = 4; int secondInt = 2; int thirdInt = 3; int fourthInt = 1; int fifthInt = 5; int[] arr = new int[5]; arr[0] = firstInt; arr[1] = secondInt; arr[2] = thirdInt; arr[3] = fourthInt; arr[4] = fifthInt; Arrays.sort(arr); System.out.println("smallest = " + arr[0]); System.out.println("largest = " + arr[arr.length - 1]);
我不知道第一章是多么简单,你没有学习什么是function主义者,但是简单地定义两个变量并给出它们的最小值和最大值:
int smallest = firstInt; int largest = secondInt; if(firstInt > secondInt ){ largest = firstInt; if(thirdInt > firstInt ){ largest = thirdInt; if(fourthInt > thirdInt ){ largest = fourthInt; if(fifthInt > fourthInt){ largest = fifthInt; } } } } if(secondInt < firstInt){ smallest= secondInt; if(thirdInt < secondInt){ smallest = thirdInt; if(fourthInt < thirdInt ){ smallest = fourthInt; if(fifthInt < fourthInt){ smallest = fifthInt; } } } } System.out.println(smallest); System.out.println(largest);