Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Value exchange achieved through two classes of variables. Requirements: switching at the arguments.
public class Exchange {
public double num1;
public double num2;
public void exchange(){
double tmp = num1;
num1 = num2;
num2 = tmp;
System.out.println("num1 = "+num1);
System.out.println("num2 = "+num2);
}
}
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Exchange exchange = new Exchange();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要交换的两个数:");
exchange.num1 = scanner.nextDouble();
exchange.num2 = scanner.nextDouble();
exchange.exchange();
}
}