Bài tập java #2 : Biến trong java

HuanBuiThanh
Bài 3 – Biến trong Java

PHP Code:
import java.io.*;
public class 
Hello {
                public static 
void main(String[] argsthrows Exception {
                                
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                                
System.out.print("Nhap a: ");
                                
int a Integer.parseInt(in.readLine());
                                
System.out.print("Nhap b: ");
                                
int b Integer.parseInt(in.readLine());
                                
int ketqua;
                                
ketqua a+b;
                                
System.out.println("Ket qua bai toan a+b la: “ + ketqua);
                }
}  


Nhập thử 2 số a và b vào đi bạn, kết quả bài toán a+b sẽ được in ra.
*Lí thuyết:

PHP Code:
import java.io.*;
public class 
Hello {
                public static 
void main(String[] argsthrows Exception {
                                
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                                
System.out.print("Your name is: ");
                                
String str;
                                
str in.readLine();
                                
System.out.println("Welcome " str " to Java");
                }
}  

Tạm thời, trong chương trình này, bạn chỉ nên quan tâm đến dòng
String str khai báo biến str kiểu chuỗi, và

PHP Code:
System.out.println("Welcome " str " to Java")  

Đây cũng là dòng System.out.println như chương trình đầu, có khác là + str + tức là đưa một biến vào chuỗi in ra. Chỉ đến đó thôi nhé, sau đó, hãy quan tâm đến bài hôm nay

PHP Code:
System.out.print("Nhap a: ");int a Integer.parseInt(in.readLine());System.out.print("Nhap b: ");int b Integer.parseInt(in.readLine());int ketqua;ketqua a+b;System.out.println("Ket qua bai toan a+b la: “ + ketqua);  

Giải thích
import bạn nhập class hay thư viện chuẩn, tạm thời đừng quan tâm nó là gì, chỉ cần nhớ là có nó để chương trình chạy
System.out.print in ra một chuỗi, nhưng không xuống dòng
System.out.println in ra một chuỗi, nhưng xuống dòng
int ketqua tức là khai báo biến ketqua kiểu int
ketqua = a+b tức là gán kết quả một biểu thức tính toán (ở đây là biến a + biến b ) cho biến ketqua
System.out.println("Ket qua bai toan a+b la: “ + ketqua) thì đơn giản rồi, in cái dòng đó ra, chỉ khác là nó đưa biến ketqua của bạn vào chuỗi đó. 

Getting Info...

Đăng nhận xét

Cảm ơn bạn đã quan tâm và gửi nhận xét tại
http://huanbuithanh.blogspot.com
hãy ghé thăm blog hàng ngày để được cập nhật những thủ thuật mới nhất nhé . thân ái !
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.