This web page requires JavaScript to be enabled.

JavaScript is an object-oriented computer programming language commonly used to create interactive effects within web browsers.

How to enable JavaScript?

Bài thực hành 1: Mảng 1 chiều, 2 chiều với java [oop]

10cth1-2 March 14, 2012 0

Mảng 1 chiều và tìm phần tử lớn nhất:

[codes=java]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<n; i++)
    {
      System.out.print("Nhap phan tu thu a["+i+"]: ");
      a[i]= nhap.nextInt();
    }
    
    for( i=0; i<n; i++)
    {  
      System.out.print("\t"+a[i]);
    }
    
    int max=a[0];
    
    for( i=0; i<n; i++)
    {
      if(a[i]>max)
        max=a[i];
    }  System.out.print("\nMax la "+max);
  }
}[/codes]

Mảng 2 chiều:

[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<dong; i++)
      for(j=0;j<cot; j++)
        a[i][j]=(int) (Math.random()
50);
        
    for(i=0; i<dong; i++)
    {
      for(j=0;j<cot; j++)
        System.out.print("\t"+a[i][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<cot; i++)
      s+=a[i][k];
    System.out.print("Tong dong la: "+s);
  }  
}[/codes]


Last modified on March 31st, 2021 at 12:56 am

Nam Le
lequocnam



0 responds

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.