0% found this document useful (0 votes)
2 views

Chap 07 Java

The document contains a distance table showing the mileage between various cities including Chicago, Boston, New York, Atlanta, Miami, Dallas, and Houston. It also includes examples of initializing and manipulating two-dimensional arrays in Java, including methods for summing elements and finding maximum row sums. Additionally, it presents a simple program for inputting and summing elements of a 2D array using a scanner.

Uploaded by

Định Tqđ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chap 07 Java

The document contains a distance table showing the mileage between various cities including Chicago, Boston, New York, Atlanta, Miami, Dallas, and Houston. It also includes examples of initializing and manipulating two-dimensional arrays in Java, including methods for summing elements and finding maximum row sums. Additionally, it presents a simple program for inputting and summing elements of a 2D array using a scanner.

Uploaded by

Định Tqđ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

->

->

->

->
->
->

1
Distance Table (in miles)
double[][] distances = {
Chicago Boston New York Atlanta Miami Dallas Houston ········{ 0, 983, 787, 714, 1375, 967, 1087 },

Chicago 0 983 787 714 1375 967 1087 ········{ 983, 0, 214, 1102, 1763, 1723, 1842 },

Boston 983 0 214 1102 1763 1723 1842 ········{ 787, 214, 0, 888, 1549, 1548, 1627 },

New York 787 214 0 888 1549 1548 1627


········{ 714, 1102, 888, 0, 661, 781, 810 },

Atlanta 714 1102 888 0 661 781 810


········{ 1375, 1763, 1549, 661, 0, 1426, 1187 },
········{ 967, 1723, 1548, 781, 1426, 0, 239 },
Miami 1375 1763 1549 661 0 1426 1187
········{ 1087, 1842, 1627, 810, 1187, 239, 0 },
Dallas 967 1723 1548 781 1426 0 239
};
Houston 1087 1842 1627 810 1187 239 0

->

2
->
->
->

3
4
matrix = new int[5][5]; matrix[2][1] = 7;
Khởi tạo mảng 2 chiều 5x5 và gán nó cho matrix Gán giá trị 7 cho phần tử hàng 2, cột 1

5
Ta cũng có thể dùng một trình khởi tạo mảng để khai báo, khởi
tạo một mảng 2 chiều (a).
Và cái này tương đương với code ở bên b

int[][] array = {
int[][] array = new int[4][3];
{1, 2, 3},
array[0][0] = 1; array[0][1] = 2; array[0][2] = 3;
{4, 5, 6},
array[1][0] = 4; array[1][1] = 5; array[1][2] = 6;
{7, 8, 9},
array[2][0] = 7; array[2][1] = 8; array[2][2] = 9;
{10, 11, 12}
array[3][0] = 10; array[3][1] = 11; array[3][2] = 12;
};

(a) (b)

6
7
int[][] triangleArray = {
{1, 2, 3, 4, 5},
{2, 3, 4, 5},
{3, 4, 5},
{4, 5},
{5}
};

8
int[][] triangleArray = new int[5][];
triangleArray[0] = new int[5];
triangleArray[1] = new int[4];
triangleArray[2] = new int[3];
triangleArray[3] = new int[2];
triangleArray[4] = new int[1];

triangleArray[0][3] = 4;
triangleArray[4][0] = 5;

9
int[][] matrix = new int[10][10];

10
1 java.util.Scanner input = new Scanner(System.in);
Enter 10 rows and 10 columns
2 System.out.println("Enter " + matrix.length + "rows and “
3 + matrix[0].length + " columns: " ); 1 2 3 4 5 6 7 8 9 10 ↵ Enter
4 for (int row = 0; row < matrix.length; row++) { 1 2 3 4 5 6 7 8 9 10 ↵ Enter
5 for (int column = 0; column < matrix[0].length; column++) {
6 matrix[row][column] = input.nextInt();
1 2 3 4 5 6 7 8 9 10 ↵ Enter
7 } 1 2 3 4 5 6 7 8 9 10 ↵ Enter
8 }
1 2 3 4 5 6 7 8 9 10 ↵ Enter
1 2 3 4 5 6 7 8 9 10 ↵ Enter
1 2 3 4 5 6 7 8 9 10 ↵ Enter
1 2 3 4 5 6 7 8 9 10 ↵ Enter
1 2 3 4 5 6 7 8 9 10 ↵ Enter
1 2 3 4 5 6 7 8 9 10 ↵ Enter

