题目: Java基本语法
课程名称: JAVA语言程序设计
班 级: 信1705-1
姓 名: 刘雨馨 学号: 20173445
指导教师: 王建民
正文部分格式要求:
1、 正文汉子部分要求宋体小四,行间距固定值20磅;首行缩进两个字符。
2、 程序英文部分要求采用Times New Roman字体,行间距固定值20磅,程序缩进采用四个字符,程序部分要求采用必要的注释。
3、 实验报告要求完成以下三项内容:
1) 按照题目内容要求编写程序实现功能。
2) 实验报告中要求包括程序设计思想、程序流程图、源程序、实现结果截图、实验总结(包括调试过程中出现的错误等)。
一、
源程序:
Calculation.java
public class Calculation { private int operator; //操作数 private int operand1; //操作数前面的数 private int operand2; //操作数后面的书 private int result; //运算结果,除法结果取整数部分 private int statistics; //统计正误 public int getResult(){ return result; } public int getStatistics(){ return statistics; } public Calculation( int operand1 , int operator , int operand2 ){ this.operand1 = operand1; this.operator = operator; this.operand2 = operand2; } public void manager(){ switch(operator){ case 1: System.out.println( operand1 +" + " + operand2 +" = " ); result = operand1 + operand2; break; case 2: System.out.println( operand1 +" - " + operand2 +" = " ); result = operand1 - operand2; break; case 3: System.out.println( operand1 +" * " + operand2 +" = " ); result = operand1 * operand2; break; case 4: System.out.println( operand1 +" / " + operand2 +" = " ); result = operand1 / operand2; break; } } public void judge( int result ){ //判断正误 if( this.result == result){ statistics = 1; //正确为1 } else statistics = 0; //错误为0 } }
Arithmetic.java
import java.util.Random; import java.util.Scanner; public class Arithmetic { @SuppressWarnings("resource") public static void main( String[] args ){ Random rand = new Random(); Scanner in = new Scanner( System.in ); int operand1 = 0 ; int operand2 = 0; int operator; int result; int count=0; //统计正确的题目数量 int i; Calculation[] cal = new Calculation[30]; //30道题目 //随机题目并显示序号与题目 for( i=0; i<30; i++ ){ operator = rand.nextInt(4)+1; switch( operator ){ case 1: operand1 = rand.nextInt(100); operand2 = rand.nextInt(100); break; case 2: //减法不出现负数 operand1 = rand.nextInt(100); operand2 = rand.nextInt( operand1 ); break; case 3: //乘除范围更小 operand1 = rand.nextInt(10); operand2 = rand.nextInt(10); break; case 4: operand1 = rand.nextInt(20); operand2 = rand.nextInt(9)+1; break; } System.out.print( (i+1) +" "); cal[i] = new Calculation( operand1, operator, operand2); cal[i].manager(); result = in.nextInt(); //输入答案并判断正误 cal[i].judge( result); } //结算 System.out.println("错误的题目及正确答案:"); for( i=0; i<30; i++){ if( cal[i].getStatistics() == 0 ){ System.out.println( i+1 +" "+ cal[i].getResult() ); } else count++; } System.out.println(); System.out.println("题目总数30道,正确"+count+"道,错误"+(30-count)+"道"); } }
实验结果截图
实验总结:
题目不难但是做的过程中小错误频发,不够细心也缺少练习,思路不确定,经常修改
调试错误:
原因是没有实例化对象,这个错误出现很多次了,需要注意记住
二、
源代码:
Random.java
public class Random { private String rand; Random(){ //生成验证码 rand = ""; for( int i=0; i<6; i++ ){ int intVal = (int)(Math.random() * 26 + 97); rand = rand + (char)intVal; //此处+用作连接符 } public void judge( String rand ){ if( this.rand.equals(rand) ) System.out.println("验证码正确"); else System.out.println("验证码错误"); } public String getRand(){ return rand; } }
Validate.java
import java.util.Scanner; public class Validate { public static void main( String [] args ){ String rand = new String(); @SuppressWarnings("resource") Scanner in = new Scanner( System.in ); Random ran = new Random(); System.out.println(ran.getRand()); System.out.println("请输入验证码"); rand = in.next(); ran.judge(rand); } }
运行结果:
总结:
总的还算顺利,第一次输出的时候在验证码前面带了一个null,需要让rand为空