← all problems

Swap two values 🌶️🌶️

The program below gives first and second their starting values, and its last two lines (locked) print first and then second. Write code in the box so that the two variables trade values: after your code runs, first should hold what second started with, and second should hold what first started with. Your code will be tested with several different starting values.

Main.java — the program frame below is already written; you're only completing the code in the editable box. Your code will be run several times, each with different starting values for the variables at the top.

public class Main {
    public static void main(String[] args) {
        int first = 3;
        int second = 8;
        // ---- write your code below ----
        System.out.println(first);
        System.out.println(second);
    }
}