成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

java算數(shù)測試代碼,java算術測試

用java語言做一個簡單的小學生算數(shù)測試器

package com.wqx.print;

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供郊區(qū)網(wǎng)站建設、郊區(qū)做網(wǎng)站、郊區(qū)網(wǎng)站設計、郊區(qū)網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、郊區(qū)企業(yè)網(wǎng)站模板建站服務,十多年郊區(qū)做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。

import java.util.Random;

import java.util.Scanner;

public class Exam {

private static int result=0;

Scanner scanner=new Scanner(System.in);

Random ran=new Random();

public boolean create()

{

boolean re=false;

int num1=createnum();

int num2=createnum();

int str=ran.nextInt(4);

if(str==0)

{

System.out.print(num1+" + "+num2+" = ");

result=num1+num2;

}

else if(str==1)

{

while(num2num1)

{

num2=createnum();

}

System.out.print(num1+" - "+num2+" = ");

result=num1-num2;

}

else if(str==2)

{

while(num1==0||num2==0)

{

if(num1==0)

{

num1=createnum();

}

if(num2==0)

{

num2=createnum();

}

}

System.out.print(num1+" x "+num2+" = ");

result=num1*num2;

}

else if(str==3)

{

while(num1%num2!=0)

{

num1=createnum();

num2=createnum();

while(num2==0)

{

num2=createnum();

}

}

System.out.print(num1+" ÷ "+num2+" = ");

result=num1/num2;

}

try {

int answer=scanner.nextInt();

if(answer==result)

{

re=true;

}

} catch (Exception e) {

System.out.println("請正確輸入答案!");

}

if(re==false)

{

System.out.println("正確答案"+result);

}

return re;

}

public int createnum()

{

int num=ran.nextInt(101);

if(num0||num100)

{

num=ran.nextInt(101);

}

return ran.nextInt(100);

}

public int createstr()

{

int str=ran.nextInt(4);

if(str0||str4)

{

str=createstr();

}

return str;

}

public static void main(String[] args) {

int count=0;

int right=0;

Scanner scanner=new Scanner(System.in);

while(true)

{

System.out.print("按任意鍵繼續(xù) 退出按 'Q' ");

if(scanner.next().toUpperCase().equals("Q"))

{

System.out.println("總題數(shù):"+count+"\t"+"正確數(shù):"+right);

break;

}

boolean temp=new Exam().create();

count++;

if(temp==true)

{

right++;

System.out.println("正確");

}else

{

System.out.println("錯誤");

}

}

}

}

java課程設計“小學算術運算測試程序”

花了好幾個小時,測試通過,希望樓主多給點分

import java.util.Scanner;

class Question{

private int firstParam;

private int secondParam;

private String operator;

private int answer;

private int score;

public int getFirstParam() {

return firstParam;

}

public void setFirstParam(int firstParam) {

this.firstParam = firstParam;

}

public int getSecondParam() {

return secondParam;

}

public void setSecondParam(int secondParam) {

this.secondParam = secondParam;

}

public String getOperator() {

return operator;

}

public void setOperator(String operator) {

this.operator = operator;

}

public int getAnswer() {

return answer;

}

public void setAnswer(int answer) {

this.answer = answer;

}

public int getScore() {

return score;

}

public void setScore(int score) {

this.score = score;

}

//構(gòu)造

public Question(int firstParam, int secondParam) {

this.firstParam = firstParam;

this.secondParam = secondParam;

}

}

