学号20192311 实验二 《数据结构与面向对象程序设计》实验报告

学号 20192311 《数据结构与面向对象程序设计》实验二报告

课程:《程序设计与数据结构》
班级: 1923
姓名: 张源峰
学号:20192311

实验教师:王志强
实验日期:2020年10月11日
必修/选修: 必修

1.实验内容

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

2. 实验过程及结果

import java.util.Scanner;
public class a1 {
public static void main(String[] args) {
int a, x, b, c;
int d = 0;
Scanner scan = new Scanner(System.in);
do {
System.out.println("是否继续运算 1:是 2:否");
a = scan.nextInt();
if (a == 1) {
System.out.println("选择运算 1加法 2减法 3乘法 4除法 5求余");
x = scan.nextInt();
System.out.println("输入第一个数值");
b = scan.nextInt();
System.out.println("输入第二个数值");
c = scan.nextInt();
if(x1||x2||x3||x4||x==5) {
d = re(x, b, c);
System.out.println("结果是:"+d);
}
else {
break;
}
}
} while (a != 0);
}

public static int re(int x, int b, int c) {
    int d = 0;
    switch (x) {
        case 1:
            d = b + c;
            break;
        case 2:
            d = b - c;
            break;
        case 3:
            d = b * c;
            break;
        case 4:
            d = b / c;
            break;
        case 5:
            d = b % c;
            break;
    }
    return d;
}

}
创建副方法,以进行最简单的,不依赖junit的测试方法
public class a1test {
public static void main(String[]args){
if(a1.re(1,2,3)!=5)
{
System.out.println("加法运算错误");
}
else if(a1.re(2,5,3)!=2)
{
System.out.println("减法运算错误");
}
else if(a1.re(3,2,3)!=6)
{
System.out.println("乘法运算错误");
}
else if(a1.re(4,9,3)!=3)
{
System.out.println("除法运算错误");
}
else if(a1.re(5,7,5)!=2)
{
System.out.println("求余运算错误");
}
else
{
System.out.println("程序正确");
}
}
}

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

其他(感悟、思考等)

参考资料

posted @ 2020-10-11 14:29  20192311张源峰  阅读(434)  评论(1编辑  收藏  举报