nhap mang
package kiemtra;
import java.io.*;
import java.net.*;
public class mang {
public static int nhap(){
String st;
int n;
//do{
DataInputStream stream =new DataInputStream(System.in);
try{
st=stream.readLine();
}catch(IOException e) {st="0";}
try{
n=Integer.valueOf(st).intValue();
}catch(NumberFormatException e) {n=0;}
return n;
//}while (n<0);
}
public static float tbc(int a,int b){
float tbc=(float)a/b;
return tbc;
}
public static void main(String[] args) {
int[]a=new int[50];
int n,i,j,k,tongchan=0,ptc=0;
float TBC=0;
System.out.println("nhap vao so phan tu cho mang:");
do{
n=nhap();
}while(n<0);
System.out.println("nhap cac phan tu mang:");
for (i=0;i<n;i++){
System.out.print("a["+i+"]=");
a[i]=nhap();
}
System.out.print("mang vua nhap la a["+n+"]:");
for(i=0;i<n;i++){
// System.out.println("a[="+i+"]=" +a[i]);
System.out.print(" "+a[i]);
}
for (i=0;i<n;i++){
if (i%2==0){
tongchan=tongchan+a[i];
ptc=ptc+1;
}
}
TBC=tbc(tongchan,ptc);
System.out.println();
System.out.println("tong cac phan tu chan trong mang:"+tongchan);
System.out.println("trung binh cong cac phan tu chan trong mang bang: "+(float)TBC);
}
}
Bạn đang đọc truyện trên: AzTruyen.Top