昆明理工大学信息工程与自动化学院学生实验报告
( 2012—2013学年第一学期)
课程名称:Java程序设计开课实验室:4422012年11月 7 日
一、实验目得及内容
目得:掌握与巩固Java结构化程序设计得概念、方法。
内容:
1.(使用&&、||、^运算符)编写一个程序,该程序让用户输入一个整数,然后判断该整数就是否
能同时被5与6整除;就是否能被5或6整除;就是否能被5或6整除,但不能同时被5与6整除。
例如:在命令行模式下该程序运行可呈现如下结果(注,也可以图形界面方式实现)
Enter an integer: 10
Is10divisibleby 5 and 6? false
Is10 divisible by 5or6? true
Is10 divisibleby 5 or 6,but notboth? true
2.编写一个程序(利用循环)计算下面式子:
3.写一个函数,该函数返回一个数组中值最小得元素得索引值,若该数组中值最小得元素多于一
个,则返回索引最小得那个,该函数得声明如下所示,在main函数中调用并测该函数。
publicstatic intindexOfSmallestElement(int[] list)
二、要求
1.给出上述程序得流程图、代码与测试结果。
内容一:
流程图:
实验代码
import java、util、Scanner;
public class Class1 {
publicstatic void main(String[] args) {
Scanner input=new Scanner(System、in);
System、out、print("Enter an integer:");
int numble=input、nextInt();
boolean numble1=(numble%5==0)&&(numble%6==0);
boolean numble2=(numble%5==0)||(numble%6==0);
booleannumble3=(numble%5==0)^(numble%6==0);
System、out、println("is "+numble+" divisible by5 and 6?"+numble1);
System、out、println("is "+numble+" divisible by 5 or 6?"+numble2);
System、out、println("is "+numble+" divisible by 5 or 6,but not both?"+numble3);
}
}
运行结果:
内容二: 流程图
实验代码
package JavaLibrary3;
import java、util、Scanner;
public class Class2 {
public static void main(String[] args){ Scanner input=new Scanner(System、in);
floatsum=0;
float denom=2;
for(floati=1;i<100、0;i++){
denom=i+1;
sum=sum+(i/denom);
}
System、out、println("1/2+2/3+3/4+……+98/99+99/100="+sum);
}
}
运行结果
内容三:
流程图
实验代码
packageJavaLibrary3;
package min;
public class Class3 {
public static int indexOfSmallestElement(int[] number) {
int min = number[0];
int count= 0;
for (int j = 1; j < number、length; j++) {
if (number[j]< min){
min=number[j];
count = j;
}
}
return (count);
}
public static void main(String[] args){
final int NUMBER_OF_ELEMENTS = 10;
int[]number = new int[NUMBER_OF_ELEMENTS];
int m;
java、util、Scanner input=newjava、util、Scanner(System、in);
System、out、print("Enter a new number:");
for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {
number[i] = input、nextInt();
}
m = indexOfSmallestElement(number);
System、out、println("最小值得下标 " + m);
}
}
运行结果