簡體   English   中英

如何交換最大值和最小值

[英]How to swapplaces of largest and smallest value

我是學習 Java 的新手,我有以下任務:

編寫一個程序,從鍵盤讀取三個整數 a、b 和 c,並交換三個值中最大和最小的位置。

import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Program to find the largest and smalles value");

        System.out.println("Please insert first number:");
        int first = scanner.nextInt();

        System.out.println("Please insert second number:");
        int second = scanner.nextInt();

        System.out.println("Please insert:");
        int third = scanner.nextInt();

        int largest = largest(first, second, third);
        int smallest = smallest(first, second, third);

        System.out.printf("The biggest value among %d, %d, и %d is : %d %n", first, second, third, largest);
        System.out.printf("The smallest value among %d, %d, и %d is : %d %n", first, second, third, smallest);
       
        scanner.close();

    }
        public static int largest(int first, int second, int third) {

        int max = first;
        if (second > max) {
            max = second; }

        if (third > max) {
            max = third; }

        return max; }

        public static int smallest(int first, int second, int third) {

        int min = first;
        if (second < min) {
            min = second; }

        if (third < min) {
            min = third; }

        return min; }

我不確定此內容中的“交換位置”是什么意思,但這就是交換值的方式(通常)

int smallest = 1;
int largest =  5;
int temp = 0;

temp = largest;  // save one number to a temporary variable
largest = smaller;  // override the variable you just saved
smaller = temp;  // place the "temp" variable value into the second variable

如果您要打印出smallestlargest ,您會發現它們的值現在已交換(最大為 1,最小為 5)。

你不需要交換任何東西。 下面是它的工作原理。

  • 將值 min 設置為可能的最大值。
  • 將值 max 設置為可能的最小值。
  • 現在對於您的所有值,只需將當前最小值與該值進行比較,將當前最大值與該值進行比較,並根據需要替換它們。
  • 完成后,最小值和最大值應該有各自的值。

然后,如果你真的想交換它們,你可以應用@hfontanez建議的技術。

該作業不要求您找到最大值和最小值 - 它指出您應該交換 position 的最大值和最小值。 這意味着這些值是有順序的。 這通常被建模為一個數組。 所以聽起來你應該將三個值讀入一個數組。 將您的輸入代碼移動到返回類型為 int[] 的方法中。 讀取輸入后,將它們放入數組中並返回。

public static int[] getInputs() {
  ... read the inputs into first, second, third ...
  int[] values = {first, second, third};
  return values;
}

獲得值數組后,您需要確定最大值和最小值的索引。 然后,您交換這些位置的值。 這通常是在臨時變量的幫助下完成的,以保存其中一個值。

public static void swapPositionOfLargestAndSmallestValues(int[] values) {
  int smallestIndex = findIndexOfSmallestValue(values);
  int largestIndex = findIndexOfLargestValue(values);
  int temp = values[smallestIndex];
  values[smallestIndex] = values[largestIndex];
  values[largestIndex = temp;
}

您將需要實現查找最大值和最小值索引的方法。 然后你的 main 看起來像這樣,其中printValues(int[])是一種以某種方式輸出數組元素的方法。

public static void main(String[] args) {
  int[] values = getInputs();
  printValues(values);
  swapPositionOfLargestAndSmallestValues(values);
  printValues(values);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM