Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我该如何将其转化为一个简单的程序,以获取5个数字并说出最小值和最大值?_Java_Jgrasp - Fatal编程技术网

Java 我该如何将其转化为一个简单的程序,以获取5个数字并说出最小值和最大值?

Java 我该如何将其转化为一个简单的程序,以获取5个数字并说出最小值和最大值?,java,jgrasp,Java,Jgrasp,我以为我有一段时间了解了一些事情,但后来我遇到了一个问题,结果行被无限打印。现在我根本无法让它工作。有人知道如何将这种代码风格转变为符合我所述目标的功能吗 import java.util.Scanner; public class Numerical { public static void main (String [ ] args ) { Scanner scan = new Scanner (System.in); System.out.println("P

我以为我有一段时间了解了一些事情,但后来我遇到了一个问题,结果行被无限打印。现在我根本无法让它工作。有人知道如何将这种代码风格转变为符合我所述目标的功能吗

import java.util.Scanner;
public class Numerical
{
   public static void main (String  [ ] args   )
   {
   Scanner scan = new Scanner (System.in);
   System.out.println("Please enter 5 numbers one after another");
   {
   int a = scan.nextInt ( ); // First number input by user
   int b = scan.nextInt ( ); // Second number input by user
   int c = scan.nextInt ( ); // Third number input by user
   int d = scan.nextInt ( ); // Fourth number input by user
   int e = scan.nextInt ( ); // Fifth number input by user
      // Check if a is the greatest number
      if   (a > b);
      while(a > c);
      while(a > d);
      while(a > e);
      System.out.println ("The highest number is " +a);
        // Check if b is the greatest number
      else if   (b > a);
      while(b > c);
      while(b > d);
      while(b > e);
      System.out.println ("The highest number is " +b);

            // Check if c is the greatest number
      else if(c > a);
      while(c > b);
      while(c > d);
      while(c > e);
      System.out.println ("The highest number is " +c);
              // Check if d is the greatest number
      else if(d > a);
      while(d > b);
      while(d > c);
      while(d > e);
      System.out.println ("The highest number is " +d);
                   // Check if e is the greatest number
      else if(e > a);
      while(e > b);
      while(e > c);
      while(e > d);
      System.out.println ("The highest number is " +e);
      }
}

}

不需要做所有这些。读取数组*的输入,使用,第一个元素最小,最后一个元素较大

我不明白你的代码,那些
while
if
语句是什么?我建议你通过一个基本的教程来更好地理解事物是如何工作的。如果你发现自己陷入困境,没有什么比调试你的代码更好的了,不仅你会发现问题,而且你会理解为什么你会遇到它


*如果您不知道输入的长度,可能需要使用
ArrayList
使用数组的好处!它们使您的代码更短、更可变,并且有许多有用的函数

import java.util.Arrays;
import java.util.Scanner;

public class Numerical {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter 5 numbers one after another");

        int[] inputs = new int[5];

        for (int i = 0; i < inputs.length; i++) {
            inputs[i] = scan.nextInt();
        }

        Arrays.sort(inputs);

        System.out.println("The highest number is " + inputs[inputs.length - 1]);
    }

}   
导入java.util.array;
导入java.util.Scanner;
公共类数字{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
System.out.println(“请依次输入5个数字”);
int[]输入=新的int[5];
对于(int i=0;i
最好的方法是使用数组并实现排序方法;然而,这个例子会让你更容易理解。我们只是将每个数字与用户输入的当前最大数字进行比较:

import java.util.Scanner;

public class Blahh
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int largest = 0;

        int a = scan.nextInt(); // First number input by user
        int b = scan.nextInt();
        largest = largest(a, b);

        int c = scan.nextInt();
        largest = largest(largest, c);

        int d = scan.nextInt();
        largest = largest(largest, d);

        int e = scan.nextInt();

        System.out.println("The largest number is: " + largest(largest, e));
    }

    public static int largest(int num1, int num2)
    {
        if (num1 > num2)
            return num1;
        else
            return num2;
    }
}

使用一个循环,当你得到每个数字时,将其与上一个已知的最高值进行比较,如果它更大,则重新分配最高值,重复直到有5个…将数字粘贴到一个排序列表中,并取该列表的开头和结尾?你的程序当前显示:如果
a
大于
b
,则不执行任何操作。如果
a
大于
c
,则永远循环。之后的一切都不会被调用。