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

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

云南大学软件学院

实验报告

姓名:王定欢学号:20141120188 班级:日期:2016.10.8 成绩:

JAVA实验三

一、实验目的:

熟悉JAVA的控制流程,循环控制,数组结构,以及Big Numbers.

二、实验要求:

1. Write a Java program called AverageNumbers

2.java that calculates the average

of 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 the

main method using an array. Iterate through the array looking for your name using a for loop. Display the message "The 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=" 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 the

variable "count" and count 1 to 10.. Display "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){

相关文档
最新文档