Java结构化程序设计实验报告.doc
文本预览下载声明
一、实验目的及内容
目的:掌握和巩固Java结构化程序设计的概念、方法。
内容:
(使用、||、^运算符)编写一个程序,该程序让用户输入一个整数,然后判断该整数是否能同时被5和6整除;是否能被5或6整除;是否能被5或6整除,但不能同时被5和6整除。
例如:在命令行模式下该程序运行可呈现如下结果(注,也可以图形界面方式实现)
Enter an integer: 10
Is 10 divisible by 5 and 6? false
Is 10 divisible by 5 or 6? true
Is 10 divisible by 5 or 6, but not both? true
编写一个程序(利用循环)计算下面式子:
写一个函数,该函数返回一个数组中值最小的元素的索引值,若该数组中值最小的元素多于一个,则返回索引最小的那个,该函数的声明如下所示,在main函数中调用并测该函数。
public static int indexOfSmallestElement(int[] list)
二、要求
给出上述程序的流程图、代码和测试结果。
1.输入一个整数判断被5和6整除
流程图:
程序代码:
//package divisiblejudgement;
import java.util.Scanner;
public class DivisibleJudgement {
public static void main(String[] args) {
System.out.print(Enter an integer: );
Scanner input = new Scanner(System.in);
int number = input.nextInt();
System.out.println(Is + number + divisible by 5 and 6?
+ (number % 5 == 0 number % 6 == 0) );
System.out.println(Is + number + divisible by 5 or 6?
+ (number % 5 == 0 || number % 6 == 0));
System.out.println(Is + number + divisible by 5 or 6, but not both?
+ (number % 5 == 0 ^ number % 6 == 0) );
}
}
测试结果:
2. 用循环求和
流程图:
程序代码:
//package addcalculate;
//import java.util.Scanner;
public class AddCalculate {
public static void main(String[] args) {
final int number=100;
double sum = 0;
double a = 1, b;
for (int i = 1; i number; i++) {
b = a + 1;
sum += a / b;
a++;
}
System.out.println(The sum is + sum);
}
}
测试结果:
3. 求数组中值最小的元素的索引值
流程图:
程序代码:
//package indexfunction;
import java.util.Scanner;
public class IndexFunction {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(Enter the number of element:);
int length = input.nextInt();
int[] array = new int[length];
System.out.println(Enter the element:);
for (int i = 0; i array.length;
显示全部