20192325郭权武 实验二 《数据结构与面向对象程序设计》实验报告

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

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

1.实验内容

此处填写实验的具体内容:
(1) 编写简单的计算器,完成加减乘除模运算。
(2) 要求从键盘输入两个数,使用判定语句选择一种操作,计算结果后输出,然后使用判定和循环语句选择继续计算还是退出。
(3) 编写测试代码,测试验证。

2. 实验过程及结果

代码为:
import java.sql.SQLOutput;
import java.util.Scanner;
public class shiyantwo {
public static void main(String[] args) {
double a;
double b;
int c;
Scanner scan = new Scanner(System.in);
double result;
int e;
do{
System.out.println("input c chose +(1) or -(2) or (3) or /(4) or %(5) ");
System.out.println("Please input number a,b,c");
a = scan.nextDouble();
b = scan.nextDouble();
c = scan.nextInt();
while( b0 && c4 || b0 && c5) {
System.out.println("please try again,change b!=0: ");
b = scan.nextDouble();
}
result=verygood(a,b,c);
System.out.println("result=" + result);
System.out.print("continue chose 1;end chose 0 : ");
e = scan.nextInt();
}while (e==1);
}
public static double verygood(double a,double b,int c){
double d=0.0;
switch(c){
case 1:d=a+b;break;
case 2:d=a-b;break;
case 3:d=a
b;break;
case 4:d=a/b;break;
case 5:d=a%b;break;
}
return d;
}
}
运行成功截图


测试代码为:
public class shijiantwoTest {
public static void main(String[] args) {
double result=0.0;
result = shiyantwo.verygood(5,5.,1);
if(result10)
System.out.println("pass 1");
else
System.out.println("wrong 1");
result = shiyantwo.verygood(5,5.,2);
if(result
0)
System.out.println("pass 2");
else
System.out.println("wrong 2");
result = shiyantwo.verygood(5,5.,3);
if(result25)
System.out.println("pass 3");
else
System.out.println("wrong 3");
result = shiyantwo.verygood(5,5.,4);
if(result
1)
System.out.println("pass 4");
else
System.out.println("wrong 4");
result = shiyantwo.verygood(5,5.,5);
if(result==0)
System.out.println("pass 5");
else
System.out.println("wrong 5");
}
}
测试成功截图为:

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

  • 问题1:对循环语句使用错误
  • 问题1解决方案:翻阅课本查找资料,询问同学。
  • 问题2:测试代码编写时遇到障碍
  • 问题2解决方案:观看教程,仔细思考。

其他(感悟、思考等)

学习编程需要循序渐进,多看,多做,才能熟悉且正确的掌握知识点。

参考资料

posted @ 2020-10-10 18:01  20192325郭权武  阅读(152)  评论(1编辑  收藏  举报