第七次作业
编写程序长度为5输出10 20 30 40 50
import java.util.Scanner; public class fewfw { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int []a={10,20,30,40,50}; for(int i=0;i<a.length;i++) System.out.println(a[i]); } }
动态输出长度为5 输出5个值
import java.util.Scanner; public class fewfw { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.println("输入五个数"); int []a=new int[5]; for(int i=0;i<a.length;i++){ {a[i]=input.nextInt();} System.out.println(a[i]); } } }
输出字符 c h i n a(数组)
import java.util.Scanner; public class wefw { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(); char[]a={'c','h','i','n','a'}; for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } } }
求同学的平均分和总分
import java.util.Scanner; public class gfrd { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input =new Scanner (System.in); double avg=0; double sum=0; double[]a=new double[5]; System.out.println("输入五个同学的成绩"); for (int i = 0; i < a.length; i++){ a[i]= input.nextDouble(); sum +=a[i]; avg =sum/5; } System.out.println(sum); System.out.println(avg); } }
数组12 53 23 44 53用 for foreach 倒叙
public class wang2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int []a= {12,53,23,44,53}; for (int i : a) { System.out.println(i); } for (int i =a.length-1; i>=0; i--) { System.out.println(a[i]); } } }
定义赋值求出奇数和偶数的个数
public class wang123 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int d = 0; int c = 0; for (int i = 0; i <a.length; i++) { if (a[i] % 2 == 0) { c++; } if (a[i] % 2 != 0) { d++; } } System.out.println(c); System.out.println(d); } }
输出1到100的数字用数组
public class wang123 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int a[] = new int[100]; for (int c = 0; c < 100; c++) { a[c] = c + 1; System.out.println(a[c]); } } }
10个人的成绩double 加5分但不能超过100分
import java.util.Scanner; public class wang123 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub double a[]=new double [10]; Scanner input=new Scanner(System.in); for(int i=0;i<10;i++){ a[i]=input.nextInt(); if(a[i]<95) a[i]+=5; if(a[i]>=95) a[i]=100; } for (int i = 0; i < a.length; i++) { System.out.println("加五分为"+a[i]); } } }