public class StudentStudy {

private int questionNumber;

private String operator;

private Question[] questions;

private int rightNumber=0;

private int wrongNumber=0;

public String getOperator() {

return operator;

}

public void setOperator(String operator) {

this.operator = operator;

}

public int getQuestionNumber() {

return questionNumber;

}

public void setQuestionNumber(int questionNumber) {

this.questionNumber = questionNumber;

}

public int getRightNumber() {

return rightNumber;

}

public void setRightNumber(int rightNumber) {

this.rightNumber = rightNumber;

}

public int getWrongNumber() {

return wrongNumber;

}

public void setWrongNumber(int wrongNumber) {

this.wrongNumber = wrongNumber;

}

public Question[] getQuestions() {

return questions;

}

public void setQuestions(Question[] questions) {

this.questions = questions;

}

public int getRandom(){

return (int)(Math.random()*10);

}

public int calculateByOperator(String operator,int first,int second){

switch(operator.charAt(0)){

case '+':

return first+second;

case '-':

return first-second;

default:

return 0;

}

}

public void makeQuestion(){

mywhile:

while(questions[questionNumber-1]==null){

Question question=new Question(getRandom(),getRandom());

for(int i=0;questions[i]!=nulliquestionNumber;i++){

if((question.getFirstParam()==questions[i].getFirstParam())(question.getSecondParam()==questions[i].getSecondParam())){

continue mywhile;

}

}

for(int i=0;iquestionNumber;i++){

if(questions[i]==null){

questions[i]=question;

questions[i].setOperator(operator);

questions[i].setScore(100/questionNumber);

questions[i].setAnswer(calculateByOperator(operator,questions[i].getFirstParam(),questions[i].getSecondParam()));

break;

}

}

}

}

public StudentStudy(int questionNumber){

this.questionNumber=questionNumber;

questions=new Question[this.questionNumber];

}

public static void main(String[] args){

Scanner input=new Scanner(System.in);

System.out.println("現(xiàn)在開始測試,請?zhí)顚懸獪y試的試題數(shù)量(1-100):");

int number=input.nextInt();

StudentStudy ss=new StudentStudy(number);

System.out.println("請選擇要測試的試題類型:1加法2減法");

int type=input.nextInt();

switch(type){

case 1:

ss.setOperator("+");

break;

case 2:

ss.setOperator("-");

break;

default:

System.out.println("輸入有誤,退出系統(tǒng)!");

System.exit(0);

}

ss.makeQuestion();

for(int i=0;inumber;i++){

System.out.println("第"+(i+1)+"題:"+ss.getQuestions()[i].getFirstParam()+ss.getQuestions()[i].getOperator()+ss.getQuestions()[i].getSecondParam()+"=?");

int answer=input.nextInt();

if(ss.getQuestions()[i].getAnswer()==answer){

ss.setRightNumber(ss.getRightNumber()+1);

System.out.println("回答正確!");

}

else{

ss.setWrongNumber(ss.getWrongNumber()+1);

System.out.println("回答錯誤!");

}

}

System.out.println("您總共答了"+ss.getQuestionNumber()+"道題,答對"+ss.getRightNumber()+"道,答錯"+ss.getWrongNumber()+"道,最后總分為:"+ss.getRightNumber()*100/ss.getQuestionNumber()+"分");

}

}

java實現(xiàn)算術表達式求值

import java.util.Collections;

import java.util.Stack;

