Bài viết này sẽ hướng dẫn cách hoán đổi giá trị của 2 số bằng cách tạo một biến tạm(temp) trong ngôn ngữ lập trình java.
SwapNumbersWithTemp.java
/**
* @(#)SwapNumbersWithTemp.java
*
* SwapNumbersWithTemp application
*
* @author BUI NGOC SON
* @version 1.00 2013/12/5
*/
public class SwapNumbersWithTemp {
public static void main(String[] args) {
// TODO, add your application code
int a = 10;
int b = 20;
int temp = a; //temp holds a
a = b; // a is now b
b = temp; // and b is now a!
System.out.println("Gia tri cua bien a: " + a);
System.out.println("Gia tri cua bien b: " + b);
}
}
* @(#)SwapNumbersWithTemp.java
*
* SwapNumbersWithTemp application
*
* @author BUI NGOC SON
* @version 1.00 2013/12/5
*/
public class SwapNumbersWithTemp {
public static void main(String[] args) {
// TODO, add your application code
int a = 10;
int b = 20;
int temp = a; //temp holds a
a = b; // a is now b
b = temp; // and b is now a!
System.out.println("Gia tri cua bien a: " + a);
System.out.println("Gia tri cua bien b: " + b);
}
}
Sau khi chạy chương trình bạn sẽ được kết quả như hình bên dưới.
No comments:
Post a Comment