来自Deitel" Java How To Program"第10版:
2.24(最大和最小整数)编写一个读取五个整数的应用程序并确定并打印最大和最小的整数 集团中的整数。仅使用您学到的编程技巧 在本章中。
我的代码:
// Ex. 2.24: LargestAndSmallestIntegers.java
// Obtain five numbers from the user, display largest and smallest from the group.
import java.util.Scanner;
public class LargestAndSmallestIntegers
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int number1,
number2,
number3,
number4,
number5,
largest,
smallest;
System.out.print("Enter first integer: ");
number1 = input.nextInt();
System.out.print("Enter second integer: ");
number2 = input.nextInt();
System.out.print("Enter third integer: ");
number3 = input.nextInt();
System.out.print("Enter fourth integer: ");
number4 = input.nextInt();
System.out.print("Enter fifth integer: ");
number5 = input.nextInt();
largest = number1;
smallest = number1;
if (number2 > largest)
largest = number2;
if (number3 > largest)
largest = number3;
if (number4 > largest)
largest = number4;
if (number5 > largest)
largest = number5;
if (number2 < smallest)
largest = number2;
if (number3 < smallest)
largest = number3;
if (number4 < smallest)
largest = number4;
if (number5 < smallest)
largest = number5;
System.out.printf("Largest of five integers is %d, and smallest is %d%n", largest, smallest);
}
}
我做错了什么?为什么它不能获得最大和最小的整数?
答案 0 :(得分:1)
在第二个区块中,您使用smallest
进行核对,然后分配到largest
。
答案 1 :(得分:0)
我猜复制粘贴拼写错误。对于最小的部分计算,将所有从最大值到最小值的值替换为从列表中获取的最大值,并用剩余条件中的最小值覆盖。
largest = number1;
smallest = number1;
if (number2 > largest)
largest = number2;
if (number3 > largest)
largest = number3;
if (number4 > largest)
largest = number4;
if (number5 > largest)
largest = number5;
// Error here
// fixed typos
if (number2 < smallest)
smallest = number2;
if (number3 < smallest)
smallest = number3;
if (number4 < smallest)
smallest = number4;
if (number5 < smallest)
smallest = number5;
答案 2 :(得分:0)
if ( two > largest ){
largest = two;
System.out.printf ( "Largest of five values is %d, and the smallest is %d%n", largest, smallest );
}
else if ( three > largest ){
largest = three;
System.out.printf ( "Largest of five values is %d, and the smallest is %d%n", largest, smallest );
}
else if ( four > largest ){
largest = four;
System.out.printf ( "Largest of five values is %d, and the smallest is %d%n", largest, smallest );
}
else if ( five > largest ){
largest = five;
System.out.printf ( "Largest of five values is %d, and the smallest is %d%n", largest, smallest );
}
if ( two < smallest ){
smallest = two;
System.out.printf ( "Largest of five values is %d, and the smallest is %d%n", largest, smallest );
}
else if ( three < smallest ){
smallest = three;
System.out.printf ( "Largest of five values is %d, and the smallest is %d%n", largest, smallest );
}
else if ( four < smallest ){
smallest = four;
System.out.printf ( "Largest of five values is %d, and the smallest is %d%n", largest, smallest );
}
else if ( five < smallest ){
smallest = five;
System.out.printf ( "Largest of five values is %d, and the smallest is %d%n", largest, smallest );
}
答案 3 :(得分:0)
import java.util.*;
public class LargestSmallest {
public static void main(String[] args) {
// Declare class variables
int counter = 0;
int number = 0;
int largest = 0;
int smallest = 0;
while (counter < 5) {
counter++;
System.out.println("Enter the number");
Scanner sc=new Scanner(System.in);
number = sc.nextInt();
if (number > largest) {
largest = number;
} else if (number < largest) {
smallest = number;
}
// Display Largest and Smallest
System.out.println("largest number is: " + largest);
System.out.println("smallest number is: " + smallest);
}
}
}