ArrayDemo2

import

java.util.Scanner;

 

public

class ArrayDemo2 {

 

public static void main(String[] args) {

 

// different ways to define an array

int[] score = { 99, 100, 89, 98 };

 

int[] score1 = new int[4];

 

int[] score2 = new int[] { 77, 88, 99, 100 };

 

String[] score3 = new String[4];

 

for (int obj : score) {

 

System.out.println(obj);

 

}

 

for (int obj : score1) {

 

System.out.println(obj);

 

}

 

for (int obj : score2) {

 

System.out.println(obj);

 

}

 

for (String obj : score3) {

 

System.out.println(obj);

 

}

 

// Scanner input = new Scanner(System.in);

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

//

// score3[i] = input.next();

// }

// for (String obj : score3) {

//

// System.out.println(obj);

//

// }

 

// to get an average score by using array

int sum = 0;

int average = 0;

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

 

sum = sum + score2[i];

 

}

average = sum / score2.length;

System.out.println("average score is: " + average);

 

// to get an average score by using array, Console input scores

Scanner input = new Scanner(System.in);

System.out.println("Please input the score for the students: ");

 

int sum1 = 0;

int average1 = 0;

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

 

score1[i] = input.nextInt();

sum1 = sum1 + score1[i];

 

}

average1 = sum1 / score1.length;

System.out.println("average score is: " + average1);

 

}

 

}

posted @ 2016-09-23 14:01  mabelfdm  阅读(73)  评论(0编辑  收藏  举报