anh em cho hỏi nha hàm nhập 1 số trong java là hàm gì vậy
VD trong C là scanf
trong C++ là cin>>
System.out.println() là hàm xuất vậy hàm nhập là gì
anh em cho hỏi nha hàm nhập 1 số trong java là hàm gì vậy
VD trong C là scanf
trong C++ là cin>>
System.out.println() là hàm xuất vậy hàm nhập là gì
// sao bạn hỏi ở nhiều diễn đàn vậy , có làm được chưa ?
class nhap
{
public static void main(String args[])
{
System.out.print("Nhap vao mot so : ");
String a = System.console().readLine(); // du`ng method nhap console().readLine() ;
// lưu ý kiểu nhập vào là String giống C# , ta phải chuyển kiểu cho nó nếu muốn !!!
System.out.println("Ban vua nhap : "+a);
}
}
Dùng class Scanner cũng được.
Nếu muốn hiểu rõ, bạn lên Java api, vào mục java.util, tìm chỗ Scanner
// sao bạn hỏi ở nhiều diễn đàn vậy , có làm được chưa ?
class nhap
{
public static void main(String args[])
{
System.out.print("Nhap vao mot so : ");
String a = System.console().readLine(); // du`ng method nhap console().readLine() ;
// lưu ý kiểu nhập vào là String giống C# , ta phải chuyển kiểu cho nó nếu muốn !!!
System.out.println("Ban vua nhap : "+a);
}
}
Reply With Quote
nhập ký tự thì nói làm gì cậu,mình thấy trong sách nói cái đó nhiều,nhưng nhập số nguyên hay số thực thì hiếm,lạ thật
[=========> Bổ sung bài viết <=========]
các bạn phân tích code nhập này xem saoCode:TextField input; Label inputLabel; int radius; public void init() { setFont(new Font("VNI-Centur",Font.BOLD,12)); input = new TextField( 5 ); input.addActionListener( this ); inputLabel = new Label( "Enter radius: " ); add( inputLabel ); add( input ); }
[=========> Bổ sung bài viết <=========]
cảm ơn đã quan tâm,mình biết làm rồi,đây là code nhập 2 số và tính tổngCode:import java.io.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader in=new BufferedReader (new InputStreamReader(System.in)); System.out.println("NHAP A:"); int a=Integer.parseInt (in.readLine()); System.out.println("NHAP B:"); int b=Integer.parseInt (in.readLine()); int ketqua; ketqua=a+b; System.out.println("ket qua bai toan a+b la:"+ketqua); } }
Được sửa bởi coder_gate lúc 09:37 ngày 05-07-2008 Reason: Bổ sung bài viết
Sax , mi`nh có ghi chú mà
La`m gì có nhập một số nguyên hay số thực từ bàn phím ??? Chỉ có nhập chuổi ru`i chuyển nó sang số nguyên chứ .String a = System.console().readLine(); // du`ng method nhap console().readLine() ;
// lưu ý kiểu nhập vào là String giống C# , ta phải chuyển kiểu cho nó nếu muốn !!!
Xài class Scanner cũng đươc
Code:import java.util.Scanner; public class Test { public static void main(String[] args) { int a; Scanner nhap = Scanner(System.in); a = nhap.nextInt(); ...; } }
Cứ gõ system.in nhập từ bàn phím
Java 6 có Console, bạn có thể dùng class này để làm việc giống như trên .NET
http://dangtritue.blogspot.com
System.console().readLine();
Erox làm gì có cú pháp này!
Post bài gì kỳ vậy!?
[=========> Bổ sung bài viết <=========]
Đây là 1 cách nữa!
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Nhap vao 1 chuoi:");
Scanner scan=new Scanner(System.in); //su dung thu vien Scanner
String userInput = scan.next();
System.out.println(userInput);
}
Được sửa bởi krisscage lúc 12:13 ngày 25-03-2009 Reason: Bổ sung bài viết
class NhapXuat
{
public static BufferedReader x = new BufferedReader(new InputStreamReader(System.in));
public int a;
public double b;
public String st;
public int docSoNguyen(String inform) throws Exception
{
try
{
System.out.print(inform + ":\t");
this.a = Integer.parseInt(x.readLine());
}
catch(Exception x)
{
docSoNguyen("Nhap sai dinh dang, nhap lai !");
}
return this.a;
}
public double docSoThuc(String inform) throws Exception
{
try
{
System.out.print(inform + ":\t");
this.b = Double.parseDouble(x.readLine());
}
catch(Exception x)
{
docSoNguyen("Nhap sai dinh dang, nhap lai !");
}
return this.b;
}
public String docChuoi(String inform) throws Exception
{
System.out.print(inform + ":\t");
this.st = x.readLine();
return this.st;
}
}
Bookmarks