Last active
December 15, 2015 13:48
-
-
Save precious-ming/5269599 to your computer and use it in GitHub Desktop.
两变量值互换(三个方法)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Change{ | |
public static void main(String[] args){ | |
java.util.Scanner input = new java.util.Scanner(System.in); | |
System.out.println("本程序交换数字x和y的值!\n"); | |
System.out.println("请您分别输入数字x和y的值,并以回车结束回车 "); | |
int x = input.nextInt(); | |
int y = input.nextInt(); | |
System.out.println("您输入的数字是: \n"+"x = "+x+"\ny = "+y); | |
int c; | |
c = x; | |
x = y; | |
y = c; | |
System.out.println("恭喜您!成功交换x和y的值:"+"\nx = "+x+"\ny = "+y); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Change2{ | |
public static void main(String[] args){ | |
java.util.Scanner input = new java.util.Scanner(System.in); | |
System.out.println("本程序用来交换x和y的值"); | |
System.out.println("请您分别输入数字x和y的值,并以回车结束回车 "); | |
int x = input.nextInt(); | |
int y = input.nextInt(); | |
System.out.println("您输入的x和y的值分别为:"+"\nx = "+x+"\ny = "+y); | |
x = x + y; | |
y = x - y; | |
x = x - y; | |
System.out.println("恭喜您!成功交换x和y的值:"+"\nx = "+x+"\ny = "+y); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Change3{ | |
public static void main(String[] args){ | |
java.util.Scanner input = new java.util.Scanner(System.in); | |
System.out.println("此程序交换两个变量的值"); | |
System.out.println("请您分别输入变量a,b的值,并以回车结束"); | |
int a = input.nextInt(); | |
int b = input.nextInt(); | |
System.out.println("您输入的变量a,b的值分别为: "+"\na = "+a+"\nb = "+b); | |
a = a^b; | |
b = a^b; | |
a = a^b; | |
System.out.println("恭喜您,成功交换变量a,b的值:"+"\na = "+a+"\nb = "+b); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment