int[] array = new int[]{2,8,4,9,5,2,4,7,8,3,4,2,2,1,3,45,5};//數(shù)組
讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:域名與空間、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、武漢網(wǎng)站維護、網(wǎng)站推廣。
double sum = 0;//用來保存數(shù)組內(nèi)所有數(shù)值的和
for(int i=0;iarray.length;i++){
sum+=array[i];//遍歷每一個數(shù)組相,將每一個數(shù)組相的值加到sum上
}
double avg = sum/array.length;//計算平均值:總和除以總個數(shù)
System.out.println(avg);//打印計算結(jié)果
public static void main(String[] args) {
/**
*
* (1)實現(xiàn)整型數(shù)組的創(chuàng)建要求由用戶輸入數(shù)組長度以及數(shù)組元素
*/
Scanner input = new Scanner(System.in);
System.out.print("請輸入整型數(shù)組的長度:");
int lengthInt = input.nextInt();
int[] arrInt = new int[lengthInt];
for (int i = 0; i arrInt.length; i++) {
System.out.println("請輸入第" + (i + 1) + "個元素");
arrInt[i] = input.nextInt();
}
System.out.print("整型數(shù)組:");
for (int i = 0; i arrInt.length; i++) {
System.out.print(arrInt[i] + "\t");
}
System.out.println();
/**
*
* (2)查找數(shù)組中最大值最小值以及求平均值
*/
int max = 0;
int min = arrInt[0];
double sum = 0;
for (int i = 0; i arrInt.length; i++) {
if (arrInt[i] max) {
max = arrInt[i];
}
if (arrInt[i] min) {
min = arrInt[i];
}
sum += arrInt[i];
}
double avg = sum / arrInt.length;
System.out.println("最大值:" + max);
System.out.println("最小值:" + min);
System.out.println("平均值:" + avg);
/**
*
* (3)實現(xiàn)數(shù)組的排序,有用戶選擇何種方式
*/
System.out.println("請選擇排序的方式(輸入數(shù)字即可):");
System.out.println("1:由小到大,2:由大到小");
int a = input.nextInt();
switch (a) {
case 1:
int i,
j,
t;
for (i = 0; i arrInt.length - 1; i++) {
for (j = 0; j arrInt.length - i - 1; j++) {
if (arrInt[j + 1] arrInt[j]) {
t = arrInt[j + 1];
arrInt[j + 1] = arrInt[j];
arrInt[j] = t;
}
}
}
break;
case 2:
int x,
y,
z;
for (x = 0; x arrInt.length - 1; x++) {
for (y = 0; y arrInt.length - x - 1; y++) {
if (arrInt[y + 1] arrInt[y]) {
z = arrInt[y + 1];
arrInt[y + 1] = arrInt[y];
arrInt[y] = z;
}
}
}
break;
default:
System.out.println("輸入有誤");
break;
}
for (int k = 0; k arrInt.length; k++) {
System.out.print(arrInt[k] + "\t");
}
System.out.println();
/**
*
* (4)實現(xiàn)數(shù)組的插入,并對插入后的數(shù)組重排序
*/
System.out.println("該數(shù)據(jù)長度為" + arrInt.length + ",插入數(shù)據(jù)后也記錄最后" + "個的元素");
boolean ref = true;
do {
System.out.println("請插入元素:");
for (int i = 0; i arrInt.length; i++) {
if (i arrInt.length - 1)
arrInt[i] = arrInt[i + 1];
else
arrInt[i] = input.nextInt();
}
System.out.println("是否再插入新的元素?(是:Y,否:其他任何字符)");
if (!input.next().equalsIgnoreCase("y")) {
ref = false;
}
} while (ref);
System.out.print("插入元素后的數(shù)組:");
for (int i = 0; i arrInt.length; i++) {
System.out.print(arrInt[i] + "\t");
}
System.out.println();
/**
*
* (5)實現(xiàn)對某個數(shù)組的刪除或者修改操作
*/
System.out.println("請選擇操作:1.刪除;2.修改");
int d = input.nextInt();
System.out.println("請輸入要操作的元素下標:(輸入0-" + (arrInt.length - 1)
+ "之內(nèi)的數(shù)字即可):");
int index = input.nextInt();
System.out.println(index);
int[] arrs = new int[arrInt.length - 1];
switch (d) {
case 1:
for (int i = 0; i arrInt.length - 1; i++) {
if (i == index index arrInt.length - 1) {
arrs[i] = arrInt[index + 1];
} else if (i index index arrInt.length - 1) {
arrs[i] = arrInt[i + 1];
} else {
arrs[i] = arrInt[i];
}
}
for (int i = 0; i arrs.length; i++) {
System.out.print(arrs[i] + "\t");
}
break;
case 2:
System.out.println("請輸入新的元素:");
int num = input.nextInt();
arrInt[index] = num;
for (int i = 0; i arrInt.length; i++) {
System.out.print(arrInt[i] + "\t");
}
break;
default:
System.out.println("輸入有誤");
break;
}
}
Java里正確格式應(yīng)該是寫做
int[][] a={{123,345},{34,56},{34,56}};
a[2][1]結(jié)果是56 因為數(shù)組是從0開始的 所以[2]是指{34,56}這一組
同理 [1]是指第二個 即56
import?java.util.Scanner;
public?class?Test?{
public?static?void?main(String[]?args)?{
int?num?=?5;
int[]?n?=?new?int[num];
Scanner?sc?=?new?Scanner(System.in);
System.out.println("請輸入"+num+"位會員的積分");
for(int?i=0;inum;i++){
System.out.print("第"+(i+1)+"位會員積分:");
n[i]=sc.nextInt();
}
System.out.println("\n序號\t歷史積分");
for(int?i=0;in.length;i++){
System.out.println(i+1+"\t"+n[i]);
}
sc.close();
}
}
按照你的要求編寫的給定一個數(shù)組,計算各元素之和的Java程序如下
public class AA {
public static void main(String[] args) {
int a[]={1,0,2,6,8,8,7,8,2};
int sum=0;
for(int i=0;ia.length;i++){
sum=sum+a[i];
}
System.out.println("sum="+sum);
}
}
運行結(jié)果
sum=42
package test;
import java.util.Random;
public class Test {
public static void main(String[] args) {
int maxVal=-1;
int maxRow=-1;
int maxCol=-1;
int[][] arr=new int[5][5] ;
for(int i=0;i5;i++){
for(int j=0;j5;j++){
arr[i][j]=new Random().nextInt(100);
if(maxValarr[i][j]){
maxVal=arr[i][j];
maxRow=i;
maxCol=j;
}
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
System.out.println("最大行 "+maxRow);
System.out.println("最大列 "+maxCol);
System.out.println("max number is "+maxVal);
}
}