第六周上机作业。
1.
package ac;
public dd {
public static void main(String[] args) {
int a[]= {10,20,30,40,50};
for(int i=0;i<a.length;i++) {
System.out.println(a[i]);
}
}
}
2.
package ac;
import java.util.Scanner;
public class dd {
public static void main(String[] args) {
// TODO 自动生成的方法存根
int[] a=new int[5];
Scanner reader=new Scanner(System.in);
System.out.println("输入五个数");
for(int i=0;i<a.length;i++)
{
a[i]=reader.nextInt();
}
for(int i=0;i<a.length;i++)
{
System.out.print(" ");
System.out.print(a[i]);
}
}
}
3.
package ac;
public class dd {
public static void main(String[] args) {
//编写一个简单程序,定义整型数组,里面的元素是{23,45,22,33,56},求数组元素的和、平均值
int a[]= {23,45,22,33,56};
double sum=0;
for(int i=0;i<a.length;i++) {
sum+=a[i];
}
System.out.println("和"+sum+"平均值"+sum/5);
}
}
4.
package ac;
public class dd {
public static void main(String[] args) {
int a[]= {18,25,7,36,13,2,89,63},max=a[0],b=0;
for(int i=1;i<a.length;i++) {
if(max < a[i]) {
max = a[i];
b = i;
}
}
System.out.println("最大数为"+max+"下标为"+b);
}
}
5.
package ac;
public class dd {
public static void main(String[] args) {
// 将一个数组中的元素逆序存放(知识点:数组遍历、数组元素访问)
int [] a = new int [] {4,1,3,5,9,2,1};
int t;
for(int i = 0; i < a.length / 2; i ++) {
t = a[i];
a[i] = a[a.length - 1 - i];
a[a.length - 1 - i] = t;
}
for(int i = 0; i < a.length; i ++) {
System.out.print(a[i] + ", ");
}
}}