java 4 assignment

goldenkoala716

New member
S ## Java 4 Bài tập

### 1. Tạo một chương trình Java đơn giản in "Hello World!"

`` `java
lớp công khai Helloworld {
công khai void void main (String [] args) {
System.out.println ("Hello World!");
}
}
`` `

### 2. Viết chương trình Java tính toán tổng của hai số

`` `java
lớp công khai sumoftwonumbers {
công khai void void main (String [] args) {
int FirstNumber = 10;
int secondnumber = 20;
int sum = FirstNumber + SecondNumber;
System.out.println ("tổng của hai số là" + sum);
}
}
`` `

### 3. Viết chương trình Java in chuỗi Fibonacci

Trình tự Fibonacci là một loạt các số trong đó mỗi số là tổng của hai số trước.Trình tự bắt đầu bằng 0 và 1, và các số tiếp theo là 0 + 1 = 1, 1 + 1 = 2, 1 + 2 = 3, v.v.

`` `java
lớp công khai fibonaccisequence {
công khai void void main (String [] args) {
int FirstNumber = 0;
int SecondNumber = 1;
int nextnumber;

// In hai số đầu tiên trong chuỗi
System.out.println (FirstNumber);
System.out.println (thứ hai);

// Tính số tiếp theo trong chuỗi
for (int i = 0; i <10; i ++) {
nextNumber = FirstNumber + SecondNumber;
FirstNumber = SecondNumber;
SecondNumber = nextNumber;

// In số tiếp theo trong chuỗi
System.out.println (NextNumber);
}
}
}
`` `

### 4. Viết chương trình Java chơi một trò chơi Tic-Tac-Toe

Tic-Tac-Toe là một trò chơi cho hai người chơi.Mỗi người chơi thay phiên nhau đặt biểu tượng (X hoặc O) của họ vào một trong chín ô vuông trên lưới 3x3.Người chơi đầu tiên có được ba biểu tượng của họ liên tiếp, cột hoặc đường chéo thắng trò chơi.

`` `java
lớp công khai Tictactoe {

private static cuối cùng char trống_space = '';
Private Static cuối cùng char x_player = 'x';
private static cuối cùng char o_player = 'o';

Char tư nhân [] [] Hội đồng quản trị;
private int currentPlayer;

public Tictactoe () {
bảng = char mới [3] [3];
for (int i = 0; i <3; i ++) {
for (int j = 0; j <3; j ++) {
Bảng [j] = trống_space;
}
}

currentPlayer = x_player;
}

public void printboard () {
for (int i = 0; i <3; i ++) {
for (int j = 0; j <3; j ++) {
System.out.print (board [j] + "");
}
System.out.println ();
}
}

công khai boolean isgameover () {
// Kiểm tra xem có người chiến thắng không
for (int i = 0; i <3; i ++) {
if (board [0] == board [1] && board [1] == board [2] && board [0]! = trống_space) {
trả lại đúng;
}
if (board [0] == board [1] && board [1] == board [2] && board [0] ! = trống_space) {
trở lại
=======================================
s ## Java 4 Assignments

### 1. Create a simple Java program that prints "Hello World!"

```java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
```

### 2. Write a Java program that calculates the sum of two numbers

```java
public class SumOfTwoNumbers {
public static void main(String[] args) {
int firstNumber = 10;
int secondNumber = 20;
int sum = firstNumber + secondNumber;
System.out.println("The sum of two numbers is " + sum);
}
}
```

### 3. Write a Java program that prints the Fibonacci sequence

The Fibonacci sequence is a series of numbers where each number is the sum of the two previous numbers. The sequence starts with 0 and 1, and the next numbers are 0 + 1 = 1, 1 + 1 = 2, 1 + 2 = 3, and so on.

```java
public class FibonacciSequence {
public static void main(String[] args) {
int firstNumber = 0;
int secondNumber = 1;
int nextNumber;

// Print the first two numbers in the sequence
System.out.println(firstNumber);
System.out.println(secondNumber);

// Calculate the next number in the sequence
for (int i = 0; i < 10; i++) {
nextNumber = firstNumber + secondNumber;
firstNumber = secondNumber;
secondNumber = nextNumber;

// Print the next number in the sequence
System.out.println(nextNumber);
}
}
}
```

### 4. Write a Java program that plays a game of tic-tac-toe

Tic-tac-toe is a game for two players. Each player takes turns placing their symbol (X or O) in one of the nine squares on a 3x3 grid. The first player to get three of their symbols in a row, column, or diagonal wins the game.

```java
public class TicTacToe {

private static final char EMPTY_SPACE = ' ';
private static final char X_PLAYER = 'X';
private static final char O_PLAYER = 'O';

private char[][] board;
private int currentPlayer;

public TicTacToe() {
board = new char[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[j] = EMPTY_SPACE;
}
}

currentPlayer = X_PLAYER;
}

public void printBoard() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(board[j] + " ");
}
System.out.println();
}
}

public boolean isGameOver() {
// Check if there is a winner
for (int i = 0; i < 3; i++) {
if (board[0] == board[1] && board[1] == board[2] && board[0] != EMPTY_SPACE) {
return true;
}
if (board[0] == board[1] && board[1] == board[2] && board[0] != EMPTY_SPACE) {
return
 
Thực hiện một chương trình yêu cầu người dùng nhập một số nguyên dương.Sau đó, chương trình sẽ in tổng của tất cả các số nguyên dương từ 1 vào số được nhập bởi người dùng.
 
Join ToolsKiemTrieuDoGroup
Back
Top
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features of our website. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock