打印出最大值和最小值之间的差异(保留四位小数)。

huangapple go评论95阅读模式
英文:

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(&quot;Enter four numbers: &quot;);
double a1 = in.nextDouble();
double a2 = in.nextDouble();
double a3 = in.nextDouble();
double a4 = in.nextDouble();
if (a1 &gt;= a2 &amp;&amp; a1 &gt;= a3 &amp;&amp; a1 &gt;= a4) {
max = a1;
}
else
if (a2 &gt;= a1 &amp;&amp; a2 &gt;= a3 &amp;&amp; a2 &gt;= a4) {
max = a2;
}
else
if(a3 &gt;= a1 &amp;&amp; a3 &gt;= a2 &amp;&amp; a3 &gt;= a4){
max = a3;
}
else
{
max = a4;
}
if(a1 &gt;= a2 &amp;&amp; a1 &gt;= a3 &amp;&amp; a1 &gt;= a4){
min = a1;
}
else
if(a2 &gt;= a1 &amp;&amp; a2 &gt;= a3 &amp;&amp; a2 &gt;= a4){
min = a2;
}
else
if(a3 &gt;= a1 &amp;&amp; a3 &gt;= a2 &amp;&amp; a3 &gt;= a4){
min = a3;
}
else
{
min = a4;
}
double result = max - min;
System.out.println(&quot;Difference is: &quot; +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 &lt;stdio.h&gt;
int main() {
double a1, a2, a3, a4;
double max, min;
printf(&quot;Input four numbers: \n&quot;);
scanf(&quot;%lf%lf%lf%lf&quot;, &amp; a1, &amp; a2, &amp; a3, &amp; a4);
if (a1 &gt;= a2 &amp;&amp; a1 &gt;= a3 &amp;&amp; a1 &gt;= a4)
max = a1;
else if (a2 &gt;= a1 &amp;&amp; a2 &gt;= a3 &amp;&amp; a2 &gt;= a4)
max = a2;
else if (a3 &gt;= a1 &amp;&amp; a3 &gt;= a2 &amp;&amp; a3 &gt;= a4)
max = a3;
else
max = a4;
if (a1 &lt;= a2 &amp;&amp; a1 &lt;= a3 &amp;&amp; a1 &lt;= a4)
min = a1;
else if (a2 &lt;= a1 &amp;&amp; a2 &lt;= a3 &amp;&amp; a2 &lt;= a4)
min = a2;
else if (a3 &lt;= a1 &amp;&amp; a3 &lt;= a2 &amp;&amp; a3 &lt;= a4)
min = a3;
else
min = a4;
printf(&quot;Difference is %0.4lf\n&quot;, 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 &gt;= a2 &amp;&amp; a1 &gt;= a3 &amp;&amp; a1 &gt;= a4){
min = a1;
}

instead of

 if(a1 &lt;= a2 &amp;&amp; a1 &lt;= a3 &amp;&amp; a1 &lt;= 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(&quot;Enter four numbers: &quot;);
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(&quot;Difference is: %.4f&quot;, 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;

huangapple
  • 本文由 发表于 2020年9月3日 22:53:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63726212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定