Home > Enterprise >  How to swapplaces of largest and smallest value
How to swapplaces of largest and smallest value

Time:12-03

I am new at learning Java and I have the following assignment:

Write a program which reads three integers a, b and c from the keyboard and swaps the places of the largest and smallest among the three values.

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; }

CodePudding user response:

I am not sure what "swap places" mean in this contents, but this is how you swap values (in general)

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

If you were to print out smallest and largest, you'll find that their values are now swapped (largest will be 1 and smallest will be 5).

CodePudding user response:

You don't need to swap anything. Here is how it works.

  • set a value min to the largest possible value.
  • set a value max to the smallest possible value.
  • Now for all your values, simply compare current min to that value and current max to that value and replace them as necessary.
  • When done, min and max should have their respective values.

Then, if you really want to swap them you can apply the technique that @hfontanez suggested.

CodePudding user response:

The assignment does not ask you to find the largest and smallest values - it states that you should swap the position of the largest and smallest values. This implies that there is an ordering of these values. This is typically modeled as an array. So it sounds like you should be reading the three values into an array. Move your input code into a method with the return type int[]. After reading the input you put them in an array and return.

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

Once you have the array of values you need to identify the index of the largest values and the smallest value. You then swap the values at those positions. This is normally done with the help of a temporary variable to hold one of those 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;
}

You will need to implement the methods that find the indexes of the largest and smallest values. Then your main looks something like this, where printValues(int[]) is a method that outputs the elements of the array in some fashion.

public static void main(String[] args) {
  int[] values = getInputs();
  printValues(values);
  swapPositionOfLargestAndSmallestValues(values);
  printValues(values);
}
  •  Tags:  
  • java
  • Related