11
1 for (int row = 0; row < matrix.length; row++) {
64 85 64 36 20 79 56 0 66 21
2 for (int column = 0; column < matrix[0].length; column++) {
3 matrix[row][column] = (int)(Math.random() * 100); 96 93 14 40 38 24 50 26 62 32
4 } 53 62 76 50 88 49 51 79 41 65
5 }
67 88 44 73 70 93 79 4 83 61
76 4 67 82 0 67 57 12 35 44
-> 11 91 65 38 64 42 72 95 65 21
90 8 94 28 23 67 99 47 35 1
39 19 27 76 47 45 83 31 32 21
17 66 98 45 88 14 38 9 62 74
73 67 68 93 47 27 77 43 36 6

12
1 for (int row = 0; row < matrix.length; row++) {
64 85 64 36 20 79 56 0 66 21
2 for (int column = 0; column < matrix[0].length; column++) {
3 System.out.println(matrix[row][column] + " ") 96 93 14 40 38 24 50 26 62 32
4 } 53 62 76 50 88 49 51 79 41 65
5 System.out.println();
6 }
67 88 44 73 70 93 79 4 83 61
76 4 67 82 0 67 57 12 35 44
11 91 65 38 64 42 72 95 65 21
90 8 94 28 23 67 99 47 35 1
39 19 27 76 47 45 83 31 32 21
17 66 98 45 88 14 38 9 62 74
73 67 68 93 47 27 77 43 36 6

13
1 int total = 0;
3 91 41 68 5 51 77 11 22 56
2 for (int row = 0; row < matrix.length; row++) {
3 for (int column = 0; column < matrix[0].length; column++) { 46 74 49 31 47 10 50 92 33 0
4 total += matrix[row][column]; 26 85 94 0 7 26 0 30 35 63
5 }
6 }
5 69 52 66 66 93 8 63 54 52
23 54 2 59 76 9 39 24 70 10
0 50 46 71 23 73 4 11 56 26
11 68 0 80 4 61 54 93 50 33
4224
76 12 60 50 10 45 9 38 17 20
46 5 2 58 72 8 39 78 47 79
73 62 56 73 11 19 84 33 23 88

14
1 for (int column = 0; column < matrix[0].length; column++) {
sum for column 0 is 442
2 int total = 0;
3 for (int row = 0; row < matrix.length; row++) { sum for column 1 is 451
4 total += matrix[row][column]; sum for column 2 is 428
5 }
6 System.out.println("sum for column " + column + " is "
sum for column 3 is 444
7 + total); sum for column 4 is 339
8 }
sum for column 5 is 633
sum for column 6 is 412
sum for column 7 is 363
sum for column 8 is 518
sum for column 9 is 423

15
1 int maxRowSum = 0;
Row 2 has the maximum sum of 616
2 int indexOfMaxRow = 0;
3 for (int row = 0; row < matrix.length; row++) {
4 int totalOfThisRow = 0;
5 for (int column = 0; column < matrix[0].length; column++) {
6 totalOfThisRow += matrix[row][column];
7 }
8 if (totalOfThisRow > maxRowSum) {
9 maxRowSum = totalOfThisRow;
10 indexOfMaxRow = row;
11 }
12 }
13 System.out.println("Row " + indexOfMaxRow +
14 " has the maximum sum of " + maxRowSum);

16
64 83 70 53 96 2 54 69 34 76
21 20 25 65 45 1 0 17 16 78
13 71 91 68 63 70 99 53 83 28

1 for (int i = 0; i < matrix.length; i++) { 66 23 95 62 40 15 27 10 95 39

2 74 14 87 65 16 46 60 53 77 54
for (int j = 0; j < matrix[0].length; j++) {
57 1 51 87 0 21 2 58 69 36
3 int i1 = (int)(Math.random() * matrix.length);
11 22 16 27 69 18 54 62 85 60
4 int j1 = (int)(Math.random() * matrix[i].length);
19 74 6 59 86 74 0 75 94 89
5
49 14 24 11 73 95 64 80 93 58
6 int temp = matrix[i][j];
56 65 54 86 74 52 70 53 82 70
7 matrix[i][j] = matrix[i1][j1];
8 matrix[i1][j1] = temp; After Random Shuffling:
9 } 25 53 70 53 86 62 21 20 58 21
10 } 1 6 22 69 83 0 53 77 73 63
15 14 56 95 53 39 17 1 70 70
57 24 71 27 64 16 2 74 16 54
87 19 11 74 64 14 49 89 54 82
69 70 76 11 45 51 65 34 75 86
99 27 66 13 93 74 95 91 23 78
74 83 80 59 87 54 10 60 62 36
96 95 40 69 65 85 46 0 65 16
52 54 94 58 0 18 60 2 68 28

