Bài thực hành Java tạo mảng 1 chiều và 2 chiều tự sinh tuần 1:
Tạo mảng 1 chiều và tìm phần tử lớn nhất:
[codes=java]/*
Bai tap 3
le.qnam
*/
import java.util.*;
import java.io.*;
class bt3
{
public static void main (String[] args) {
int a[],n=0,i;
Scanner nhap=new Scanner(System.in);
System.out.print(“Nhap vao so phan tu n”);
n=nhap.nextInt();
a=new int[n+1];
for( i=0; i
System.out.print(“Nhap phan tu thu a[“+i+”]: “);
a[i]= nhap.nextInt();
}
for( i=0; i
System.out.print(“\t”+a[i]);
}
int max=a[0];
for( i=0; i
if(a[i]>max)
max=a[i];
} System.out.print(“\nMax la “+max);
}
}[/codes]
Tự tạo mảng 2 chiều và tính tổng của dòng thứ k:
[codes=java]import java.util.*;
import java.io.*;
import java.lang.*;
class bt4
{
public static void main (String[] args)
{
Scanner nhap=new Scanner(System.in);
int a[][],i,j;
System.out.print(“Nhap so dong”); int dong=nhap.nextInt();
System.out.print(“Nhap so cot”); int cot=nhap.nextInt();
a=new int [dong][cot];
for(i=0; i
for(i=0; i
for(j=0;j
System.out.print(“\n”);
}
int s=0;
System.out.print(“Nhap vao dong thu k can tinh tong:”);
int k=nhap.nextInt();
for(i=0; i
System.out.print(“Tong dong la: “+s);
}
}[/codes]
Last modified on December 9th, 2020 at 12:58 am
Nam Le
lequocnam
0 responds