public class Calculator {

private StackString postfixStack = new StackString();//后綴式棧

private StackCharacter opStack = new StackCharacter();//運算符棧

private int [] operatPriority = new int[] {0,3,2,1,-1,1,0,2};//運用運算符ASCII碼-40做索引的運算符優(yōu)先級

public static void main(String[] args) {

System.out.println(5+12*(3+5)/7.0);

Calculator cal = new Calculator();

String s = "5+12*(3+5)/7";

double result = cal.calculate(s);

System.out.println(result);

}

/**

* 按照給定的表達式計算

* @param expression 要計算的表達式例如:5+12*(3+5)/7

* @return

*/

public double calculate(String expression) {

StackString resultStack = new StackString();

prepare(expression);

Collections.reverse(postfixStack);//將后綴式棧反轉(zhuǎn)

String firstValue ,secondValue,currentValue;//參與計算的第一個值,第二個值和算術運算符

while(!postfixStack.isEmpty()) {

currentValue = postfixStack.pop();

if(!isOperator(currentValue.charAt(0))) {//如果不是運算符則存入操作數(shù)棧中

resultStack.push(currentValue);

} else {//如果是運算符則從操作數(shù)棧中取兩個值和該數(shù)值一起參與運算

secondValue = resultStack.pop();

firstValue = resultStack.pop();

String tempResult = calculate(firstValue, secondValue, currentValue.charAt(0));

resultStack.push(tempResult);

}

}

return Double.valueOf(resultStack.pop());

}

/**

* 數(shù)據(jù)準備階段將表達式轉(zhuǎn)換成為后綴式棧

* @param expression

*/

private void prepare(String expression) {

opStack.push(',');//運算符放入棧底元素逗號,此符號優(yōu)先級最低

char[] arr = expression.toCharArray();

int currentIndex = 0;//當前字符的位置

int count = 0;//上次算術運算符到本次算術運算符的字符的長度便于或者之間的數(shù)值

char currentOp ,peekOp;//當前操作符和棧頂操作符

for(int i=0;iarr.length;i++) {

currentOp = arr[i];

if(isOperator(currentOp)) {//如果當前字符是運算符

if(count 0) {

postfixStack.push(new String(arr,currentIndex,count));//取兩個運算符之間的數(shù)字

}

peekOp = opStack.peek();

if(currentOp == ')') {//遇到反括號則將運算符棧中的元素移除到后綴式棧中直到遇到左括號

while(opStack.peek() != '(') {

postfixStack.push(String.valueOf(opStack.pop()));

}

opStack.pop();

} else {

while(currentOp != '(' peekOp != ',' compare(currentOp,peekOp) ) {

postfixStack.push(String.valueOf(opStack.pop()));

peekOp = opStack.peek();

}

opStack.push(currentOp);

}

count = 0;

currentIndex = i+1;

} else {

count++;

}

}

if(count 1 || (count == 1 !isOperator(arr[currentIndex]))) {//最后一個字符不是括號或者其他運算符的則加入后綴式棧中

postfixStack.push(new String(arr,currentIndex,count));

}

while(opStack.peek() != ',') {

postfixStack.push(String.valueOf( opStack.pop()));//將操作符棧中的剩余的元素添加到后綴式棧中

}

}

/**

* 判斷是否為算術符號

* @param c

* @return

*/

private boolean isOperator(char c) {

return c == '+' || c == '-' || c == '*' || c == '/' || c == '(' ||c == ')';

}

/**

* 利用ASCII碼-40做下標去算術符號優(yōu)先級

* @param cur

* @param peek

* @return

*/

public boolean compare(char cur,char peek) {// 如果是peek優(yōu)先級高于cur,返回true,默認都是peek優(yōu)先級要低

boolean result = false;

if(operatPriority[(peek)-40] = operatPriority[(cur) - 40]) {

result = true;

}

return result;

}

/**

* 按照給定的算術運算符做計算

* @param firstValue

* @param secondValue

* @param currentOp

* @return

*/

private String calculate(String firstValue,String secondValue,char currentOp) {

String result = "";

switch(currentOp) {

case '+':

result = String.valueOf(ArithHelper.add(firstValue, secondValue));

break;

case '-':

result = String.valueOf(ArithHelper.sub(firstValue, secondValue));

break;

case '*':

result = String.valueOf(ArithHelper.mul(firstValue, secondValue));

break;

case '/':

result = String.valueOf(ArithHelper.div(firstValue, secondValue));

break;

}

return result;

}

}

