Share java 4 lab 4,

thanhnhanhuynh

New member
## Java 4 Lab 4

### Giới thiệu

Trong phòng thí nghiệm này, bạn sẽ làm việc với lớp máy quét ** ** của Java để đọc đầu vào từ bảng điều khiển.Bạn cũng sẽ tìm hiểu cách sử dụng phương thức ** printf ** để định dạng đầu ra.

### Mục tiêu

Đến cuối phòng thí nghiệm này, bạn sẽ có thể:

*Sử dụng lớp ** Trình quét ** để đọc đầu vào từ bảng điều khiển
*Sử dụng phương thức ** printf ** để định dạng đầu ra

### Bắt đầu

Mở tài liệu ** Máy quét ** và đọc qua phần tổng quan ** **.Điều này sẽ cung cấp cho bạn một sự hiểu biết tốt về cách thức hoạt động của lớp học.

### Đọc đầu vào

Lớp ** Trình quét ** cung cấp một số phương thức để đọc đầu vào từ bảng điều khiển.Phương pháp cơ bản nhất là phương thức ** tiếp theo **, đọc mã thông báo tiếp theo của đầu vào.Một mã thông báo là một chuỗi các ký tự được tách ra khỏi các mã thông báo khác bằng khoảng trắng.Ví dụ: chuỗi sau chứa ba mã thông báo: "Xin chào", "Thế giới" và "!".

`` `
Chào thế giới!
`` `

Phương thức ** tiếp theo ** sẽ đọc mã thông báo đầu tiên từ đầu vào và trả về nó dưới dạng chuỗi.Ví dụ: mã sau sẽ in mã thông báo đầu tiên từ đầu vào:

`` `
Máy quét máy quét = Máy quét mới (System.in);
Chuỗi mã thông báo = scanner.next ();
System.out.println (mã thông báo);
`` `

Khi bạn chạy mã này, bạn sẽ được nhắc nhập một số đầu vào.Nếu bạn nhập chuỗi "Hello World!", Đầu ra sẽ là "Xin chào".

Phương pháp ** tiếp theo ** cũng có thể được sử dụng để đọc các loại đầu vào khác nhau, chẳng hạn như số nguyên và nhân đôi.Ví dụ: mã sau sẽ đọc một số nguyên từ đầu vào và in nó:

`` `
Máy quét máy quét = Máy quét mới (System.in);
int number = Scanner.NextInt ();
System.out.println (số);
`` `

### Đầu ra định dạng

Phương thức ** printf ** có thể được sử dụng để định dạng đầu ra.Phương thức ** printf ** có chuỗi định dạng và số lượng đối số biến.Chuỗi định dạng chỉ định cách các đối số nên được định dạng.Ví dụ: mã sau in chuỗi "Xin chào thế giới!"in đậm:

`` `
System.out.printf ("Hello World!%N");
`` `

Ký tự **%n ** trong chuỗi định dạng cho biết phương thức ** printf ** để chèn một ký tự dòng mới.

Phương thức ** printf ** cũng có thể được sử dụng để định dạng số.Ví dụ: mã sau đây in số 123456789 trong ký hiệu khoa học:

`` `
System.out.printf ("123456789 = %e %n", 123456789);
`` `

### Bài tập

Trong bài tập này, bạn sẽ viết một chương trình đọc một số từ bảng điều khiển và in căn bậc hai của số.

** Bước 1: ** Tạo một dự án Java mới.

** Bước 2: ** Thêm mã sau vào tệp `main.java` của bạn:

`` `
nhập java.util.scanner;

lớp công khai chính {

công khai void void main (String [] args) {
Máy quét máy quét = Máy quét mới (System.in);
System.out.println ("Nhập một số:");
int number = Scanner.NextInt ();
System.out.println ("Bộ gốc của" + số + "là" + math.sqrt (số));
}

}
`` `

** Bước 3: ** Chạy chương trình của bạn.Khi bạn chạy chương trình, bạn sẽ được nhắc nhập một số.Nhập bất kỳ số nào và chương trình sẽ in căn bậc hai của số.

### Phần kết luận

Trong phòng thí nghiệm này, bạn đã học cách sử dụng lớp ** Scanner ** để đọc đầu vào từ bảng điều khiển và phương thức ** printf ** để định dạng đầu ra.Bạn cũng đã hoàn thành một bài tập sử dụng các kỹ năng này để viết một chương trình in căn bậc hai của một số.

### hashtags

* #Java
* #Programming
* #mã số
* #Learnjava
* #mã hóa
=======================================
## Java 4 lab 4

### Introduction

In this lab, you will be working with Java's **Scanner** class to read input from the console. You will also learn how to use the **printf** method to format output.

### Objectives

By the end of this lab, you will be able to:

* Use the **Scanner** class to read input from the console
* Use the **printf** method to format output

### Getting Started

Open the **Scanner** class documentation and read through the **Overview** section. This will give you a good understanding of how the class works.

### Reading Input

The **Scanner** class provides a number of methods for reading input from the console. The most basic method is the **next** method, which reads the next token of input. A token is a sequence of characters that is separated from other tokens by whitespace. For example, the following string contains three tokens: "Hello", "world", and "!".

```
Hello world!
```

The **next** method will read the first token from the input and return it as a string. For example, the following code will print the first token from the input:

```
Scanner scanner = new Scanner(System.in);
String token = scanner.next();
System.out.println(token);
```

When you run this code, you will be prompted to enter some input. If you enter the string "Hello world!", the output will be "Hello".

The **next** method can also be used to read different types of input, such as integers and doubles. For example, the following code will read an integer from the input and print it:

```
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
System.out.println(number);
```

### Formatting Output

The **printf** method can be used to format output. The **printf** method takes a format string and a variable number of arguments. The format string specifies how the arguments should be formatted. For example, the following code prints the string "Hello world!" in bold:

```
System.out.printf("Hello world!%n");
```

The **%n** character in the format string tells the **printf** method to insert a newline character.

The **printf** method can also be used to format numbers. For example, the following code prints the number 123456789 in scientific notation:

```
System.out.printf("123456789 = %e%n", 123456789);
```

### Exercise

In this exercise, you will be writing a program that reads a number from the console and prints the square root of the number.

**Step 1:** Create a new Java project.

**Step 2:** Add the following code to your `Main.java` file:

```
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
int number = scanner.nextInt();
System.out.println("The square root of " + number + " is " + Math.sqrt(number));
}

}
```

**Step 3:** Run your program. When you run the program, you will be prompted to enter a number. Enter any number and the program will print the square root of the number.

### Conclusion

In this lab, you learned how to use the **Scanner** class to read input from the console and the **printf** method to format output. You also completed an exercise that used these skills to write a program that prints the square root of a number.

### Hashtags

* #Java
* #Programming
* #Code
* #Learnjava
* #Coding
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top