Java Swap Values
This program swaps the values of two inputs and then prints them back out to the console.
package com.thealmostengineer.swapvalues;
import java.util.Scanner;
public class Swapper {
private static int x;
private static int y;
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Enter two integer values: ");
Scanner sc = new Scanner(System.in);
x = sc.nextInt();
y = sc.nextInt();
swapValues(x, y);
systemPause();
}
public static void systemPause(){
System.out.println("Press any key to continue...");
new Scanner(System.in).nextLine();
}
private static void swapValues(int x, int y){
int temp;
System.out.println("Original values: " + x + " " + y);
temp = x;
x = y;
y = temp;
System.out.println("Values swapped: " + x + " " + y);
}
}
Updated: 2020-07-15 | Posted: 2014-02-08