public class ArithHelper {

// 默認除法運算精度

private static final int DEF_DIV_SCALE = 16;

// 這個類不能實例化

private ArithHelper() {

}

/**

* 提供精確的加法運算。

*

* @param v1 被加數(shù)

* @param v2 加數(shù)

* @return 兩個參數(shù)的和

*/

public static double add(double v1, double v2) {

java.math.BigDecimal b1 = new java.math.BigDecimal(Double.toString(v1));

java.math.BigDecimal b2 = new java.math.BigDecimal(Double.toString(v2));

return b1.add(b2).doubleValue();

}

public static double add(String v1, String v2) {

java.math.BigDecimal b1 = new java.math.BigDecimal(v1);

java.math.BigDecimal b2 = new java.math.BigDecimal(v2);

return b1.add(b2).doubleValue();

}

/**

* 提供精確的減法運算。

*

* @param v1 被減數(shù)

* @param v2 減數(shù)

* @return 兩個參數(shù)的差

*/

public static double sub(double v1, double v2) {

java.math.BigDecimal b1 = new java.math.BigDecimal(Double.toString(v1));

java.math.BigDecimal b2 = new java.math.BigDecimal(Double.toString(v2));

return b1.subtract(b2).doubleValue();

}

public static double sub(String v1, String v2) {

java.math.BigDecimal b1 = new java.math.BigDecimal(v1);

java.math.BigDecimal b2 = new java.math.BigDecimal(v2);

return b1.subtract(b2).doubleValue();

}

/**

* 提供精確的乘法運算。

*

* @param v1

* 被乘數(shù)

* @param v2

* 乘數(shù)

* @return 兩個參數(shù)的積

*/

public static double mul(double v1, double v2) {

java.math.BigDecimal b1 = new java.math.BigDecimal(Double.toString(v1));

java.math.BigDecimal b2 = new java.math.BigDecimal(Double.toString(v2));

return b1.multiply(b2).doubleValue();

}

public static double mul(String v1, String v2) {

java.math.BigDecimal b1 = new java.math.BigDecimal(v1);

java.math.BigDecimal b2 = new java.math.BigDecimal(v2);

return b1.multiply(b2).doubleValue();

}

/**

* 提供(相對)精確的除法運算,當發(fā)生除不盡的情況時,精確到 小數(shù)點以后10位,以后的數(shù)字四舍五入。

*

* @param v1

* 被除數(shù)

* @param v2

* 除數(shù)

* @return 兩個參數(shù)的商

*/

public static double div(double v1, double v2) {

return div(v1, v2, DEF_DIV_SCALE);

}

public static double div(String v1, String v2) {

java.math.BigDecimal b1 = new java.math.BigDecimal(v1);

java.math.BigDecimal b2 = new java.math.BigDecimal(v2);

return b1.divide(b2, DEF_DIV_SCALE, java.math.BigDecimal.ROUND_HALF_UP).doubleValue();

}

/**

* 提供(相對)精確的除法運算。當發(fā)生除不盡的情況時,由scale參數(shù)指 定精度,以后的數(shù)字四舍五入。

*

* @param v1 被除數(shù)

* @param v2 除數(shù)

* @param scale 表示表示需要精確到小數(shù)點以后幾位。

* @return 兩個參數(shù)的商

*/

public static double div(double v1, double v2, int scale) {

if (scale 0) {

throw new IllegalArgumentException("The scale must be a positive integer or zero");

}

java.math.BigDecimal b1 = new java.math.BigDecimal(Double.toString(v1));

java.math.BigDecimal b2 = new java.math.BigDecimal(Double.toString(v2));

return b1.divide(b2, scale, java.math.BigDecimal.ROUND_HALF_UP).doubleValue();

}

/**

* 提供精確的小數(shù)位四舍五入處理。

*

* @param v 需要四舍五入的數(shù)字

* @param scale 小數(shù)點后保留幾位

* @return 四舍五入后的結(jié)果

*/

public static double round(double v, int scale) {

if (scale 0) {

throw new IllegalArgumentException("The scale must be a positive integer or zero");

}

java.math.BigDecimal b = new java.math.BigDecimal(Double.toString(v));

java.math.BigDecimal one = new java.math.BigDecimal("1");

return b.divide(one, scale, java.math.BigDecimal.ROUND_HALF_UP).doubleValue();

}

public static double round(String v, int scale) {

if (scale 0) {

throw new IllegalArgumentException("The scale must be a positive integer or zero");

}

java.math.BigDecimal b = new java.math.BigDecimal(v);

java.math.BigDecimal one = new java.math.BigDecimal("1");

return b.divide(one, scale, java.math.BigDecimal.ROUND_HALF_UP).doubleValue();

}

}

怎么寫Java測試代碼?

可以用main函數(shù)和JUnit來寫測試代碼。main是最早使用的,但是現(xiàn)在更流行的測試工具是JUnit。

JUnit是一個Java語言的單元測試框架。它由Kent Beck和Erich Gamma建立,逐漸成為源于Kent Beck的sUnit的xUnit家族中最為成功的一個。 JUnit有它自己的JUnit擴展生態(tài)圈。多數(shù)Java的開發(fā)環(huán)境都已經(jīng)集成了JUnit作為單元測試的工具。

下面是一些具體的編寫測試代碼的技巧或較好的實踐方法:

1. 不要用TestCase的構(gòu)造函數(shù)初始化Fixture,而要用setUp()和tearDown()方法。

2. 不要依賴或假定測試運行的順序,因為JUnit利用Vector保存測試方法。所以不同的平臺會按不同的順序從Vector中取出測試方法。

