Java: Nhập liệu với Scanner và System.in.read()


Khóa học qua video:
Lập trình Python All Lập trình C# All SQL Server All Lập trình C All Java PHP HTML5-CSS3-JavaScript
Đăng ký Hội viên
Tất cả các video dành cho hội viên

Video hướng dẫn:


package v1study;

import java.io.IOException;
import java.util.Scanner;

public class Bai5 {
  public static void main(String[] args) throws IOException {
    //Sử dụng lớp Scanner để nhập liệu cho các loại dữ liệu khác nhau.
    //Trước tiên cần tạo 1 đối tượng Scanner:
    Scanner input = new Scanner(System.in);
    int i;
    short s;
    byte b;
    long l;
    float f;
    double d;
    boolean bo;
    String str;
    char c;
    System.out.print("Nhập 1 số nguyên: ");
    i = input.nextInt();
    System.out.println("Sau khi nhập, i = " + i);
    System.out.print("Nhập 1 số nguyên short: ");
    s = input.nextShort();
    System.out.println("Sau khi nhập, s = " + s);
    System.out.print("Nhập 1 số nguyên byte: ");
    b = input.nextByte();
    System.out.println("Sau khi nhập, b = " + b);
    System.out.print("Nhập 1 số nguyên long: ");
    l = input.nextLong();
    System.out.println("Sau khi nhập, l = " + l);
    System.out.println("Nhập 1 số thực float: ");
    f = input.nextFloat();
    System.out.println("Sau khi nhập, f = " + f);
    System.out.print("Nhập 1 số thực double: ");
    d = input.nextDouble();
    System.out.println("Sau khi nhập, d = " + d);
    System.out.print("Nhập 1 giá trị boolean: ");
    bo = input.nextBoolean();
    System.out.println("Sau khi nhập, bo = " + bo);
    System.out.print("Nhập 1 chuỗi: ");
    input.nextLine();
    str = input.nextLine();
    System.out.println("Sau khi nhập, str = " + str);
    System.out.print("Nhập 1 ký tự: ");
    c = (char) System.in.read();
    System.out.println("Sau khi nhập, c = " + c);
  }
}
» Tiếp: Bài tập Java cơ bản
« Trước: Phép toán (Operator)
Khóa học qua video:
Lập trình Python All Lập trình C# All SQL Server All Lập trình C All Java PHP HTML5-CSS3-JavaScript
Đăng ký Hội viên
Tất cả các video dành cho hội viên
Copied !!!