Java程序填空.docx

上传人:b****4 文档编号:3990169 上传时间:2022-11-27 格式:DOCX 页数:31 大小:282.92KB
下载 相关 举报
Java程序填空.docx_第1页
第1页 / 共31页
Java程序填空.docx_第2页
第2页 / 共31页
Java程序填空.docx_第3页
第3页 / 共31页
Java程序填空.docx_第4页
第4页 / 共31页
Java程序填空.docx_第5页
第5页 / 共31页
点击查看更多>>
下载资源
资源描述

Java程序填空.docx

《Java程序填空.docx》由会员分享,可在线阅读,更多相关《Java程序填空.docx(31页珍藏版)》请在冰豆网上搜索。

Java程序填空.docx

Java程序填空

本题的功能是计算二维数组arr[][]={{34,21,45,67,20),{23,10,3,45,76},{22,3,79,56,50}}中的最小值,并输出。

参考答案:

第1处:

j<5或j<=4

第2处:

j=0

第3处:

i++或i+=1或i=i+1

 

本题的功能是通过鼠标确定两个点,然后画两点间的直线。

窗口中有-个按钮“Drawline”,单击该按钮后,它就处于按下状态,然后用鼠标在窗口中单击-下,在单击的地方就会出现-个坐标圆点,用鼠标在另外-个地方单击-下又会出现另外-个圆点,并且此时在两个坐标圆点间画出-条直线,且“Drawline”处于可用状态,再单击这个按钮就可以画另外-条直线。

 

∙参考答案:

第1处:

Point

第2处:

mevt.getPoint

 

下面的程序中,有两个文本框,Input和Output,用户在Input文本框中输入字符串后回车,字符串会在Output文本框中出现。

∙参考答案:

第1处;newActionlistener( )

第2处:

tfl.getText( )

 

本题中定义了长度为20的-维整型数组a,并将数组元素的下标值赋给数组元素,最后打印输出数组中下标为奇数的元素。

publicclassjaval{

publicstaticvoidmain(String[]args){

inta[]=

Inti:

for

a[i]=i

for

i++)

=1:

i=0;i<20;i++){

System.out.print(”a[”+i+”]=”+a[i]+”,“);

}

∙参考答案:

第1处:

newint020]

第2处:

i=0;i<20

第3处:

i%2!

=0

 

本题的功能是计算二维数组arr[][]={{34,21,45,67,20},{23,10,3,45,76},{22,3,79,56,50}}中的最小值,并输出。

 

