第七周
- 编写一个简单程序,要求数组长度为5,静态赋值10,20,30,40,50,在控制台输出该数组的值。
package gshf.fswy.fwf;
public class test1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] x={10,20,30,40,50};
for(int y:x){
System.out.println(y+" ");
}
}
}
2.编写一个简单程序,要求数组长度为5,动态赋值,并在控制台输出该数组的值。
package gshf.fswy.fwf;
import java.util.Scanner;
public class test2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method st
Scanner input=new Scanner(System.in);
int [] a=new int[5];
for (int i = 0;i < a.length; i++){
a[i]=input.nextInt();
}
}
}
3.定义字符型数组,分别存储c、h、 i、n、a 并在控制台输出
package gshf.fswy.fwf;
public class test3 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String []a={"c","h","i","n","a"};
for (int i=0;i<a.length;i++){
System.out.println(a[i]);
}
}
}
4.输入5个学生成绩,求总分和平均分
package gshf.fswy.fwf;
import java.util.Scanner;
public class test4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
int []a=new int[5];
int sum = 0;
double b=0;
for (int i=0; i<a.length; i++){
a[i]=input.nextInt();
sum+=a[i];
}
b=sum/5.0;
System.out.println("总分数为:"+sum+"平均数为:"+b);
}
}
5.定义数组{12,53,23,44,53} 用for和foreach分别输出,再倒序输出
package gshf.fswy.fwf;
public class test5 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[]=new int []{12,53,23,44,53};
for(int i=0;i<a.length;i++){
System.out.println("for循环输出"+a[i]);
}
for (int i : a){
System.out.println("foreach循环输出"+i);
}
for(int i=4;i>=0;i--){
System.out.println("倒序输出"+a[i]);
}
}
}
1
package a;
public class f {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int []a={2,5,3,4,56,9,13,86,9,10};
int os=0;
int js=0;
for (int i = 0; i < 10; i++) {
if(a[i]%2==0){
os++;
}
else{
js++;
}
}
System.out.println("偶数个数"+os);
System.out.println("奇数个数"+js);
}
}
2
package a;
public class g {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int []a=new int[100];
for (int i= 0; i <a.length; i++) {
a[i]=i+1;
System.out.println(a[i]);
}
}
3.
package a;
public class h {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
double []cj={66.6,76.9,30.5,77.8,89.2,34,56.9,58.2,95.6,88.8};
double jia=0;
for (int i = 0; i < 10; i++) {
jia=cj[i]+5;
System.out.println("加5分后为"+jia);
if(jia>100){
System.out.println(jia+"超过100分成绩为100");
}
}
}
}