Java试题三答案.docx
《Java试题三答案.docx》由会员分享,可在线阅读,更多相关《Java试题三答案.docx(10页珍藏版)》请在冰点文库上搜索。
Java试题三答案
Java试题(三)答案
一、选择题答案
选择第1题
C
选择第2题
D
选择第3题
选择第4题
选择第5题
B
选择第6题
选择第7题
选择第8题
选择第9题
A
选择第10题
选择第11题
选择第12题
二、多项选择题答案
多项选择第1题
AB
多项选择第2题
BD
多项选择第3题
ADE
多项选择第4题
AD
多项选择第5题
BCD
多项选择第6题
多项选择第7题
ABCD
多项选择第8题
多项选择第9题
BC
多项选择第10题
多项选择第11题
AC
三、填空题答案
填空第1题
protected;default;public
填空第2题
Object
填空第3题
java.lang.Character;java.lang.Boolean
填空第4题
Whatapleasure!
IamTom
Howdoyoudo?
填空第5题
this(a)
填空第6题
publicclassMyFrameextendsFrameimplementsRunnable
四、编程题答案
编程第1题
importjava.io.*;
publicclassSelectSort
{
publicstaticvoidmain(Stringargs[])
inta[]={20,10,50,40,30,70,60,80,90,100};
inttemp;
for(inti=0;i for(intj=i+1;j { if(a[i] temp=a[i]; a[i]=a[j]; a[j]=temp; } } for(intk=0;k { System.out.println("a["+k+"]:"+a[k]); } } }编程第2题 importjava.io.*; import.*; publicclassHelloServer { publicstaticvoidmain(Stringargs[])throwsIOException { ServerSocketserver=null; server=newServerSocket(8888); SocketClientSocket=null; ClientSocket=server.accept(); Stringline; BufferedReaderis=newBufferedReader(newInputStreamReader(ClientSocket.getInputStream())); PrintWriteros=newPrintWriter(ClientSocket.getOutputStream()); while(true) { line=is.readLine(); if(line.equals("hello")){ os.println("hello"); os.flush(); } } } }编程第3题 importjava.util.*; importjava.text.*; classThreeTimeThreadextendsThread { publicThreeTimeThread(Stringstr) { super(str); } publicvoidrun() { while(true){ SimpleDateFormatformatter=newSimpleDateFormat("yyyy.MM.ddG'at'hh:mm:ssz"); DatecurrentTime=newDate(); try{ sleep(1000); }catch(Exceptione){} StringdateString=formatter.format(currentTime); System.out.println(getName()+":"+dateString); } } publicstaticvoidmain(Stringargs[])throwsException { newThreeTimeThread("first").start(); newThreeTimeThread("second").start(); newThreeTimeThread("third").start(); } }编程第4题 Calculator.java: importjava.awt.*; importjava.awt.event.*; importjava.lang.*; importjava.applet.*; publicclassCalculatorextendsAppletimplementsActionListener{ PanelbuttonPanel; TextFieldtf;//用于显示输入的数字的文本框 Buttonb0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bDot,bPlus,bSub,bDiv,bMulti,bEqual;//按键 StringonDisplay="";//显示在文本框中的字符串 booleanisDotPressed=false;//按键'.'是否被按下 floatoperand;//通过按键输入的数 floatoperand1,operand2;//operand1和operand2为计算用的操作数 charoperator;//计录所用的操作符 floatresult;//计算结果 inttimes=1;//小数点后有几位的10次方幂 publicvoidinit(){ setLayout(newBorderLayout(5,5)); tf=newTextField(30); add(tf,BorderLayout.NORTH); buttonPanel=newPanel(); buttonPanel.setLayout(newGridLayout(4,4,5,5)); add(buttonPanel,BorderLayout.CENTER); //下面依次把按钮添加上去 b1=newButton("1"); buttonPanel.add(b1); b1.addActionListener(this); b1.setActionCommand("1"); b2=newButton("2"); buttonPanel.add(b2); b2.addActionListener(this); b2.setActionCommand("2"); b3=newButton("3"); buttonPanel.add(b3); b3.addActionListener(this); b3.setActionCommand("3"); bPlus=newButton("+"); buttonPanel.add(bPlus); bPlus.addActionListener(this); bPlus.setActionCommand("+"); b4=newButton("4"); buttonPanel.add(b4); b4.addActionListener(this); b4.setActionCommand("4"); b5=newButton("5"); buttonPanel.add(b5); b5.addActionListener(this); b5.setActionCommand("5"); b6=newButton("6"); buttonPanel.add(b6); b6.addActionListener(this); b6.setActionCommand("6"); bSub=newButton("-"); buttonPanel.add(bSub); bSub.addActionListener(this); bSub.setActionCommand("-"); b7=newButton("7"); buttonPanel.add(b7); b7.addActionListener(this); b7.setActionCommand("7"); b8=newButton("8"); buttonPanel.add(b8); b8.addActionListener(this); b8.setActionCommand("8"); b9=newButton("9"); buttonPanel.add(b9); b9.addActionListener(this); b9.setActionCommand("9"); bMulti=newButton("*"); buttonPanel.add(bMulti); bMulti.addActionListener(this); bMulti.setActionCommand("*"); b0=newButton("0"); buttonPanel.add(b0); b0.addActionListener(this); b0.setActionCommand("0"); bDot=newButton("."); buttonPanel.add(bDot); bDot.addActionListener(this); bDot.setActionCommand("."); bEqual=newButton("="); buttonPanel.add(bEqual); bEqual.addActionListener(this); bEqual.setActionCommand("="); bDiv=newButton("/"); buttonPanel.add(bDiv); bDiv.addActionListener(this); bDiv.setActionCommand("/"); } publicvoidactionPerformed(ActionEvente){ Stringstr=e.getActionCommand(); charb=str.charAt(0); switch(b){ case'0': case'1': case'2': case'3': case'4': case'5': case'6': case'7': case'8': case'9':onDisplay+=b; operand=operand*10+Integer.parseInt(str); if(isDotPressed)times*=10; tf.setText(onDisplay); break; case'.':onDisplay+=b; isDotPressed=true; tf.setText(onDisplay); break; case'+': case'-': case'*': case'/':operator=b; operand1=operand/times; System.out.println(operand1); onDisplay=""; times=1; isDotPressed=false; operand=0; break; case'=':operand2=operand/times; System.out.println(operand2); switch(operator){ case'+':result=operand1+operand2;break; case'-':result=operand1-operand2;break; case'*':result=operand1*operand2;break; case'/':result=operand1/operand2;break; } tf.setText(float.toString(result)); onDisplay=""; times=1; isDotPressed=false; operand=0; break; } } } Calculator.html: 五、程序填空题答案程序填空第1题throwsServerTimedException;thrownewServerTimedException(serverName,portName);
for(intj=i+1;j { if(a[i] temp=a[i]; a[i]=a[j]; a[j]=temp; } } for(intk=0;k { System.out.println("a["+k+"]:"+a[k]); } } }编程第2题 importjava.io.*; import.*; publicclassHelloServer { publicstaticvoidmain(Stringargs[])throwsIOException { ServerSocketserver=null; server=newServerSocket(8888); SocketClientSocket=null; ClientSocket=server.accept(); Stringline; BufferedReaderis=newBufferedReader(newInputStreamReader(ClientSocket.getInputStream())); PrintWriteros=newPrintWriter(ClientSocket.getOutputStream()); while(true) { line=is.readLine(); if(line.equals("hello")){ os.println("hello"); os.flush(); } } } }编程第3题 importjava.util.*; importjava.text.*; classThreeTimeThreadextendsThread { publicThreeTimeThread(Stringstr) { super(str); } publicvoidrun() { while(true){ SimpleDateFormatformatter=newSimpleDateFormat("yyyy.MM.ddG'at'hh:mm:ssz"); DatecurrentTime=newDate(); try{ sleep(1000); }catch(Exceptione){} StringdateString=formatter.format(currentTime); System.out.println(getName()+":"+dateString); } } publicstaticvoidmain(Stringargs[])throwsException { newThreeTimeThread("first").start(); newThreeTimeThread("second").start(); newThreeTimeThread("third").start(); } }编程第4题 Calculator.java: importjava.awt.*; importjava.awt.event.*; importjava.lang.*; importjava.applet.*; publicclassCalculatorextendsAppletimplementsActionListener{ PanelbuttonPanel; TextFieldtf;//用于显示输入的数字的文本框 Buttonb0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bDot,bPlus,bSub,bDiv,bMulti,bEqual;//按键 StringonDisplay="";//显示在文本框中的字符串 booleanisDotPressed=false;//按键'.'是否被按下 floatoperand;//通过按键输入的数 floatoperand1,operand2;//operand1和operand2为计算用的操作数 charoperator;//计录所用的操作符 floatresult;//计算结果 inttimes=1;//小数点后有几位的10次方幂 publicvoidinit(){ setLayout(newBorderLayout(5,5)); tf=newTextField(30); add(tf,BorderLayout.NORTH); buttonPanel=newPanel(); buttonPanel.setLayout(newGridLayout(4,4,5,5)); add(buttonPanel,BorderLayout.CENTER); //下面依次把按钮添加上去 b1=newButton("1"); buttonPanel.add(b1); b1.addActionListener(this); b1.setActionCommand("1"); b2=newButton("2"); buttonPanel.add(b2); b2.addActionListener(this); b2.setActionCommand("2"); b3=newButton("3"); buttonPanel.add(b3); b3.addActionListener(this); b3.setActionCommand("3"); bPlus=newButton("+"); buttonPanel.add(bPlus); bPlus.addActionListener(this); bPlus.setActionCommand("+"); b4=newButton("4"); buttonPanel.add(b4); b4.addActionListener(this); b4.setActionCommand("4"); b5=newButton("5"); buttonPanel.add(b5); b5.addActionListener(this); b5.setActionCommand("5"); b6=newButton("6"); buttonPanel.add(b6); b6.addActionListener(this); b6.setActionCommand("6"); bSub=newButton("-"); buttonPanel.add(bSub); bSub.addActionListener(this); bSub.setActionCommand("-"); b7=newButton("7"); buttonPanel.add(b7); b7.addActionListener(this); b7.setActionCommand("7"); b8=newButton("8"); buttonPanel.add(b8); b8.addActionListener(this); b8.setActionCommand("8"); b9=newButton("9"); buttonPanel.add(b9); b9.addActionListener(this); b9.setActionCommand("9"); bMulti=newButton("*"); buttonPanel.add(bMulti); bMulti.addActionListener(this); bMulti.setActionCommand("*"); b0=newButton("0"); buttonPanel.add(b0); b0.addActionListener(this); b0.setActionCommand("0"); bDot=newButton("."); buttonPanel.add(bDot); bDot.addActionListener(this); bDot.setActionCommand("."); bEqual=newButton("="); buttonPanel.add(bEqual); bEqual.addActionListener(this); bEqual.setActionCommand("="); bDiv=newButton("/"); buttonPanel.add(bDiv); bDiv.addActionListener(this); bDiv.setActionCommand("/"); } publicvoidactionPerformed(ActionEvente){ Stringstr=e.getActionCommand(); charb=str.charAt(0); switch(b){ case'0': case'1': case'2': case'3': case'4': case'5': case'6': case'7': case'8': case'9':onDisplay+=b; operand=operand*10+Integer.parseInt(str); if(isDotPressed)times*=10; tf.setText(onDisplay); break; case'.':onDisplay+=b; isDotPressed=true; tf.setText(onDisplay); break; case'+': case'-': case'*': case'/':operator=b; operand1=operand/times; System.out.println(operand1); onDisplay=""; times=1; isDotPressed=false; operand=0; break; case'=':operand2=operand/times; System.out.println(operand2); switch(operator){ case'+':result=operand1+operand2;break; case'-':result=operand1-operand2;break; case'*':result=operand1*operand2;break; case'/':result=operand1/operand2;break; } tf.setText(float.toString(result)); onDisplay=""; times=1; isDotPressed=false; operand=0; break; } } } Calculator.html: 五、程序填空题答案程序填空第1题throwsServerTimedException;thrownewServerTimedException(serverName,portName);
if(a[i] temp=a[i]; a[i]=a[j]; a[j]=temp; } } for(intk=0;k { System.out.println("a["+k+"]:"+a[k]); } } }编程第2题 importjava.io.*; import.*; publicclassHelloServer { publicstaticvoidmain(Stringargs[])throwsIOException { ServerSocketserver=null; server=newServerSocket(8888); SocketClientSocket=null; ClientSocket=server.accept(); Stringline; BufferedReaderis=newBufferedReader(newInputStreamReader(ClientSocket.getInputStream())); PrintWriteros=newPrintWriter(ClientSocket.getOutputStream()); while(true) { line=is.readLine(); if(line.equals("hello")){ os.println("hello"); os.flush(); } } } }编程第3题 importjava.util.*; importjava.text.*; classThreeTimeThreadextendsThread { publicThreeTimeThread(Stringstr) { super(str); } publicvoidrun() { while(true){ SimpleDateFormatformatter=newSimpleDateFormat("yyyy.MM.ddG'at'hh:mm:ssz"); DatecurrentTime=newDate(); try{ sleep(1000); }catch(Exceptione){} StringdateString=formatter.format(currentTime); System.out.println(getName()+":"+dateString); } } publicstaticvoidmain(Stringargs[])throwsException { newThreeTimeThread("first").start(); newThreeTimeThread("second").start(); newThreeTimeThread("third").start(); } }编程第4题 Calculator.java: importjava.awt.*; importjava.awt.event.*; importjava.lang.*; importjava.applet.*; publicclassCalculatorextendsAppletimplementsActionListener{ PanelbuttonPanel; TextFieldtf;//用于显示输入的数字的文本框 Buttonb0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bDot,bPlus,bSub,bDiv,bMulti,bEqual;//按键 StringonDisplay="";//显示在文本框中的字符串 booleanisDotPressed=false;//按键'.'是否被按下 floatoperand;//通过按键输入的数 floatoperand1,operand2;//operand1和operand2为计算用的操作数 charoperator;//计录所用的操作符 floatresult;//计算结果 inttimes=1;//小数点后有几位的10次方幂 publicvoidinit(){ setLayout(newBorderLayout(5,5)); tf=newTextField(30); add(tf,BorderLayout.NORTH); buttonPanel=newPanel(); buttonPanel.setLayout(newGridLayout(4,4,5,5)); add(buttonPanel,BorderLayout.CENTER); //下面依次把按钮添加上去 b1=newButton("1"); buttonPanel.add(b1); b1.addActionListener(this); b1.setActionCommand("1"); b2=newButton("2"); buttonPanel.add(b2); b2.addActionListener(this); b2.setActionCommand("2"); b3=newButton("3"); buttonPanel.add(b3); b3.addActionListener(this); b3.setActionCommand("3"); bPlus=newButton("+"); buttonPanel.add(bPlus); bPlus.addActionListener(this); bPlus.setActionCommand("+"); b4=newButton("4"); buttonPanel.add(b4); b4.addActionListener(this); b4.setActionCommand("4"); b5=newButton("5"); buttonPanel.add(b5); b5.addActionListener(this); b5.setActionCommand("5"); b6=newButton("6"); buttonPanel.add(b6); b6.addActionListener(this); b6.setActionCommand("6"); bSub=newButton("-"); buttonPanel.add(bSub); bSub.addActionListener(this); bSub.setActionCommand("-"); b7=newButton("7"); buttonPanel.add(b7); b7.addActionListener(this); b7.setActionCommand("7"); b8=newButton("8"); buttonPanel.add(b8); b8.addActionListener(this); b8.setActionCommand("8"); b9=newButton("9"); buttonPanel.add(b9); b9.addActionListener(this); b9.setActionCommand("9"); bMulti=newButton("*"); buttonPanel.add(bMulti); bMulti.addActionListener(this); bMulti.setActionCommand("*"); b0=newButton("0"); buttonPanel.add(b0); b0.addActionListener(this); b0.setActionCommand("0"); bDot=newButton("."); buttonPanel.add(bDot); bDot.addActionListener(this); bDot.setActionCommand("."); bEqual=newButton("="); buttonPanel.add(bEqual); bEqual.addActionListener(this); bEqual.setActionCommand("="); bDiv=newButton("/"); buttonPanel.add(bDiv); bDiv.addActionListener(this); bDiv.setActionCommand("/"); } publicvoidactionPerformed(ActionEvente){ Stringstr=e.getActionCommand(); charb=str.charAt(0); switch(b){ case'0': case'1': case'2': case'3': case'4': case'5': case'6': case'7': case'8': case'9':onDisplay+=b; operand=operand*10+Integer.parseInt(str); if(isDotPressed)times*=10; tf.setText(onDisplay); break; case'.':onDisplay+=b; isDotPressed=true; tf.setText(onDisplay); break; case'+': case'-': case'*': case'/':operator=b; operand1=operand/times; System.out.println(operand1); onDisplay=""; times=1; isDotPressed=false; operand=0; break; case'=':operand2=operand/times; System.out.println(operand2); switch(operator){ case'+':result=operand1+operand2;break; case'-':result=operand1-operand2;break; case'*':result=operand1*operand2;break; case'/':result=operand1/operand2;break; } tf.setText(float.toString(result)); onDisplay=""; times=1; isDotPressed=false; operand=0; break; } } } Calculator.html: 五、程序填空题答案程序填空第1题throwsServerTimedException;thrownewServerTimedException(serverName,portName);
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
for(intk=0;k { System.out.println("a["+k+"]:"+a[k]); } } }编程第2题 importjava.io.*; import.*; publicclassHelloServer { publicstaticvoidmain(Stringargs[])throwsIOException { ServerSocketserver=null; server=newServerSocket(8888); SocketClientSocket=null; ClientSocket=server.accept(); Stringline; BufferedReaderis=newBufferedReader(newInputStreamReader(ClientSocket.getInputStream())); PrintWriteros=newPrintWriter(ClientSocket.getOutputStream()); while(true) { line=is.readLine(); if(line.equals("hello")){ os.println("hello"); os.flush(); } } } }编程第3题 importjava.util.*; importjava.text.*; classThreeTimeThreadextendsThread { publicThreeTimeThread(Stringstr) { super(str); } publicvoidrun() { while(true){ SimpleDateFormatformatter=newSimpleDateFormat("yyyy.MM.ddG'at'hh:mm:ssz"); DatecurrentTime=newDate(); try{ sleep(1000); }catch(Exceptione){} StringdateString=formatter.format(currentTime); System.out.println(getName()+":"+dateString); } } publicstaticvoidmain(Stringargs[])throwsException { newThreeTimeThread("first").start(); newThreeTimeThread("second").start(); newThreeTimeThread("third").start(); } }编程第4题 Calculator.java: importjava.awt.*; importjava.awt.event.*; importjava.lang.*; importjava.applet.*; publicclassCalculatorextendsAppletimplementsActionListener{ PanelbuttonPanel; TextFieldtf;//用于显示输入的数字的文本框 Buttonb0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bDot,bPlus,bSub,bDiv,bMulti,bEqual;//按键 StringonDisplay="";//显示在文本框中的字符串 booleanisDotPressed=false;//按键'.'是否被按下 floatoperand;//通过按键输入的数 floatoperand1,operand2;//operand1和operand2为计算用的操作数 charoperator;//计录所用的操作符 floatresult;//计算结果 inttimes=1;//小数点后有几位的10次方幂 publicvoidinit(){ setLayout(newBorderLayout(5,5)); tf=newTextField(30); add(tf,BorderLayout.NORTH); buttonPanel=newPanel(); buttonPanel.setLayout(newGridLayout(4,4,5,5)); add(buttonPanel,BorderLayout.CENTER); //下面依次把按钮添加上去 b1=newButton("1"); buttonPanel.add(b1); b1.addActionListener(this); b1.setActionCommand("1"); b2=newButton("2"); buttonPanel.add(b2); b2.addActionListener(this); b2.setActionCommand("2"); b3=newButton("3"); buttonPanel.add(b3); b3.addActionListener(this); b3.setActionCommand("3"); bPlus=newButton("+"); buttonPanel.add(bPlus); bPlus.addActionListener(this); bPlus.setActionCommand("+"); b4=newButton("4"); buttonPanel.add(b4); b4.addActionListener(this); b4.setActionCommand("4"); b5=newButton("5"); buttonPanel.add(b5); b5.addActionListener(this); b5.setActionCommand("5"); b6=newButton("6"); buttonPanel.add(b6); b6.addActionListener(this); b6.setActionCommand("6"); bSub=newButton("-"); buttonPanel.add(bSub); bSub.addActionListener(this); bSub.setActionCommand("-"); b7=newButton("7"); buttonPanel.add(b7); b7.addActionListener(this); b7.setActionCommand("7"); b8=newButton("8"); buttonPanel.add(b8); b8.addActionListener(this); b8.setActionCommand("8"); b9=newButton("9"); buttonPanel.add(b9); b9.addActionListener(this); b9.setActionCommand("9"); bMulti=newButton("*"); buttonPanel.add(bMulti); bMulti.addActionListener(this); bMulti.setActionCommand("*"); b0=newButton("0"); buttonPanel.add(b0); b0.addActionListener(this); b0.setActionCommand("0"); bDot=newButton("."); buttonPanel.add(bDot); bDot.addActionListener(this); bDot.setActionCommand("."); bEqual=newButton("="); buttonPanel.add(bEqual); bEqual.addActionListener(this); bEqual.setActionCommand("="); bDiv=newButton("/"); buttonPanel.add(bDiv); bDiv.addActionListener(this); bDiv.setActionCommand("/"); } publicvoidactionPerformed(ActionEvente){ Stringstr=e.getActionCommand(); charb=str.charAt(0); switch(b){ case'0': case'1': case'2': case'3': case'4': case'5': case'6': case'7': case'8': case'9':onDisplay+=b; operand=operand*10+Integer.parseInt(str); if(isDotPressed)times*=10; tf.setText(onDisplay); break; case'.':onDisplay+=b; isDotPressed=true; tf.setText(onDisplay); break; case'+': case'-': case'*': case'/':operator=b; operand1=operand/times; System.out.println(operand1); onDisplay=""; times=1; isDotPressed=false; operand=0; break; case'=':operand2=operand/times; System.out.println(operand2); switch(operator){ case'+':result=operand1+operand2;break; case'-':result=operand1-operand2;break; case'*':result=operand1*operand2;break; case'/':result=operand1/operand2;break; } tf.setText(float.toString(result)); onDisplay=""; times=1; isDotPressed=false; operand=0; break; } } } Calculator.html: 五、程序填空题答案程序填空第1题throwsServerTimedException;thrownewServerTimedException(serverName,portName);
System.out.println("a["+k+"]:
"+a[k]);
编程第2题
import.*;
publicclassHelloServer
publicstaticvoidmain(Stringargs[])throwsIOException
ServerSocketserver=null;
server=newServerSocket(8888);
SocketClientSocket=null;
ClientSocket=server.accept();
Stringline;
BufferedReaderis=newBufferedReader(newInputStreamReader(ClientSocket.getInputStream()));
PrintWriteros=newPrintWriter(ClientSocket.getOutputStream());
while(true)
line=is.readLine();
if(line.equals("hello")){
os.println("hello");
os.flush();
编程第3题
importjava.util.*;
importjava.text.*;
classThreeTimeThreadextendsThread
publicThreeTimeThread(Stringstr)
super(str);
publicvoidrun()
while(true){
SimpleDateFormatformatter=newSimpleDateFormat("yyyy.MM.ddG'at'hh:
mm:
ssz");
DatecurrentTime=newDate();
try{
sleep(1000);
}catch(Exceptione){}
StringdateString=formatter.format(currentTime);
System.out.println(getName()+":
"+dateString);
publicstaticvoidmain(Stringargs[])throwsException
newThreeTimeThread("first").start();
newThreeTimeThread("second").start();
newThreeTimeThread("third").start();
编程第4题
Calculator.java:
importjava.awt.*;
importjava.awt.event.*;
importjava.lang.*;
importjava.applet.*;
publicclassCalculatorextendsAppletimplementsActionListener{
PanelbuttonPanel;
TextFieldtf;//用于显示输入的数字的文本框
Buttonb0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bDot,bPlus,bSub,bDiv,bMulti,bEqual;//按键
StringonDisplay="";//显示在文本框中的字符串
booleanisDotPressed=false;//按键'.'是否被按下
floatoperand;//通过按键输入的数
floatoperand1,operand2;//operand1和operand2为计算用的操作数
charoperator;//计录所用的操作符
floatresult;//计算结果
inttimes=1;//小数点后有几位的10次方幂
publicvoidinit(){
setLayout(newBorderLayout(5,5));
tf=newTextField(30);
add(tf,BorderLayout.NORTH);
buttonPanel=newPanel();
buttonPanel.setLayout(newGridLayout(4,4,5,5));
add(buttonPanel,BorderLayout.CENTER);
//下面依次把按钮添加上去
b1=newButton("1");
buttonPanel.add(b1);
b1.addActionListener(this);
b1.setActionCommand("1");
b2=newButton("2");
buttonPanel.add(b2);
b2.addActionListener(this);
b2.setActionCommand("2");
b3=newButton("3");
buttonPanel.add(b3);
b3.addActionListener(this);
b3.setActionCommand("3");
bPlus=newButton("+");
buttonPanel.add(bPlus);
bPlus.addActionListener(this);
bPlus.setActionCommand("+");
b4=newButton("4");
buttonPanel.add(b4);
b4.addActionListener(this);
b4.setActionCommand("4");
b5=newButton("5");
buttonPanel.add(b5);
b5.addActionListener(this);
b5.setActionCommand("5");
b6=newButton("6");
buttonPanel.add(b6);
b6.addActionListener(this);
b6.setActionCommand("6");
bSub=newButton("-");
buttonPanel.add(bSub);
bSub.addActionListener(this);
bSub.setActionCommand("-");
b7=newButton("7");
buttonPanel.add(b7);
b7.addActionListener(this);
b7.setActionCommand("7");
b8=newButton("8");
buttonPanel.add(b8);
b8.addActionListener(this);
b8.setActionCommand("8");
b9=newButton("9");
buttonPanel.add(b9);
b9.addActionListener(this);
b9.setActionCommand("9");
bMulti=newButton("*");
buttonPanel.add(bMulti);
bMulti.addActionListener(this);
bMulti.setActionCommand("*");
b0=newButton("0");
buttonPanel.add(b0);
b0.addActionListener(this);
b0.setActionCommand("0");
bDot=newButton(".");
buttonPanel.add(bDot);
bDot.addActionListener(this);
bDot.setActionCommand(".");
bEqual=newButton("=");
buttonPanel.add(bEqual);
bEqual.addActionListener(this);
bEqual.setActionCommand("=");
bDiv=newButton("/");
buttonPanel.add(bDiv);
bDiv.addActionListener(this);
bDiv.setActionCommand("/");
publicvoidactionPerformed(ActionEvente){
Stringstr=e.getActionCommand();
charb=str.charAt(0);
switch(b){
case'0':
case'1':
case'2':
case'3':
case'4':
case'5':
case'6':
case'7':
case'8':
case'9':
onDisplay+=b;
operand=operand*10+Integer.parseInt(str);
if(isDotPressed)times*=10;
tf.setText(onDisplay);
break;
case'.':
isDotPressed=true;
case'+':
case'-':
case'*':
case'/':
operator=b;
operand1=operand/times;
System.out.println(operand1);
onDisplay="";
times=1;
isDotPressed=false;
operand=0;
case'=':
operand2=operand/times;
System.out.println(operand2);
switch(operator){
result=operand1+operand2;break;
result=operand1-operand2;break;
result=operand1*operand2;break;
result=operand1/operand2;break;
tf.setText(float.toString(result));
Calculator.html:
五、程序填空题答案
程序填空第1题
throwsServerTimedException;
thrownewServerTimedException(serverName,portName);
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2