publicclassjaval{

publicstaticvoidmain(String[]args){

intarr[][]={{34,21,45,67,20),{23,10,3,45, 

76},{22,3,79,56,50}}; 

inti=0:

 

intj=0; 

intmin=arr[0][0]; 

while(i<3){

while(     ){

if(arr[i][j]

min=arr[i][j]; 

j++; 

}

     ; 

     ; 

}

System.out.println("Themin:

"+min); 

}

}

∙参考答案:

第1处:

j<5或j<=4

第2处:

j=0

第3处:

i++或i+=1或i=i+1

.

本题将数组中arr[]={5,6,3,7,9,1}的各个元素按下标的逆序输出。

 

publicclassjaval{

publicstaticvoidmain(String[]args){

intarr[]={5,6,3,7,9,1}; 

         ; 

n=         ; 

while(n>=O){

System.OUt.print(arr[n]+""); 

         ; 

}

}

}

∙参考答案:

第1处:

intn

第2处:

arr.1ength-1

第3处:

n--或n=n-1或n-=l

 

.本题定义了一个方法add(),用于求两个整形数的和。

方法中有两个整形参数a和b,方法体中计算a和b的和sum,并将结果返回。

程序中调用add()方法求整数24和34的和,并将结果打印输出。

 

publicclassjaval{

publicstaticvoidmain(String[]args){

inta=24,b=34; 

System.out.println(add(a,b)); 

}

publicstaticintadd(        ){

         ; 

sum—a+b; 

         ; 

}

∙参考答案:

第1处:

inta,intb

第2处:

intsum

第3处:

returnsum

 

本题将一个整形变量opl的二进制的低3位全部变成1。

 

publicclassjaval{

publicstaticvoidmain(String[]args){

          opl=21; 

intop2=7:

 

          res; 

          ; 

System.out.println(res); 

}

∙参考答案:

第1处:

int 

第2处:

int 

第3处:

res=opl|op2

 

本题的功能是计算l~10之间除了5以外的各个自然数的和。

 

publicclassjaval{

publicstaticvoidmain(String[]args){

inti=1; 

intsum=0; 

while(i<=10){

if(i==5){

       ; 

       ; 

}

       ; 

i++; 

}

System.out.println("sum="+sum); 

}

}

∙参考答案:

第1处:

i=i+1或i++或i+=1

第2处:

continue

第3处:

sum+=i或sum=sum+i

 

本题的功能是获取鼠标在窗口中的位置。

当鼠标移进窗口中,就会实时显示鼠标在窗口中的相对位置,比如显示为"鼠标的当前位置:

X:

Y"(其中,X为横坐标,Y为纵坐标)。

 

importjava.awt.*; 

importjava.awt.event.*; 

importjava.util.*; 

importjavax.swing.*; 

publicclassjava2

{

publicstaticvoidmain(String[]args)

{

MouseFrameframe=newMouseFrame(); 

frame.setDefaultCloseoperation(JFrame.EXIT_

0NCLOSE); 

frame.show(); 

}

}

classMouseFrameextendsJFrame

{

publicMouseFrame()

{

setTitle("java2"); 

setSize(WIDTH,HEIGHT); 

MousePanelpanel=newMousePanel(); 

ContainercontentPane=getContentPane(); 

contentPane.add(panel); 

}

publicstaticfinalintWIDTH=300; 

publicstaticfinalintHEIGHT=200; 

classMousePanelextendsJPanel

{

publicMousePanel()

{

addMouseListener(newMouseHandler()); 

addMouseMotionListener(newMouseMotionHan-

dler()); 

}

publicvoidpaintComponent(Graphicsg)

super.paintComponent(g); 

Stringtext="鼠标指针位置:

"+mousex+":

"

+mousey; 

g.drawString(text,10,10); 

}

privateintmousex,mousey; 

privateclassMouseMotionHandler      {

publicvoidmouseMoved(MouseEventevent)

{

mousex=event.getX(); 

mousey=event.getY(); 

repaint(); 

}

publicvoidmouseDragged(MouseEventevent)

{

mousex=event.getX(); 

mousey=event.getY(); 

repaint(); 

}

}

privateclassMouseHandler     

{

publicvoidmousePressed(MouseEventeveat)

{mousex=event.getX(); 

mousey=event.getY(); 

}

}

}

∙参考答案:

第1处:

implementsMouseMotionListener

第2处:

extendsMouseAdapter

 

本题主窗口中包括一个文本框和一个文本域,在上面的文本框中输入一个整数并按回车键,就会在下面的文本域中显示该整数的平方值;如果在文本框中输入的不是一个整数,将弹出一个警告窗口。

 

tmportjava.awt.event.*:

 

importjava.awt.*; 

importjavax.swing.JOptionPane; 

classDwindowextendsFrameimplementsActionLis-

tener

{TextFieldinputNumber; 

TextAreashow; 

Dwindow(Strings)

{super(s):

 

inputNumber=newTextField(22); 

inputNumber.addActionListener(this); 

show=newTextArea(); 

add(inputNumber,BorderLayout.NORTH); 

add(show,BorderLayout.CENTER); 

setBounds(60,60,300,300);setVisible(true); 

validate(); 

addWindowListener(newWindowAdapter()

{publicvoidwindowClosing(WindowEvente)

{System.exit(0); 

}

}); 

}

publicvoidactionPerformed(ActionEvente)

{booleanboo=false; 

if(e.getSource()==inputNumber)

{Strings=        ; 

chara[]=S.toCharArray(); 

for(inti=0;i

{if(!

(Character.isDigit(a[i])))

boo=true; 

}

if(boo==true)

{JOptionPane.showMessageDialog(this,"您输入 

了非法字符","警告对话框", 

        ); 

inputNumber.setText(null); 

}

elseif(boo==false)

{intnumber=Integer.parseInt(s); 

show.append("\n"+number+"平方:

"+(num-

bet*number)):

 

}

}

}

}

publicclassjava2

{publicstaticvoidmain(Stringargs[])

{newDwindow("java2"); 

}

}

∙参考答案:

第1处:

inputNumber.getText()

第2处:

JOptionPane.WARNING_MESSAGE

本题是一个Applet,它的功能是在窗口上添加12×12个标签,并且横向和纵向标签的颜色为黑白相间。

 

importjava.applet.*; 

importjava..awt.*; 

importjava,.awt.event*; 

pubhcclassjava2

extendsApplet

{GridLayoutgrid; 

pubhcvoidinit()

{grid=newGridLayout(12,12); 

setLayout(grid); 

Label         =newLabel[12][12]; 

for(inti=0;i<12;i++)

{for(intj=0;j<12;j++)

{label[i][j]=        ; 

if((i+j)%2==0)

label[i][j].setBackground(Color.black); 

else

label[i][j].setBackground(color.white); 

add(label[i][j]); 

}

}

}}

∙参考答案:

第1处:

label[][]

第2处:

newlabel()

 

本题中定义了一个树型的通信录,窗El左侧是一个树, 

右侧是一个文本域,单击树的结点,则在右侧文本域中显示 

相关信息,如果单击的是树结点,则显示对应名字的 

信息。

 

importjavax.swing.*; 

importjavax.swing.tree.*; 

importjava.awt.*; 

importjava.awt.event.*; 

importjavax.swing.event.*; 

classMytree2extendsJFrame      

{JTreetree=null;JTextAreatext=newJTextArea

(20,20); 

Mytree2()

{Containercon=getContentPane(); 

DefauhMutableTreeNoderoot=newDefault-

MutableTreeNode("同学通信录"); 

DefaultMutableTreeNodetl=newDefault-

MutableTreeNode("大学同学"); 

DefaultMutableTreeNodet2=newDefault-

MutableTreeNode("研究生同学"); 

DefaultMutableTreeNodetl l=newDefault-

MutatleTreeNode("艳"); 

DefaultMutableTreeNodetl 2=newDefault-

MutableTreeNode("小永"); 

DefaultMutableTreeNodet2 1=newDefauh-

MutableTreeNode("王小小"); 

DefauhMutableTreeNodet2 2=newDefauh-

MutableTreeNode("董小"); 

setTitle("java2"); 

root.add(t1);root.add(t2); 

tl.add(t1_1);tl.add(t1_2);t2.add(t2_1);t2. 

add(t2_2); 

tree=newJTree(root); 

JSerollPanescrollpane=newJScrollPane(text); 

JSplitPanesplitpane=newJSplitPane(JSplitPane. 

HORIZONTALSPLIT, 

true,tree,scrollpane); 

tree.addTreeSeleetionListener(this); 

con.add(splitpane); 

addWindowListener(newWindowAdapter()

{publicvoidwindowClosing(WindowEvente)

{System.exit(0);}}); 

setVisible(true);setBounds(70,80,200,300); 

}

publicvoidvalueChanged(TreeSelectionEvente)

{if(e.getSouree()==tree)

{DefauhMutableTreeNodenode=

(DefaultMutableTreeNode)tree.getLastSelected-

PathComponent(); 

if(node.isLeaf())

(Stringstr=      ; 

if(str.equals("艳"))

(text.setText(str+":

联系:

0411-

4209876");}

elseif(str.equals("小永")) 

{text.setText(str+":

联系:

010-

62789876");} 

elseif(str.equals("王小小"))

{text.setText(str+":

联系:

0430-

63596677");)

elseif(str.equals("董小"))

{text.setText(str+":

联系:

020-

85192789");}

}

else

{text.setText(node.getUserObject().toString

()); 

}

}

}

publicclassjava2

{publicstaticvoidmain(Stringargs[])

{Mytree2win=newMytree2();win.pack();}

}

∙参考答案:

第1处:

implementsTreeSelectionListener

第2处:

node.toString() 

 

本题中,主窗口有一个按钮“打开对话框”和一个文本域,单击按钮“打开对话框”后会弹出一个对话框,对话框上有两个按钮“Yes"和“N0”,单击对话框上的“Yes”和“N0”按钮后返回主窗口,并在右侧文本域中显示刚才所单击的按钮信息。

 

importjava.awt.event.*; 

importjava.awt.*; 

classMyDialog        implementsActionListener

{staticfinalintYES=1,N0=0; 

intmessage=-1;Buttonyes,no; 

MyDialog(Framef.StringS,booleanb)

{super(f,S,b); 

ves=newButton("Yes");yes.addActionListener

(this); 

no=newButton("No"); no.addActionListener

(this)o

setLayout(newFlowLayout()); 

add(yes);add(no); 

setBounds(60,60,100,100); 

addWindowListener(newWindowAdapter()

{publicvoidwindowClosing(WindowEvente)

{message=-1;setVisible(false);)

}); 

}

publicvoidactionPerformed(ActionEvente)

{if(e.getSource()==yes)

{message=YES; 

setVisible(false); 

}

elseif(e.getSource()==no)

{message=NO; 

setVisible(false); 

}

}

publicintgetMessage()

{returnmessage; 

}

}

classDwindowextendsFrameimplementsActionLis-

tener

{TextAreatext;Buttonbutton;MyDialogdialog; 

Dwindow(Strings)

{super(s); 

text=newTextArea(5,22);button=newButton

("打开对话框"); 

button.addActionListener(this); 

setLayout(newFlowLayout()); 

add(button);add(text); 

dialog=newMyDialog(this,"Dialog",true); 

setBounds(60,60,300,300);setVisible(true); 

validate(); 

addWindowListener(newWindowAdapter()

{publicvoidwindowClosing(WindowEvente)

{System.exit(0);}

}); 

}

publicvoidactionPerformed(ActionEvente)

{if(e.getSource()==button)

{         ; 

if(dialog.getMessage()==MyDialog.YES)

{text.append("\n你单击了对话框的yes按 

钮"); 

}

elseif(dialog.getMessage()==MyDialog.NO)

{text.append("\n你单击了对话框的N0按 

钮"); 

}

}

}

}

publicclassjava2

{publicstaticvoidmain(Stringargs[])

{newDwindow("java2"); 

}

}

∙参考答案:

第1处:

extendsDialog

第2处:

dialog.setVisible(true)

 

题中,在下画线上填写代码,指定变量b为字节型,变量f为单精度实型,变量1为64位整型。

 

publicclassjaval{

publicstaticvoidmain(String[-]args){

        b=49; 

        f=8.9f; 

        l=0xfedl; 

System.out.println("b="+b); 

System.out.println("f="+f):

 

System.OUt.println("l="+1); 

}

}

∙参考答案:

第1处:

byte

第2处:

float

第3处:

long

 

本题的功能是读人运行程序时所传入的参数(一个或多个),并将参数依次显示出来,比如运行程序:

javajaval

partlpart2,则打印输出为:

partlpart2。

 

publicclassjaval{

publicstaticvoidmain(String[]args){

inti=0:

 

w

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1