Java程序设计实验报告 实验名称:循环和数组

这篇实验报告详细介绍了使用Java进行循环和数组操作的多个实验,包括:1) 实现九九乘法口诀表;2) 设计扑克牌查询系统;3) 根据成绩赋予等级;4) 计算多个整数的最大公约数;5) 生成随机加法问题;6) 打印闰年。每个实验都包含问题描述、分析和实验代码,展示了Java编程中循环和数组的重要应用。
摘要由CSDN通过智能技术生成
              Java程序设计实验报告

实验名称:循环和数组
学号:
姓名:大聪TI学员
实验日期:2018年5月30日星期三

一.实验目的:
1. 遵循循环设计策略来开发循环,使用标记值控制循环。
2. 使用for语句编写循环。
3. 了解三种类型循环语句的相似处和不同点,编写嵌套循环。
4. 学习最小化数值误差的技术。
5. 从各种例子中学习循环。使用break和continue来实现程序的控制。
6. 描述数组在程序设计中的必要性。
7. 声明数组引用变量以及创建数组。
8. 利用数组初始化语法声明、创建和初始化数组。
9. 编写程序实现常用的数组操作(显示数组,对所有元素求和,求最小和最大元素,随机打乱和移动元素)
10. 将一个数组的内容复制到另一个数组,开发和调用数组参数和数组返回值的方法。
11. 使用线性查找算法或一分查找算法查找数组的元素。
12. 使用选择排序法对数组排序。
13. 使用java uti.Arrays类中的方法。从命令行传参数给主方法。

二.实验具体内容
1.实验:九九乘法口诀表
(1)题目
题目描述:打印九九乘法口诀表,格式如下:(注意:要有循环语句。)

* | 1 2 3 4 5 6 7 8 9

1 | 1 2 3 4 5 6 7 8 9
2 | 4 6 8 10 12 14 16 18
3 | 9 12 15 18 21 24 27
4 | 16 20

Java程序设计实验报告 实验实验题目:从键盘上读入10个字符串存入数组a中,然后输出这10个字符串中最大字符串 和最小字符串。 实验代码: public class StrPro { public static void main(String[] args) { String str[] = new String[5]; System.out.println("Please input 10 strings:"); int i; String max,min; for(i=0;i<5;i++){ System.out.print("Please input the "+(i+1)+" string:"); Scanner sc = new Scanner(System.in); str[i] = sc.nextLine(); } max = str[0]; min = str[0]; for(i=0;i<str.length;i++){ if(max.compareTo(str[i])<0){ max = str[i]; } if(min.compareTo(str[i])>0){ min = str[i]; } } System.out.println("最大的字符串为:"+max); System.out.println("最小的字符串为:"+min); } } 实验结果: 实验心得体会: 掌握了java的基本语法,数组的定义与使用,做这个实验要了解字符串数组的定义 及字符串数组的输入方法,还有比较字符串数组的大小的调用方法等。 实验实验题目: 自定义一个矩形类(Rectangle),包含的属性有:长(length),宽(width), 包含的方法有:关于属性的setter和getter方法,即setLength,getLength,setWidth ,getWidth,计算矩形面积的方法(getArea)。 定义矩形类的子类正方形类(Square),包含的属性和方法自行确定,要求完成的 功能是,能计算正方形的面积。 定义一个测试类(Test),测试矩形类和正方形类能否正确的计算面积。 以上类中属性和方法的访问权限自行确定,方法和构造方法若有参数,也自行确定 。 实验代码: public class Rectangle { int Length; int Width; public int getLength() { return Length; } public void setLength(int length) { Length = length; } public int getWidth() { return Width; } public void setWidth(int width) { Width = width; } int getArea(){ return Length * Width; } } public class Square extends Rectangle{ Square(int border) { super.setLength(border); super.setWidth(border); } } public class Test { public void test(){ System.out.println("请选择计算的形状的序号:1.矩形 2.正方形"); Scanner sc = new Scanner(System.in); int i = sc.nextInt(); int len,wid; if(i==1){ System.out.print("请输入矩形的长:"); Scanner s = new Scanner(System.in); len = s.nextInt(); System.out.print("请输入矩形的宽:"); wid = s.nextInt(); Rectangle re = new Rectangle(); re.setLength(len); re.setWidth(wid); System.out.println("矩形面积为:"+re.getArea()); } else if(i==2){ System.out.print("请输入正方形的边长:"); Scanner s = new Scanner(System.in); len = s.nextInt(); Square sq = new Square(len); System.out.println("正方形面积为:"+sq.getArea()); } else{ System.out.println("输入错误!"); } } public static v
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值