3. 避免編寫有副作用的TestCase。例如:如果隨后的測試依賴于某些特定的交易數(shù)據(jù),就不要提交交易數(shù)據(jù)。簡單的回滾就可以了。

4. 當繼承一個測試類時,記得調(diào)用父類的setUp()和tearDown()方法。

5. 將測試代碼和工作代碼放在一起,一邊同步編譯和更新。(使用Ant中有支持junit的task.)

6. 測試類和測試方法應該有一致的命名方案。如在工作類名前加上test從而形成測試類名。

7. 確保測試與時間無關,不要依賴使用過期的數(shù)據(jù)進行測試。導致在隨后的維護過程中很難重現(xiàn)測試。

8. 如果你編寫的軟件面向國際市場,編寫測試時要考慮國際化的因素。不要僅用母語的Locale進行測試。

9. 盡可能地利用JUnit提供地assert/fail方法以及異常處理的方法,可以使代碼更為簡潔。

10.測試要盡可能地小,執(zhí)行速度快。

11.不要硬性規(guī)定數(shù)據(jù)文件的路徑。

12.利用Junit 的自動異常處理書寫簡潔的測試代碼

事實上在Junit 中使用try-catch 來捕獲異常是沒有必要的,Junit 會自動捕獲異常。那些沒有被捕獲的異常就被當成錯誤處理。

13. 充分利用Junit 的assert/fail 方法

assertSame()用來測試兩個引用是否指向同一個對象

assertEquals()用來測試兩個對象是否相等

14. 確保測試代碼與時間無關

15. 使用文檔生成器做測試文檔。

java 算數(shù)代碼

pselect?user.id,?user.name,?buy.id,?buy.time?from?user?left?outer?join?buy?on?user.id?=?buy.userid?order?by?buy.time?asc?group?by?buy.userid;/pp算法這題題目有點問題,每種獎品的數(shù)量都知道了,難道所有獎品的總數(shù)量會不知道嗎?所以這個count我只能理解為,需要抽出的獎品的數(shù)量,即抽獎幾次。/pp割一下-----------------------------------------------------------------/pp又仔細看了一下題目,發(fā)現(xiàn)最終返回的是一個String,也就是只抽一次獎,具體要根據(jù)每種獎品的數(shù)量不同來計算概率進行抽獎,count的確是所有獎品的數(shù)量,但是我從map把每種獎品的總數(shù)累加,不就是所有獎品數(shù)量了嘛。。。傳個count確實是多此一舉了

public?static?String?draw(MapString,?Integer?map,?int?count)?{

//?產(chǎn)生1-count之間的隨機數(shù)

Integer?random?=?new?Random().nextInt(count)?+?1;

//?map的key,即獎品名稱

SetString?keys?=?map.keySet();

//?map的value值,即每種獎品的數(shù)量

CollectionInteger?values?=?map.values();

//?題目明確說了有3種獎品,獲取前兩種獎品的數(shù)量

Integer?count1?=?(int)?values.toArray()[0];

Integer?count2?=?(int)?values.toArray()[1];

String?name?=?null;//?獎品名稱

if?(random?=?count1)?{//?表示抽中第一種獎品

name?=?(String)?keys.toArray()[0];

}?else?if?(random??count1?+?count2)?{//?表示抽中第三種獎品

name?=?(String)?keys.toArray()[2];

}?else?{//?表示抽中第二中獎品

name?=?(String)?keys.toArray()[1];

}

return?name;

}

網(wǎng)站標題:java算數(shù)測試代碼,java算術測試
當前地址:http://jinyejixie.com/article26/dsedpcg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃移動網(wǎng)站建設、商城網(wǎng)站電子商務、網(wǎng)頁設計公司、網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營
宿松县| 紫云| 洛浦县| 石泉县| 馆陶县| 平南县| 宁武县| 潍坊市| 江陵县| 德安县| 塘沽区| 孝义市| 绿春县| 连城县| 西乌珠穆沁旗| 类乌齐县| 蓬安县| 赤城县| 南澳县| 中超| 六盘水市| 北碚区| 汝阳县| 天祝| 柘荣县| 仙桃市| 冕宁县| 循化| 杂多县| 浮山县| 汽车| 军事| 托克托县| 安图县| 神池县| 方山县| 潞城市| 鞍山市| 石嘴山市| 马公市| 常宁市|