当前位置:文档之家› 云南大学软件学院Java实验三

云南大学软件学院Java实验三

云南大学软件学院实验报告姓名:王定欢学号:20141120188 班级:日期:2016.10.8 成绩:JAVA实验三一、实验目的:熟悉JAVA的控制流程,循环控制,数组结构,以及Big Numbers.二、实验要求:1. Write a Java program called AverageNumbers2.java that calculates the averageof numbers 1 to 50 using the for loop. Do it again using the while loop.2.Write a Java program called InputParms.java that accepts 3 arguments in themain method using an array. Iterate through the array looking for your name using a for loop. Display the message "The name <your name> was found" if your name is found.3.Write a Java program called BreakLoop.java that uses a for loop with the variable"count" and count 1 to 10.. Display "count=<count>" each time you loop through. Break out of the loop at 5. At the end of the program display "Broke out of the loop at count = 5".4.Write a Java program called ContinueLoop.java that uses a for loop with thevariable "count" and count 1 to 10.. Display "count=<count>" each time you loop through. Skip the display statement using the continue statement if count = 5. At the end of the program display "Used continue to skip printing 5".三、实验内容:一.①代码:package lab3;public class AverageNumbers2 {public static void main(String[]args){int a,b=0,i=1;for(i=1;i<=50;i++){a=i;b=b+a;}double avg;avg=b/50.0;System.out.println("The for loop averagenumber is:"+avg);int num=1,sum=0;while(num<=50){sum=sum+num;num++;}double avg1;avg1=sum/50.0;System.out.println("The while loop averagenumber is:"+avg1);}}②.实验结果:二.①.代码:package lab3;import java.util.Scanner;public class InputaParms {public static void main(String[] args){String name[] = new String [3];System.out.println("please input three name");Scanner in = new Scanner(System.in);for(int i=0;i<3;i++){System.out.println("please input the order"+(i+1)+" name:");name[i] = in.next();}System.out.println("please input your name:");String yourname = in.next();for(int j=0;j<3;j++){if (name[j].equals(yourname)) {System.out.println("your name "+name[j]+" was found");}}}}②.实验结果:三.①代码:package lab3;public class BreakLoop {public static void main(String[]args){for(int i=0;i<10;i++){if(i==5){break;}System.out.println("count="+i);}System.out.println("Broke out of the loop at count = 5");}}②.实验结果:四.①代码:package lab3;public class ContinueLoop {public static void main(String[]args){for(int i=0;i<10;i++){if(i==5){continue;}System.out.println("count="+i);}System.out.println("Used continue to skip printing 5");}}②.实验结果五.附加题①,代码:package lab3;import java.util.Arrays;import java.util.Random;public class array {public static void main(String[]args){Random r = new Random();int a[][] = new int[10][10];for(int n=0;n<10;n++){for(int m=0;m<10;m++){a[n][m] = r.nextInt(10);}}printN(a);//打印数组方法System.out.println("-------------------");int max = a[0][0];int sum = 0;int[] b = new int[10];for(int i = 0;i<10;i++){max=a[i][0];for(int j=0;j<10;j++){if(a[i][j]>max){max=Math.max(max, a[i][j]);}}b[i]=max;System.out.print(b[i]+" ");sum=max+sum;}//对数组进行排序,冒泡排序法int temp=0;for(int i=0;i<10;i++){for(int j=i;j<9;j++){if (b[j]>b[j+1]){temp=b[j+1];b[j+1]=b[j];b[j]=temp;}}}double mid ;//求中位数if (b.length%2==0) {int dex = b.length/2;mid = (b[dex]+b[dex+1])/2;}else mid = (b.length +1)/2;//求平均数double avg;avg=sum/10.0;System.out.println("平均数为:"+avg); System.out.println("中位数为::"+mid); }// 数组打印方法private static void printN(int[][] a){for(int n=0;n<a.length;n++){for(int m=0;m<a[n].length ;m++){System.out.print(a[n][m]+" ");}System.out.println();}}}②.实验结果:四、实验总结:本次实验主要学习了如何运用控制流程结构,循环控制结构,数组结构三大结构,和C语言的用法基本一致。

其中break是跳出循环语句块,执行循环语句下面的语句;continue是跳出所在的当前循环,继续进行下次循环,不执行循环中continue下面的语句。

指导教师签名:。

相关主题