学号20192323 2020-2021-1 《数据结构与面向对象程序设计》实验二报告

学号20192323 2020-2021-1 《数据结构与面向对象程序设计》实验二报告

课程:《程序设计与数据结构》
班级: 1923
姓名: 卢虹峄
学号:20192323
实验教师:王志强
实验日期:2020年10月8日
必修/选修: 必修

1.实验内容

(1) 编写简单的计算器,完成加减乘除模运算。
(2) 要求从键盘输入两个数,使用判定语句选择一种操作,计算结果后输出,然后使用判定和循环语句选择继续计算还是退出。
(3) 编写测试代码,测试验证。(https://www.cnblogs.com/rocedu/p/4472842.html)

2. 实验过程及结果

程序代码:
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
for (;😉{
System.out.println("input m and n");
float m=input.nextInt();
float n=input.nextInt();
System.out.println("select way");
System.out.println("1.m+n");
System.out.println("2.m%n");
System.out.println("3.mn");
System.out.println("4.m-n");
System.out.println("5.m/n");
int a=input.nextInt();
test(m, n, a);
if(a<1||a>5){
System.out.println("worry");
}
if(a5)
break;
}
}
public static float test(float x,float y,float a) {
float i=0;
if(a
1);{
i=x+y;
System.out.println("m+n="+(x+y));
}
if(a2);{
i=x%y;
System.out.println("m%n=" + (x % y));
}
if(a
3);{
i=x
y;
System.out.println("mn="+(xy));
}
if(a4);{
i=x-y;
System.out.println("m-n="+(x-y));
}
if(a
5);{
i=x/y;
System.out.println("m/n="+(x/y));
}
return i;
}
}


测试代码:
public class testTest {
public static void main(String[] args) {
if(test.test(1,2,1) != 3.0)
System.out.println("test failed!");
else System.out.println("test succeed!");
if(test.test(1,2,2) != 1.0)
System.out.println("test failed!");
else System.out.println("test succeed!");
if(test.test(1,2,3) != 2.0)
System.out.println("test failed!");
else System.out.println("test succeed!");
if(test.test(1,2,4) != -1.0)
System.out.println("test failed!");
else System.out.println("test succeed!");
if(test.test(1,2,5) != 0.5)
System.out.println("test failed!");
else System.out.println("test succeed!");
}
}

3. 实验过程中遇到的问题和解决过程

  • 问题1:选择方法时所有方法一起出现
  • 问题1解决方案:先是自己查看网上教学,但是还是无法改正,后来看了博客中其他同学的程序,有所启发。
  • 问题2:教程里的东西太理想化了,自己操做时才发现没那么简单。
  • 问题2解决方案:重复的查看教程,一遍遍修改。
  • ...

其他(感悟、思考等)

自己的编程能力不足导致编写程序时困难,一个地方卡壳很久,这次的程序主要用了main函数,代码数较长。

参考资料

posted @ 2020-10-10 16:05  20192323卢虹峄  阅读(90)  评论(0)    收藏  举报