17
18
1 import java.util.Scanner; Enter 3 rows and 4 columns:
2 1 2 3 4 ↵ Enter
3 public class PassTwoDimensionalArray {
5 6 7 8 ↵ Enter
4 public static void main(String[] args) {
int[][] m = getArray(); // Get an array
9 10 11 12 ↵ Enter
5
6
7 // Display sum of elements Sum of all elements is 78
8 System.out.println("\nSum of all elements is " + sum(m));
Process finished with exit code 0
9 }
10
11 public static int[][] getArray() {
... ...
24 }
25
26 public static int sum(int[][] m) {
... ...
35 }
36 }

19
11 public static int[][] getArray() { 26 public static int sum(int[][] m) {
12 // Create a Scanner 27 int total = 0;
13 Scanner input = new Scanner(System.in); 28 for (int row = 0; row < m.length; row++) {
14 29 for (int column = 0; column < m[row].length; column++) {
15 // Enter array values 30 total += m[row][column];
16 int[][] m = new int[3][4]; 31 }
17 System.out.println("Enter " + m.length + " rows and " 32 }
18 + m[0].length + " columns: "); 33
19 for (int i = 0; i < m.length; i++) 34 return total;
20 for (int j = 0; j < m[i].length; j++) 35 }
21 m[i][j] = input.nextInt();
22
23 return m;
24 }
25

20
Keys to the Questions
0 1 2 3 4 5 6 7 8 9
Student 0 A B A C C D E E A D

Students’ Answers to the Questions:


0 1 2 3 4 5 6 7 8 9
Student 0 A B A C C D E E A D
Student 1 D B A B C A E E A D
Student 2 E D D A C B E E A D
Student 3 C B A E D C E E A D
Student 4 A B D C C D E E A D ->
Student 5 B B E C C D E E A D ->
Student 6 B B A C C D E E A D ->
Student 7 E B E C C D E E A D

21
Keys to the Questions
0 1 2 3 4 5 6 7 8 9
Student 0 A B A C C D E E A D

Students’ Answers to the Questions:


0 1 2 3 4 5 6 7 8 9
Student 0 A B A C C D E E A D
Student 1 D B A B C A E E A D
Student 2 E D D A C B E E A D
Student 3 C B A E D C E E A D
Student 4 A B D C C D E E A D ->
Student 5 B B E C C D E E A D ->
Student 6 B B A C C D E E A D ->
Student 7 E B E C C D E E A D

22
x y
0 -1 3
1 -1 -1
2 1 1
3 2 0.5
4 2 -1
5 3 3
6 4 2
7 4 -0.5

23
5 3 7 5 3 4 6 7 8 9 1 2

6 1 9 5 6 7 2 1 9 5 3 4 8

9 8 6 1 9 8 3 4 2 5 6 7

8 6 3 8 5 9 7 6 1 4 2 3

4 8 3 1 4 2 6 8 5 3 7 9 1

7 2 6 7 1 3 9 2 4 8 5 6

6 9 6 1 5 3 7 2 8 4

4 1 9 5 2 8 7 4 1 9 6 3 5

8 7 9 3 4 5 2 8 6 1 7 9

(a) Puzzle (b) Solution

24
5 3 0 0 7 0 0 0 0 int[][] grid =

6 0 0 1 9 5 0 0 0 { { 5, 3, 0, 0, 7, 0, 0, 0, 0 },
{ 6, 0, 0, 1, 9, 5, 0, 0, 0 },
0 9 8 0 0 0 0 6 0
{ 0, 9, 8, 0, 0, 0, 0, 6, 0 },
8 0 0 0 6 0 0 0 3 { 8, 0, 0, 0, 6, 0, 0, 0, 3 },
4 0 0 8 0 3 0 0 1 { 4, 0, 0, 8, 0, 3, 0, 0, 1 },
{ 7, 0, 0, 0, 2, 0, 0, 0, 6 },
7 0 0 0 2 0 0 0 6
{ 0, 6, 0, 0, 0, 0, 2, 8, 0 },
0 6 0 0 0 0 0 0 0
{ 0, 0, 0, 4, 1, 9, 0, 0, 5 },
0 0 0 4 1 9 0 0 5 { 0, 0, 0, 0, 8, 0, 0, 7, 9 }

0 0 0 0 8 0 0 7 9 };

