Java 1.0 swapping numbers using temporary value

javaSwappingNumbersTemp

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        int x,y, temp;
        System.out.println("Enter x and y");
        Scanner in = new Scanner(System.in);

        System.out.println("Select an x value:");
        x = in.nextInt();
        System.out.println("Select a y value:");
        y = in.nextInt();

        System.out.println("Before swapping\n" +
                            "x = " + x + " \n " +
                            "y = " + y);

        temp = x;
        x = y;
        y = temp;

        System.out.println("After swapping\nx = "+x+"\ny = "+y);
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s