英文:
The Largest/Smallest Outputs do not showcase the right values
问题
应该输出输入中的最大/最小数字,看起来有什么问题?我尝试在网上查找,源代码似乎是相同的。谢谢您的帮助。
源代码:
package counting.largest.smallest;
import java.util.Scanner;
public class CountingLargestSmallest {
public static void main(String[] args) {
Scanner TheN = new Scanner(System.in);
int counter = 0;
int number;
int smallest = Integer.MIN_VALUE;
int largest = Integer.MAX_VALUE;
while (counter < 10) {
System.out.print("Integer=");
number = TheN.nextInt();
counter++;
if (number < smallest) {
smallest = number;
} else if (number > largest) {
largest = number;
}
}
System.out.println("\nSmallest=" + smallest);
System.out.println("Largest=" + largest);
}
}
输出:
Integer=1
Integer=2
Integer=3
Integer=4
Integer=5
Integer=6
Integer=7
Integer=8
Integer=9
Integer=10
Smallest=1
Largest=10
英文:
Should receive an output of the largest/smallest numbers within the inputs, what seems to be the problem? I have tried looking it up online, the source code appears to be the same. Thank you for your assistance.
Source code:
package counting.largest.smallest;
import java.util.Scanner;
public class CountingLargestSmallest {
public static void main(String[] args) {
Scanner TheN = new Scanner(System.in);
int counter = 0;
int number;
int smallest = Integer.MIN_VALUE;
int largest = Integer.MAX_VALUE;
while (counter < 10) {
System.out.print("Integer=");
number = TheN.nextInt();
counter++;
if (number < smallest) {
number = smallest;
} else if (number > largest) {
number = largest;
}
}
System.out.println("\nSmallest=" + smallest);
System.out.println("Largest=" + largest);
}
}
Output:
Integer=1
Integer=2
Integer=3
Integer=4
Integer=5
Integer=6
Integer=7
Integer=8
Integer=9
Integer=10
Smallest=-2147483648
Largest=2147483647
答案1
得分: 1
这是翻译好的部分:
有3个错误:
int smallest=Integer.MIN_VALUE;
int largest=Integer.MAX_VALUE;
number=smallest;
number=largest;
- 使用
else if
<br>
1: 当你想找到最小的数时,将它保持为 Integer.MAX_VALUE
,当你想找到最大的数时,将它保持为 Integer.MIN_VALUE
。<br>
为什么对于最小值我们初始化为 Integer.MAX_VALUE
,对于最大值我们初始化为 Integer.MIN_VALUE
?<br>
假设你想从用户那里获取 n
个数字相乘。所以我们将变量 mul
声明并初始化为 1
。因为我们知道任何数乘以 1 都会得到相同的数。<br>
然而,如果我们用 0
来初始化 mul
,那么结果将始终为 0
。
所以我们说乘法中的 1
是 单位元
。<br>
类似地,为了找到 最小
数,我们使用一个单位元,即 Integer.MAX_VALUE
。<br>
这样,小于 Integer.MAX_VALUE
的数将保存在 Smallest
变量中。
int smallest=Integer.MAX_VALUE;
同样地,对于最大值,我们使用 Integer.MIN_VALUE
进行初始化,以便大于 Integer.MIN_VALUE
的数被保存在 largest
变量中。
int largest=Integer.MIN_VALUE;
2: 你正在将 number
变量赋值为 smallest
和 largest
的值,应该是 smallest=number
和 largest=number
。<br>
当你写 x=0
时,它表示 x 等于 0,这意味着 x 发生了变化,类似地,当你写 number=smallest
时,number = Integer.MAX_VALUE
,而 smallest
根本没有变化。<br>
所以你应该像这样写:smallest = number
,这意味着最小值是这个数,而且 smallest
在每次满足条件时都会改变。
3: 当你写下面的代码时
if(condition){
}else if(conditon){
}
只有在第一个 if
语句为 false
时,else if
才会执行,如果第一个条件为 true
,则第二个 if
永远不会被执行。<br>
如果我们有一个降序的数字,那么第二个 if
将永远不会被执行,从而导致 largest
变量中有 Integer.MIN_VALUE
。<br>
而在这里,你想要同时检查 最小
和 最大
,所以我们应该为每个条件都有独立的 if
语句。
if(condition){
}
if(condition){
}
代码:
Scanner TheN= new Scanner(System.in);
int counter=0;
int number;
// 已更改
int smallest=Integer.MAX_VALUE;
int largest=Integer.MIN_VALUE;
while(counter<10){
System.out.print("Integer=");
number=TheN.nextInt();
counter++;
if(number<smallest){
// 已更改
smallest= number;
}
if(number>largest){
// 已更改
largest=number;
}
}
System.out.println("\nSmallest="+smallest);
System.out.println("Largest="+largest);
输出:
Integer=1
Integer=2
Integer=2
Integer=4
Integer=3
Integer=6
Integer=7
Integer=8
Integer=9
Integer=10
Smallest=1
Largest=10
英文:
there were 3 bugs:
1.
int smallest=Integer.MIN_VALUE;
int largest=Integer.MAX_VALUE;
2.
number=smallest;
number=largest;
- Using
else if
<br>
1: When you want to find a min
number keep it Integer.MAX_VALUE
and when you want to find max
keep it has Integer.MIN_VALUE
.<br>
Why for min
we Initialize Integer.MAX_VALUE
and for max
we Initialize Integer.MIN_VALUE
?<br>
Say you want to multiple n
numbers from the user. So we will declare and initialize mul
variable to 1
. cause we know any number
multiple by 1 will give us the same number
.<br>
Whereas if we initialize mul
with 0
it will give us 0
only.
So we say 1
for multiplication is identity
.<br>
Similarly, for finding Minimum
number we use an identity that is Integer.MAX_VALUE
.<br>
So that number that less
then Integer.MAX_VALUE
are saved in Samllest
variable.
int smallest=Integer.MAX_VALUE;
Similarly for max
we initialize
it with Integer.MIN_VALUE
SO that number that are greater
then Integer.MIN_VALUE
are stored in largest
variable.
int largest=Integer.MIN_VALUE;
2: You are assigning number
variable the value of smallest
and largest
it should be like smallest=number
and largest=number
<br>
when you write x=0
it says x is 0 that means x is changed, Similarly when you write number=smallest
its number = Integer.MAX_VALUE
and smallest is not changing at all.<br>
So you should write like smallest = number
, It means smallest is the number and smallest
is changing every time condition satisfies
.
3: When you write
if(condition){
}else if(conditon){
}
else if
is only excuted when first if
statement if false
and if first condition
is true
second if
is never executed.<br>
if we have descending number then second if
will never be excuted which would result in having Integer.MIN_VALUE
in largest
variable.<br>
And here you want to check for both min
and max
so we should have indepent if
statement for each
if(condition){
}
if(condition){
}
Code:
Scanner TheN= new Scanner(System.in);
int counter=0;
int number;
// changed
int smallest=Integer.MAX_VALUE;
int largest=Integer.MIN_VALUE;
while(counter<10){
System.out.print("Integer=");
number=TheN.nextInt();
counter++;
if(number<smallest){
// changed
smallest= number;
}
if(number>largest){
// changed
largest=number;
}
}
System.out.println("\nSmallest="+smallest);
System.out.println("Largest="+largest);
Output:
Integer=1
Integer=2
Integer=2
Integer=4
Integer=3
Integer=6
Integer=7
Integer=8
Integer=9
Integer=10
Smallest=1
Largest=10
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论