25
Một bài giải
{ { 5, 3, 4, 6, 7, 8, 9, 1, 2 },

-> { 6, 7, 2, 1, 9, 5, 3, 4, 8 },
{ 1, 9, 8, 3, 4, 2, 5, 6, 7 },
-> { 8, 5, 9, 7, 6, 1, 4, 2, 3 },

-> { 4, 2, 6, 8, 5, 3, 7, 9, 1 },
{ 7, 1, 3, 9, 2, 4, 8, 5, 6 },
{ 9, 6, 1, 5, 3, 7, 2, 8, 4 },
{ 2, 8, 7, 4, 1, 9, 6, 3, 5 },
{ 3, 4, 5, 2, 8, 6, 1, 7, 9 }
};

26
1 import java.util.Scanner;
2
Enter a Sudoku puzzle solution:
3 public class CheckSudokuSolution {
9 6 3 1 7 4 2 5 8 ↵ Enter
4 public static void main(String[] args) {
5 // Read a Sudoku solution 1 7 8 3 2 5 6 4 9 ↵ Enter
6 int[][] grid = readASolution();
7 2 5 4 6 8 9 7 3 1 ↵ Enter
8 System.out.println(isValid(grid) ?
9 "Valid solution" : "Invalid solution");
8 2 1 4 3 7 6 9 6 ↵ Enter
10 }
4 9 6 8 5 2 3 1 7 ↵ Enter
11
12 /** Read a Sudoku solution from the console */ 7 3 5 9 6 1 8 2 4 ↵ Enter
13 public static int[][] readASolution() {
14 // Create a Scanner 5 8 9 7 1 3 4 6 2 ↵ Enter
15 Scanner input = new Scanner(System.in);
16
3 1 7 2 4 6 9 8 5 ↵ Enter
17 System.out.println("Enter a Sudoku puzzle solution:");
6 4 2 5 9 8 1 7 3 ↵ Enter
18 int[][] grid = new int[9][9];
19 for (int i = 0; i < 9; i++)
20 for (int j = 0; j < 9; j++)
21 grid[i][j] = input.nextInt(); Valid solution
22
23 return grid;
Process finished with exit code 0
24 }
25

27
26 /** Check whether a solution is valid */
27 public static boolean isValid(int[][] grid) { grid[i][j]
28 for (int i = 0; i < 9; i++)
29 for (int j = 0; j < 9; j++)
grid[(i/3)*3][(j/3)*3]
30 if (grid[i][j] < 1 || grid[i][j] > 9
31 || !isValid(i, j, grid))
32 return false;
33 return true; // The solution is valid
34 }
35
36 /** Check whether grid[i][j] is valid in the grid */
37 public static boolean isValid(int i, int j, int[][] grid) {
38 // Check whether grid[i][j] is unique in i's row
39 for (int column = 0; column < 9; column++)
if (row != i && col != j && grid[row][col] == grid[i][j])
40 if (column != j && grid[i][column] == grid[i][j])
41 return false;
42
43 // Check whether grid[i][j] is unique in j's column
44 for (int row = 0; row < 9; row++)
45 if (row != i && grid[row][j] == grid[i][j])
46 return false;
47
48 // Check whether grid[i][j] is unique in the 3−by−3 box
49 for (int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++)
50 for (int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++)
51 if (!(row == i && col == j) && grid[row][col] == grid[i][j])
52 return false;
53
54 return true; // The current value at grid[i][j] is valid
55 }
56 }

28
29
Which student Which exam Multiple-choice or essay score

double[][][] scores = new double[6][5][2]; scores [i] [j] [k]

double[][][] scores = {
{{7.5, 20.5}, {9.0, 22.5}, {15, 33.5}, {13, 21.5}, {15, 2.5}},
{{4.5, 21.5}, {9.0, 22.5}, {15, 34.5}, {12, 20.5}, {14, 9.5}},
{{6.5, 30.5}, {9.4, 10.5}, {11, 33.5}, {11, 23.5}, {10, 2.5}},
{{6.5, 23.5}, {9.4, 32.5}, {13, 34.5}, {11, 20.5}, {16, 7.5}},
{{8.5, 26.5}, {9.4, 52.5}, {13, 36.5}, {13, 24.5}, {16, 2.5}},
{{9.5, 20.5}, {9.4, 42.5}, {13, 31.5}, {12, 20.5}, {16, 6.5}}};
};

