18

私のコードではエラーは発生しませんが、最小値と最大値が表示されません。コードは次のとおりです。

Scanner input = new Scanner(System.in);

int array[] = new int[10];

System.out.println("Enter the numbers now.");

for (int i = 0; i < array.length; i++) {
    int next = input.nextInt();
    // sentineil that will stop loop when 999 is entered
    if (next == 999) {
        break;
    }
    array[i] = next;
    // get biggest number
    getMaxValue(array);
    // get smallest number
    getMinValue(array);
}

System.out.println("These are the numbers you have entered.");
printArray(array);


// getting the maximum value
public static int getMaxValue(int[] array) {
    int maxValue = array[0];
    for (int i = 1; i < array.length; i++) {
        if (array[i] > maxValue) {
            maxValue = array[i];
        }
    }
    return maxValue;
}

// getting the miniumum value
public static int getMinValue(int[] array) {
    int minValue = array[0];
    for (int i = 1; i < array.length; i++) {
        if (array[i] < minValue) {
            minValue = array[i];
        }
    }
    return minValue;
}

//this method prints the elements in an array......
//if this case is true, then that's enough to prove to you that the user input has  //been stored in an array!!!!!!!
public static void printArray(int arr[]) {
    int n = arr.length;

    for (int i = 0; i < n; i++) {
        System.out.print(arr[i] + " ");
    }
}

それを表示するには system.out.println() が必要ですか、それとも戻り値が機能する必要がありますか?

4

13 に答える 13

18
getMaxValue(array);
// get smallest number
getMinValue(array);

メソッドを呼び出していますが、戻り値を使用していません。

System.out.println(getMaxValue(array));
System.out.println(getMinValue(array)); 
于 2013-08-30T05:21:16.920 に答える
16

あなたの方法でこれをしたくない場合は、これも試すことができます。

    Arrays.sort(arr);
    System.out.println("Min value "+arr[0]);
    System.out.println("Max value "+arr[arr.length-1]);
于 2013-08-30T05:44:52.813 に答える
6

最も単純なソリューションの1つは次のとおりです。

//MIN NUMBER
Collections.sort(listOfNumbers);
listOfNumbers.get(0);

//MAX NUMBER
Collections.sort(listOfNumbers);
Collections.reverse(listOfNumbers);
listOfNumbers.get(0);
于 2014-12-19T13:16:03.940 に答える
3

最小/最大値を捨てるだけです:

  // get biggest number
  getMaxValue(array); // <- getMaxValue returns value, which is ignored
  // get smallest number
  getMinValue(array); // <- getMinValue returns value, which is ignored as well

次のようなことができます

  ... 
  array[i] = next;

  System.out.print("Max value = ");
  System.out.println(getMaxValue(array)); // <- Print out getMaxValue value

  System.out.print("Min value = ");
  System.out.println(getMinValue(array)); // <- Print out getMinValue value

  ...  
于 2013-08-30T05:23:35.023 に答える
3

ここで 2 つの間違いを犯しています。1.配列の初期化が完了する前にメソッド
を呼び出す。 2.メソッドから返された戻り値を格納しない。 だからこのコードを試してくださいgetMaxValue(),getMinValue()
getMaxValue(),getMinValue()

   for (int i = 0 ; i < array.length; i++ ) 
  {
       int next = input.nextInt();
       // sentineil that will stop loop when 999 is entered
       if (next == 999)
       break;
       array[i] = next;
  }
  // get biggest number
  int maxValue = getMaxValue(array);
  System.out.println(maxValue );

  // get smallest number
  int minValue = getMinValue(array);
  System.out.println(minValue);
于 2013-08-30T05:24:19.133 に答える
1
import java.util.*;
class Maxmin
{
    public static void main(String args[])
    {
        int[] arr = new int[10];
        Scanner in = new Scanner(System.in);
        int i, min=0, max=0;
        for(i=0; i<=arr.length; i++)
        {
            System.out.print("Enter any number: ");
            arr[i] = in.nextInt();          
        }
        min = arr[0];
        for(i=0; i<=9; i++)
        {
            if(arr[i] > max)
            {
                max = arr[i];
            }
            if(arr[i] < min)
            {
                min = arr[i];
            }
        }
        System.out.println("Maximum is: " + max);
        System.out.println("Minimum is: " + min);
    }
}
于 2016-01-07T07:30:03.797 に答える
1

あなたの最大、最小の方法は正しいです

しかし、あなたはコンソールに印刷しません!

そして...おそらくより良い場所の変更(最大、最小)方法

ループ内の現在の (最大、最小) メソッド。それは必要ではありません.. 1回の呼び出しが必要です

このコードを変更することをお勧めします

    for (int i = 0 ; i < array.length; i++ ) {
       int next = input.nextInt();
       // sentineil that will stop loop when 999 is entered
       if (next == 999)
       break;
       array[i] = next;
}
System.out.println("max Value : " + getMaxValue(array));
System.out.println("min Value : " + getMinValue(array));
System.out.println("These are the numbers you have entered.");
printArray(array);
于 2013-08-30T05:25:17.907 に答える
1

//Javaでソートせずに配列内の最大値と最小値を見つけるには

import java.util.Scanner;
import java.util.*;
public class MaxMin_WoutSort {
 public static void main(String args[])
   {
      int n,max=Integer.MIN_VALUE,min=Integer.MAX_VALUE;
      System.out.println("Enter the number of elements: ");
      Scanner sc = new Scanner(System.in);

      int[] arr = new int[sc.nextInt()]; //U can't say static or dynamic.
                                         //UnWrapping object sc to int value;sc.nextInt()
      System.out.println("Enter the elements: ");
      for(int i=0;i<arr.length;i++)      //Loop for entering values in array
      {
          int next = sc.nextInt();
          arr[i] = next;
      }
      for(int j=0;j<arr.length;j++)
     {
          if(arr[j]>max)               //Maximum Condition
            max = arr[j];
          else if(arr[j]<min)         //Minimum Condition
              min = arr[j];
     }
     System.out.println("Highest Value in array: " +max);
     System.out.println("Smallest Value in array: "+min);

 }
}
于 2017-01-15T09:06:02.473 に答える
0

同じコードを更新しました。コードを元のコードと比較してください。

public class Help {

public static void main(String args[]){
    Scanner input = new Scanner(System.in);

    int array[] = new int[10];

    System.out.println("Enter the numbers now.");

    for (int i = 0; i < array.length; i++) {
        int next = input.nextInt();
        // sentineil that will stop loop when 999 is entered
        if (next == 999) {
            break;
        }
        array[i] = next;
    }

    System.out.println("These are the numbers you have entered.");
    printArray(array);

    // get biggest number
    System.out.println("Maximum: "+getMaxValue(array));
    // get smallest number
    System.out.println("Minimum: "+getMinValue(array));
}

// getting the maximum value
public static int getMaxValue(int[] array) {
    int maxValue = array[0];
    for (int i = 1; i < array.length; i++) {
        if (array[i] > maxValue) {
            maxValue = array[i];
        }
    }
    return maxValue;
}

// getting the miniumum value
public static int getMinValue(int[] array) {
    int minValue = array[0];
    for (int i = 1; i < array.length; i++) {
        if (array[i] < minValue) {
            minValue = array[i];
        }
    }
    return minValue;
}

//this method prints the elements in an array......
//if this case is true, then that's enough to prove to you that the user input has  //been stored in an array!!!!!!!
public static void printArray(int arr[]) {
    int n = arr.length;

    for (int i = 0; i < n; i++) {
        System.out.print(arr[i] + " ");
    }
}
}
于 2017-07-13T08:58:47.133 に答える