英文:
prints out the Difference (using 4-decimal places) of the maximum and minimum values
问题
以下是翻译好的部分:
我正在尝试解决一个**Java**程序的问题,该程序从键盘接收4个实数,并打印出这些数字的最大值和最小值之间的**差值**(保留4位小数)。
*测试数据和预期输出:
输入四个数字:-1.5 2 7.5 11.2
差值为 12.7000*
我尝试了以下方法:
import java.util.Scanner;
public class solution05 {
public static void main(String[] args)
{
double max, min;
Scanner in = new Scanner(System.in);
System.out.println("输入四个数字:");
double a1 = in.nextDouble();
double a2 = in.nextDouble();
double a3 = in.nextDouble();
double a4 = in.nextDouble();
if (a1 >= a2 && a1 >= a3 && a1 >= a4) {
max = a1;
}
else
if (a2 >= a1 && a2 >= a3 && a2 >= a4) {
max = a2;
}
else
if(a3 >= a1 && a3 >= a2 && a3 >= a4){
max = a3;
}
else
{
max = a4;
}
if(a1 >= a2 && a1 >= a3 && a1 >= a4){
min = a1;
}
else
if(a2 >= a1 && a2 >= a3 && a2 >= a4){
min = a2;
}
else
if(a3 >= a1 && a3 >= a2 && a3 >= a4){
min = a3;
}
else
{
min = a4;
}
double result = max - min;
System.out.println("差值为:" + result);
}
}
我的输出是
输入四个数字:
1
5
4
9
差值为:0.0
进程已退出,退出代码:0
我按照一个C代码的概念进行了操作,该代码用于类似的目的。
#include <stdio.h>
int main() {
double a1, a2, a3, a4;
double max, min;
printf("输入四个数字:\n");
scanf("%lf%lf%lf%lf", & a1, & a2, & a3, & a4);
if (a1 >= a2 && a1 >= a3 && a1 >= a4)
max = a1;
else if (a2 >= a1 && a2 >= a3 && a2 >= a4)
max = a2;
else if (a3 >= a1 && a3 >= a2 && a3 >= a4)
max = a3;
else
max = a4;
if (a1 <= a2 && a1 <= a3 && a1 <= a4)
min = a1;
else if (a2 <= a1 && a2 <= a3 && a2 <= a4)
min = a2;
else if (a3 <= a1 && a3 <= a2 && a3 <= a4)
min = a3;
else
min = a4;
printf("差值为 %0.4lf\n", max - min);
return 0;
}
请问我在这里漏掉了什么。请帮忙。提前谢谢您。
英文:
I am trying to solve the problem of a Java program that accepts 4 real numbers from the keyboard and prints out the Difference (using 4-decimal places) of the maximum and minimum values of these numbers.
Test data and expected output:
Enter four numbers: -1.5 2 7.5 11.2
Difference is 12.7000
I have tried the following way:
import java.util.Scanner;
public class solution05 {
public static void main(String[] args)
{
double max, min;
Scanner in = new Scanner(System.in);
System.out.println("Enter four numbers: ");
double a1 = in.nextDouble();
double a2 = in.nextDouble();
double a3 = in.nextDouble();
double a4 = in.nextDouble();
if (a1 >= a2 && a1 >= a3 && a1 >= a4) {
max = a1;
}
else
if (a2 >= a1 && a2 >= a3 && a2 >= a4) {
max = a2;
}
else
if(a3 >= a1 && a3 >= a2 && a3 >= a4){
max = a3;
}
else
{
max = a4;
}
if(a1 >= a2 && a1 >= a3 && a1 >= a4){
min = a1;
}
else
if(a2 >= a1 && a2 >= a3 && a2 >= a4){
min = a2;
}
else
if(a3 >= a1 && a3 >= a2 && a3 >= a4){
min = a3;
}
else
{
min = a4;
}
double result = max - min;
System.out.println("Difference is: " +result);
}
}
My output is
Enter four numbers:
1
5
4
9
Difference is: 0.0
Process finished with exit code 0
I followed the concept from a C code which serve the similar purpose.
#include <stdio.h>
int main() {
double a1, a2, a3, a4;
double max, min;
printf("Input four numbers: \n");
scanf("%lf%lf%lf%lf", & a1, & a2, & a3, & a4);
if (a1 >= a2 && a1 >= a3 && a1 >= a4)
max = a1;
else if (a2 >= a1 && a2 >= a3 && a2 >= a4)
max = a2;
else if (a3 >= a1 && a3 >= a2 && a3 >= a4)
max = a3;
else
max = a4;
if (a1 <= a2 && a1 <= a3 && a1 <= a4)
min = a1;
else if (a2 <= a1 && a2 <= a3 && a2 <= a4)
min = a2;
else if (a3 <= a1 && a3 <= a2 && a3 <= a4)
min = a3;
else
min = a4;
printf("Difference is %0.4lf\n", max - min);
return 0;
}
What am I missing here. Please help.
Thank you in advance
答案1
得分: 0
在你的代码中,将以下部分:
if(a1 >= a2 && a1 >= a3 && a1 >= a4){
min = a1;
}
替换为:
if(a1 <= a2 && a1 <= a3 && a1 <= a4){
min = a1;
}
等等。
英文:
In secons if-chain your code
if(a1 >= a2 && a1 >= a3 && a1 >= a4){
min = a1;
}
instead of
if(a1 <= a2 && a1 <= a3 && a1 <= a4){
min = a1;
}
etc
答案2
得分: 0
首先,您可以简化您的代码,然后将 `nextInt()` 更改为 `nextDouble()`,然后,如果还不起作用,尝试打印 `max` 和 `min`,以检查它们是否被正确赋值。
import java.util.Scanner;
public class solution05 {
public static void main(String[] args)
{
double max, min;
Scanner in = new Scanner(System.in);
System.out.println("输入四个数字:");
double a1 = in.nextDouble();
double a2 = in.nextDouble();
double a3 = in.nextDouble();
double a4 = in.nextDouble();
max = Math.max(a1, a2);
max = Math.max(max, a3);
max = Math.max(max, a4);
min = Math.min(a1, a2);
min = Math.min(min, a3);
min = Math.min(min, a4);
// 在这里打印 max 和 min
double result = max - min;
String output = String.format("差值为:%.4f", result);
System.out.println(output);
}
}
英文:
First, you can reduce your code, then, change nextInt()
to nextDouble()
, then, if it doesn't work yet, try printing max
and min
, to check if they are assigned correctly.
import java.util.Scanner;
public class solution05 {
public static void main(String[] args)
{
double max, min;
Scanner in = new Scanner(System.in);
System.out.println("Enter four numbers: ");
double a1 = in.nextDouble();
double a2 = in.nextDouble();
double a3 = in.nextDouble();
double a4 = in.nextDouble();
max = Math.max(a1, a2);
max = Math.max(max, a3);
max = Math.max(max, a4);
min = Math.min(a1, a2);
min = Math.min(min, a3);
min = Math.min(min, a4);
// Print max and min here
double result = max - min;
String output = String.format("Difference is: %.4f", result);
System.out.println(output);
}
}
答案3
得分: 0
你可以使用 Math.min 和 Math.max 来使事情变得更简单:
double max = Math.max(a1, a2);
max = Math.max(max, a3);
max = Math.max(max, a4);
double min = Math.min(a1, a2);
min = Math.min(min, a3);
min = Math.min(min, a4);
现在只需:
double difference = max - min;
英文:
You could use Math.min and Math.max to make things easier:
double max = Math.max(a1, a2);
max = Math.max(max, a3);
max = Math.max(max, a4);
double min = Math.min(a1, a2);
min = Math.min(min, a3);
min = Math.min(min, a4);
Now simply:
double difference = max - mix;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论