Java 编写一个应用程序,从用户输入三个整数并显示数字的总和、平均值、乘积、最小和最大
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25675043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write an application that inputs three integers from the user and displays the sum, average, product, smallest and largest of the numbers
提问by Shairyar Shafqat
I'm getting the sum, average and the product. The real difficulty I am facing is with the smallest and the largest number.
我得到总和、平均值和乘积。我面临的真正困难是最小和最大的数字。
I can do it with two numbers, but three numbers is not making any sense to me. Ask me if my question isn't clear or if its not making sense.
我可以用两个数字来做,但三个数字对我来说没有任何意义。问我我的问题是否不清楚或没有意义。
import java.util.Scanner;
// exercise 2.17
public class ArithmeticSmallestLargest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1;
int num2;
int num3;
int sum;
int average;
int product;
double largest
System.out.print("Enter First Integer: ");
num1 = input.nextInt();
System.out.print("Enter Second Integer: ");
num2 = input.nextInt();
System.out.print("Enter Third Integer: ");
num3 = input.nextInt();
sum = num1 + num2 + num3;
average = sum / 3;
product = num1 * num2 * num3;
if (largest =num1 > num2 & num2 > num3)
System.out.println(sum);
System.out.println(average);
System.out.println(product);
System.out.println("The biggest number is " + largest);
}
}
采纳答案by Shairyar Shafqat
import java.util.Scanner;
// exercise 2.17
public class ArithmeticSmallestLargest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1;
int num2;
int num3;
int sum;
int average;
int product;
int largest;
int smallest;
System.out.print("Enter First Integer: ");
num1 = input.nextInt();
System.out.print("Enter Second Integer: ");
num2 = input.nextInt();
System.out.print("Enter Third Integer: ");
num3 = input.nextInt();
sum = num1 + num2 + num3;
average = sum / 3;
product = num1 * num2 * num3;
largest = num1;
smallest = num1;
if(num2 > largest)
largest = num2;
if(num3 > largest)
largest = num3;
if(num2 < smallest)
smallest = num2;
if (num3 < smallest)
smallest = num3;
System.out.println("The sum is " + sum);
System.out.println("The average is " + average);
System.out.println("The product is " + product);
System.out.println("Largest of three integers is " + largest + " and the smallest is "+ smallest + ".");
}
}
回答by Nabin Chaudhary
import java.util.Scanner;
public class ArithmeticSmallestLargest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num1;
int num2;
int num3;
int sum;
int average;
int product;
int largest;
int smallest;
System.out.print("Enter First Integer: ");
num1 = input.nextInt();
System.out.print("Enter Second Integer: ");
num2 = input.nextInt();
System.out.print("Enter Third Integer: ");
num3 = input.nextInt();
sum = num1 + num2 + num3;
average = sum / 3;
product = num1 * num2 * num3;
largest = num1;
smallest = num1;
if(num2 > largest)
largest = num2;
if(num3 > largest)
largest = num3;
if(num2 < smallest)
smallest = num2;
if (num3 < smallest)
smallest = num3;
System.out.printf("The sum is %d%n " , sum);
System.out.printf("The average is %d%n " , average);
System.out.printf("The product is %d%n " , product);
System.out.printf("Largest of three integers is %d%n " , largest);
System.out.printf("Smallest of three integers is %d%n " , smallest);
}
}
Answer is given below:
答案如下:
Enter First Integer: 60
Enter Second Integer: 90
Enter Third Integer: 30
输入第一个整数:60
输入第二个整数:90
输入第三个整数:30
The sum is 180
The average is 60
The product is 162000
Largest of three integers is 90
Smallest of three integers is 30
总和为 180
平均值为 60
乘积为 162000
三个整数中最大的为 90 三个整数中
最小的为 30
...Program finished with exit code 0
Press ENTER to exit console.
...程序完成,退出代码 0
按 ENTER 退出控制台。