日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java 四则运算_四则运算————javaweb版

發(fā)布時(shí)間:2024/3/12 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 四则运算_四则运算————javaweb版 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.設(shè)計(jì)思路:

定義一個(gè)類arithmetic,在該類中的定義相關(guān)成員,隨機(jī)產(chǎn)生的題目以及答案用數(shù)組承接,在第一個(gè)jsp里面用戶輸入題目數(shù)量以及設(shè)置做題時(shí)間,將這兩個(gè)數(shù)傳到第二個(gè)jsp頁(yè)面,在此頁(yè)面定義類對(duì)象,調(diào)用相關(guān)類函數(shù),進(jìn)行出題:

最后將算式的數(shù)組和答案的數(shù)組以及用戶輸入的值傳到第三個(gè)jsp頁(yè)面,進(jìn)行答案的校對(duì)即可。

源代碼:

arithmetic.java:

package com.jaovo.msg.model;

public class arithmetic {

public int []answer;//答案

public int shumu;//出題數(shù)目

public String []suanshi;//算式

public void setsuanshi(String []suanshi)

{

this.suanshi=suanshi;

}

public String [] biaodashi(int n)

{

shumu=n;

answer=new int[n];

int a,b,c,d1 = 0,d,d2=0;

int []mixture=new int[2];

String []biaodashi=new String[n];

for(int i=0;i

{

a=(int)(Math.random()*100)+1;//1-100

b=(int)(Math.random()*100)+1;

c=(int)(Math.random()*5)+1;//隨機(jī)生成一個(gè)1-5的整數(shù),4表示加法,1表示減法,2表示乘法,3表示除法,5表示混合

if(c==5)//混合運(yùn)算

{

do

{

for(int m=0;m<2;m++)

{

mixture[m]=(int)(Math.random()*2);//0-1

}//控制運(yùn)算符

a=(int)(Math.random()*100)+1;

b=(int)(Math.random()*100)+1;

d=(int)(Math.random()*100)+1;//生成三個(gè)數(shù)

if(mixture[0]==0&&mixture[1]==0)

{

biaodashi[i]=a+"+"+b+"+"+d+" = ";

d1=a+b+d;

}

if(mixture[0]==1&&mixture[1]==1)

{

biaodashi[i]=a+"-"+b+"-"+d+" = ";

d2=a-b;

d1=a-b-d;

}

if(mixture[0]==0&&mixture[1]==1)

{

biaodashi[i]=a+"+"+b+"-"+d+" = ";

d1=a+b-d;

}

if(mixture[0]==1&&mixture[1]==0)

{

biaodashi[i]=a+"-"+b+"+"+d+" = ";

d2=a-b;

d1=a-b+d;

}

}while(d2<0||d1<0);

answer[i]=d1;

}

if(c==4)//單加法

{

d1=a+b;

biaodashi[i]=a+"+"+b+" = ";

while(d1>100)

{

a=(int)(Math.random()*100)+1;

b=(int)(Math.random()*100)+1;//1-100 包括1和100 不加1 表示0-99

d1=a+b;

}

biaodashi[i]=a+"+"+b+" = ";

answer[i]=d1;

System.out.print(a+"+"+b+"= ");

}

if(c==1)//單減法

{

d1=a-b;

while(d1<0)

{

a=(int)(Math.random()*100)+1;

b=(int)(Math.random()*100)+1;

d1=a-b;

}

biaodashi[i]=a+"-"+b+" = ";

answer[i]=d1;

System.out.print(a+"-"+b+"= ");

}

if(c==2)//乘法

{

a=(int)(Math.random()*10);//0-9

b=(int)(Math.random()*10);//1-100 包括1和100 不加1 表示0-99

d1=a*b;

while(a<1||b<1||d1>81)

{

a=(int)(Math.random()*10);//0-9

b=(int)(Math.random()*10);//1-100 包括1和100 不加1 表示0-99

}

d1=a*b;

biaodashi[i]=a+"*"+b+" = ";

answer[i]=d1;

System.out.print(a+"*"+b+"= ");

}

if(c==3)//除法

{

d1=a/b;

while(a%b!=0||a/b>9||(a<=81&&b>=10)||(a>9&&a==b)||(a>81))

{

a=(int)(Math.random()*100)+1;

b=(int)(Math.random()*100)+1;//1-100 包括1和100 不加1 表示0-99

}

d1=a/b;

biaodashi[i]=a+"÷"+b+" = ";

answer[i]=d1;

System.out.print(a+"÷"+b+"= ");

}

//查重

for(int k=i-1;k>=0;k--)

{

while(biaodashi[i].equals(biaodashi[k]))

{

a=(int)(Math.random()*100)+1;//1-100

b=(int)(Math.random()*100)+1;

c=(int)(Math.random()*5)+1;//隨機(jī)生成一個(gè)1-5的整數(shù),4表示加法,1表示減法,2表示乘法,3表示除法,5表示混合

if(c==5)

{

do//混合運(yùn)算

{

for(int m=0;m<2;m++)

{

mixture[m]=(int)(Math.random()*2);//0-1

}//控制運(yùn)算符

a=(int)(Math.random()*100)+1;

b=(int)(Math.random()*100)+1;

d=(int)(Math.random()*100)+1;//生成三個(gè)數(shù)

if(mixture[0]==0&&mixture[1]==0)

{

biaodashi[i]=a+"+"+b+"+"+d+" = ";

d1=a+b+d;

}

if(mixture[0]==1&&mixture[1]==1)

{

biaodashi[i]=a+"-"+b+"-"+d+" = ";

d2=a-b;

d1=a-b-d;

}

if(mixture[0]==0&&mixture[1]==1)

{

biaodashi[i]=a+"+"+b+"-"+d+" = ";

d1=a+b-d;

}

if(mixture[0]==1&&mixture[1]==0)

{

biaodashi[i]=a+"-"+b+"+"+d+" = ";

d2=a-b;

d1=a-b+d;

}

}while(d2<0||d1<0);

answer[i]=d1;

}

if(c==4)

{

d1=a+b;

biaodashi[i]=a+"+"+b+" = ";

while(d1>100)

{

a=(int)(Math.random()*100)+1;

b=(int)(Math.random()*100)+1;//1-100 包括1和100 不加1 表示0-99

d1=a+b;

}

biaodashi[i]=a+"+"+b+" = ";

answer[i]=d1;

System.out.print(a+"+"+b+"= ");

}

if(c==1)

{

d1=a-b;

while(d1<0)

{

a=(int)(Math.random()*100)+1;

b=(int)(Math.random()*100)+1;

d1=a-b;

}

biaodashi[i]=a+"-"+b+" = ";

answer[i]=d1;

System.out.print(a+"-"+b+"= ");

}

if(c==2)

{

a=(int)(Math.random()*10);//0-9

b=(int)(Math.random()*10);//1-100 包括1和100 不加1 表示0-99

d1=a*b;

while(a<1||b<1||d1>81)

{

a=(int)(Math.random()*10);//0-9

b=(int)(Math.random()*10);//1-100 包括1和100 不加1 表示0-99

}

d1=a*b;

biaodashi[i]=a+"*"+b+" = ";

answer[i]=d1;

System.out.print(a+"*"+b+"= ");

}

if(c==3)

{

while(a%b!=0)

{

a=(int)(Math.random()*100)+1;

b=(int)(Math.random()*100)+1;//1-100 包括1和100 不加1 表示0-99

}

d1=a/b;

biaodashi[i]=a+"÷"+b+" = ";

answer[i]=d1;

System.out.print(a+"÷"+b+"= ");

}

}

}

}

return biaodashi;

}

}

chutijiemian.jsp:

pageEncoding="UTF-8"%>

出題數(shù)目

WELCOME

你想做幾道題來著?
設(shè)置時(shí)間:

chuti.jsp:

pageEncoding="UTF-8"%>

出題頁(yè)

//接收客戶端傳遞過來的參數(shù)

request.setCharacterEncoding("UTF-8");

String time = request.getParameter("usertime");//接收時(shí)間

int time1=0;

int x=1;

for(int m=0;m

{

time1+=(time.charAt(time.length()-m-1)-'0')*x;

x*=10;

}//字符串類型的數(shù)字轉(zhuǎn)換為整型 成為參數(shù)

%>

var c=1;

var t;

var num1=

function timeCount()

{

document.getElementById("txt").innerHTML=num1-c;

c=c+1;

t=setTimeout("timeCount()",1000);

if(num1==c-1)

{

clearTimeout(t);

alert("時(shí)間到了!");

load();

}

}

function load(){

document.getElementById("anniu").click();

}

window.onload =function(){

timeCount();//onload 事件會(huì)在頁(yè)面或圖像加載完成后立即發(fā)生。

}

開始答題

倒計(jì)時(shí):

//接收客戶端傳遞過來的參數(shù)

request.setCharacterEncoding("UTF-8");

String num = request.getParameter("username");//接收出題的數(shù)目

int num1=0;

x=1;

for(int m=0;m

{

num1+=(num.charAt(num.length()-m-1)-'0')*x;

x*=10;

}//字符串類型的數(shù)字轉(zhuǎn)換為整型 成為參數(shù)

arithmetic demo=new arithmetic();//定義對(duì)象

String []biaodashi1=new String[num1];

biaodashi1=demo.biaodashi(num1);//接收算式

demo.setsuanshi(biaodashi1);//調(diào)用函數(shù) 給數(shù)據(jù)成員算式賦值 以便用于傳遞

for(int i=0;i

{

out.println(biaodashi1[i]);//輸出表達(dá)式

%>

out.println("
");

out.println("
");//換行

}

session.setAttribute("jieshou",demo);//用于下一個(gè)界面的接收本界面的這個(gè)類的全部?jī)?nèi)容result 所以定義的對(duì)象

%>

提交

Result.jsp:

pageEncoding="UTF-8"%>

出題

正確答案

//接收客戶端傳遞過來的參數(shù)

arithmetic newdemo=new arithmetic();

newdemo=(arithmetic)session.getAttribute("jieshou");//用于接收CHUti界面?zhèn)鬟^來的數(shù) (對(duì)象)

String []yoursolution=new String[newdemo.shumu];//接收傳過來的文本框的答案

int sumright=0,sumerror=0,empty=0;

for(int i=0;i

{

request.setCharacterEncoding("UTF-8");

out.print(newdemo.suanshi[i]);//正確的算式

yoursolution[i] = request.getParameter("result["+i+"]");//你的答案

out.println(yoursolution[i]);

%>

out.println("正確答案是: ");

out.println(newdemo.answer[i]);//正確的答案

%>

int num1=0;

int x=1;

for(int m=0;m

{

num1+=(yoursolution[i].charAt(yoursolution[i].length()-m-1)-'0')*x;

x*=10;

}//字符串類型的數(shù)字轉(zhuǎn)換為整型 用于和正確答案比較 因?yàn)閺某鲱}界面接受的答案是字符串類型

if(yoursolution[i].equals(""))

{

out.println("你沒有回答哦!");

empty++;

}

else if(num1==newdemo.answer[i])

{

sumright++;

out.println("恭喜你!回答正確!");

}

else

{

sumerror++;

out.println("回答錯(cuò)誤,再接再厲!");

}

out.println("
");//換行

}

out.println("回答正確了"+sumright+"道題!");

out.println("
");//換行

out.println("回答錯(cuò)誤了"+sumerror+"道題!");

out.println("
");//換行

out.println("沒有回答"+empty+"道題!");

out.println("
");//換行

%>

退出

運(yùn)行結(jié)果:

已接觸Javawe快一個(gè)月了,因?yàn)槭莿倓偨佑|這個(gè)網(wǎng)頁(yè)吧,所以會(huì)的很少,不會(huì)的很多。對(duì)于Javaweb,我感覺是個(gè)很神奇的,參數(shù)的傳遞,自動(dòng)調(diào)用,自動(dòng)啟動(dòng)等等,這些都是沒見過的,當(dāng)然還要進(jìn)一步理解!寫的慢的原因,對(duì)Javaweb不是很了解,只能一步一步來

時(shí)間記錄日志:

學(xué)生:馬佳慧??????????????????????? ????????????????????日期:2017/12/5

教師:王建民??????????????????????????????????????????? 課程:軟件工程概論

日期\時(shí)間

開始時(shí)間

結(jié)束時(shí)間

中斷時(shí)間

凈時(shí)間

活動(dòng)

備注

12/1

9:00

11:30

10:30

120分鐘

自習(xí),練習(xí)

課間

14:00

16:30

15:30

80分鐘

練習(xí)

中場(chǎng)休息

12/2

9:30

11:00

10:30

80分鐘

聽課,練習(xí)

中場(chǎng)休息

14:30

18:00

16:00

200分鐘

上課

中場(chǎng)休息

12/3

8:00

11:30

10:00

150分鐘

寫作業(yè),練習(xí),提交作業(yè)

寫 四則

12/4

19:00

22:00

20:30

150分鐘

練習(xí),自習(xí)

課間

12/5

8:00

16:50

8:50/9:50/12:00

180分鐘

改錯(cuò),補(bǔ)充,寫總結(jié)上課

課間

總結(jié)

以上是生活随笔為你收集整理的java 四则运算_四则运算————javaweb版的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。