30
x = new int[2][2][5];
x[0] x[1]
x[0][0] x[0][1] x[1][0] x[1][1]

31
32
1 import java.util.Scanner;
2
3 public class GuessBirthdayUsingArray {
4 public static void main(String[] args) {
5 int day = 0;
6 int answer;
7
8 int[][][] dates = {
9 {{1, 3, 5, 7},
10 {9, 11, 13, 15},
11 {17, 19, 21, 23},
12 {25, 27, 29, 31}},
13 {{2, 3, 6, 7},
14 {10, 11, 14, 15},
15 {18, 19, 22, 23},
16 {26, 27, 30, 31}},
17 {{4, 5, 6, 7},
18 {12, 13, 14, 15},
19 {20, 21, 22, 23},
20 {28, 29, 30, 31}},
21 {{8, 9, 10, 11},
22 {12, 13, 14, 15},
23 {24, 25, 26, 27},
24 {28, 29, 30, 31}},
25 {{16, 17, 18, 19},
26 {20, 21, 22, 23},
27 {24, 25, 26, 27},
28 {28, 29, 30, 31}}};
29

33
30 // Create a Scanner
31 Scanner input = new Scanner(System.in);
32
33 for (int i = 0; i < 5; i++) {
34 System.out.println("Is your birthday in Set" + (i + 1) + "?");
35 for (int j = 0; j < 4; j++) {
36 for (int k = 0; k < 4; k++)
37 System.out.printf("%4d", dates[i][j][k]);
38 System.out.println();
39 }
40
41 System.out.print("\nEnter 0 for No and 1 for Yes: ");
42 answer = input.nextInt();
43
44 if (answer == 1)
45 day += dates[i][0][0];
46 }
47
48 System.out.println("Your birthday is " + day);
49 }
50 }

34
Is your birthday in Set1?
1 3 5 7
9 11 13 15
17 19 21 23
25 27 29 31

Enter 0 for No and 1 for Yes: 1 ↵ Enter


Is your birthday in Set2?
2 3 6 7
10 11 14 15
18 19 22 23
26 27 30 31

Enter 0 for No and 1 for Yes: 0 ↵ Enter


Is your birthday in Set3?
4 5 6 7
12 13 14 15
20 21 22 23
28 29 30 31

Enter 0 for No and 1 for Yes: 1 ↵ Enter


Is your birthday in Set4?
8 9 10 11
12 13 14 15
24 25 26 27
28 29 30 31

Enter 0 for No and 1 for Yes: 0 ↵ Enter


Is your birthday in Set5?
16 17 18 19
20 21 22 23
24 25 26 27
28 29 30 31

Enter 0 for No and 1 for Yes: 1 ↵ Enter


Your birthday is 21

35
->
->
-> Tìm độ dài array bằng arrayRefVar.length
->
-> foreach loop

36
->
->
-> Tìm độ dài array bằng arrayRefVar.length
->
-> foreach loop

37
38
39
40
41
42
double[] myList = new double[4];

myList reference
myList[0] 5.6

myList[1] 4.5

Array reference myList[2] 3.3


variable
myList[3] 13.2

43
44
45
46
47
double[] myList = new double[];
System.out.println("Enter " + myList.length + " values: ");
for (int i = 0; i < myList.length; i++)
myList[i] = input.nextDouble();

for (int i = 0; i < myList.length; i++) {


myList[i] = Math.random() * 100;
}

48
for (int i = 0; i < myList.length; i++) {
System.out.print(myList[i] + " ");
}

char[] myList = {'a', 'b', 'c'};


System.out.println(myList);

49
double total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i];
}

double max = myList[0];


for (int i = 0; i < myList.length; i++) {
if (myList[i] > max) max = myList[i];
}

50
int indexOfMax = 0;
double max = myList[0];
for (int i = 0; i < myList.length; i++) {
if (myList[i] > max) {
max = myList[i];
indexOfMax = i;
}
}

for (int i = 0; i < myList.length - 1; i++) {


int randomIndex = (int)(Math.random() * myList.length);

double temp = myList[i];


myList[i] = myList[randomIndex];
myList[randomIndex] = temp;
}

51
double temp = myList[0]; // Retain the first element

// Shift elements 1 pos to the left


for (int i = 0; i < myList.length - 1; i++) {
myList[i] = myList[i + 1];
}

// Move the first element to fill in the last position


myList[myList.length - 1] = temp;

Thêm hình vào đây

52
String[] months = {"January", "February","December"};
System.out.println("Enter a month number (1 to 12): ");
int monthNumber = input.nextInt();
System.out.println("The month is " + months[monthNumber - 1]);

53
for (elementType element : arrayRefVar) {
// Process the element
}

for (double e : myList) {


System.out.println(e);
}

54
for (double e : myList) {
System.out.println(e);
}

55
56
double[] numbers = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5};
Arrays.sort(numbers);
Arrays.parallelSort(numbers);

char[] chars = {'a', 'A', '4', 'F', 'D', 'P'};


Arrays.sort(chars,1,3);
Arrays.parallelSort(chars,1,3);

[1.9, 2.9, 3.4, 3.5, 4.4, 6.0]


[a, 4, A, F, D, P]

57
int[] list = {2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79};
System.out.println("1. Index is " +
java.util.Arrays.binarySearch(list, 11));
System.out.println("2. Index is " +
java.util.Arrays.binarySearch(list, 12));

char[] chars = {'a', 'c', 'g', 'x', 'y', 'z'};


System.out.println("3. Index is " +
java.util.Arrays.binarySearch(chars, 'a'));
System.out.println("4. Index is " +
java.util.Arrays.binarySearch(chars, 't'));

1. Index is 4
2. Index is -6
3. Index is 0
4. Index is -4

58
int[] list = {2, 4, 7, 10};
System.out.println(Arrays.toString(list));

[2, 4, 7, 10]

int[][] list = {
{2, 4, 7, 10},
{1, 3, 5, 7},
}; [[I@1d251891, [I@48140564]
System.out.println(Arrays.toString(list));

59
int[] list1 = {2, 4, 5, 8};
int[] list2 = {2, 4, 5, 8};
int[] list3 = {4, 2, 5, 8};
System.out.println(Arrays.equals(list1, list2)); // TRUE
System.out.println(Arrays.equals(list2, list3)); // FALSE

60
int[] list1 = {2,4,7,10};
int[] list2 = {2,4,7,7,7,10};
Arrays.fill(list1, 5); // Fills 5 to the whole array
System.out.println(Arrays.toString(list1));
Arrays.fill(list2, 1, 5, 8); // Fills 8 to a partial array
System.out.println(Arrays.toString(list2));

[5, 5, 5, 5]
[2, 8, 8, 8, 8, 10]

61
public class A { public class TestMain {
public static void main(String[] args) { public static void main(String[] args) {
String[] strings = {"New York", "Boston", "Atlanta" }; for (int i = 0; i < args.length; i++)
TestMain.main(strings); System.out.println(args[i]);
} }
} }

62
> java TestMain “First num” alpha 53
First num
alpha
53

63
> java TestMain “First num” alpha 53
First num
alpha
53

64
> java Calculator 45 + 56 ↵ Enter
45 + 56 = 101

> java Calculator 45 - 56 ↵ Enter


45 - 56 = -11

> java Calculator 45 . 56 ↵ Enter


45 . 56 =2520

> java Calculator 45 + 56 ↵ Enter


45 / 56 = 0

>

65
1 public class Calculator {
2 /** Main method */
3 public static void main(String[] args) {
4 // Check number of strings passed
5 if (args.length != 3) {
6 System.out.println(
7 "Usage: java Calculator operand1 operator operand2");
8 System.exit(1);
9 }
10
11 // The result of the operation
12 int result = 0;
13
14 // Determine the operator
15 switch (args[1].charAt(0)) {
16 case '+': result = Integer.parseInt(args[0]) +
17 Integer.parseInt(args[2]);
18 break;
19 case '-': result = Integer.parseInt(args[0]) -
20 Integer.parseInt(args[2]);
21 break;
22 case '.': result = Integer.parseInt(args[0]) *
23 Integer.parseInt(args[2]);
24 break;
25 case '/': result = Integer.parseInt(args[0]) /
26 Integer.parseInt(args[2]);
27 }
28
29 // Display result
30 System.out.println(args[0] + ' ' + args[1] + ' ' + args[2]
31 + " = " + result);
32 }
33 }

66
1 public class Test {
2 public static void main(String[] args) {
3 for (int i = 0; i < args.length; i++) {
4 System.out.println(args[i]);
5 }
6 }
7 }

CHÈN THÊM TERMINAL Ở ĐÂY

67

You might also like