简答题 Java
题号 #2244505

本题的功能是用冒泡法对数组元素arr[]={30,1,-9,70)进行从小到大排列。冒泡法排序是比较相邻的两个元素的大小,然后把小的元素交换到前面。
public class javal{
public static void main(String[]args){
int i,j;
int arr[]={30,1,-9,70);
int n=         ;
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(arr[i]>arr[j]){
int temp=arr[i];
         ;
         ;
}
}
}
for(i=0;i<n;i++)
System.out.print(arr[i]+"");
}
}

进入在线模考

参考答案

暂无

答案解析

第1处:arr.length
第2处:arr[]=arr[j]
第3处:arr[j]=temp
【解析】第1处从下面的循环结构可看出n的值应为数组的大小;第2处和第3处是借助临时变量把小的元素交换到前面。

你可能感兴趣的试题

1 本题的功能是用按钮来控制文字的颜色。窗口中有三个按钮“Yellow”、“Blue”和“Red”,它们分别对应文字标签中文本的颜色为黄色、蓝色和红色,单击任意一个按钮,文字标签中的文本就变成按钮对应的颜色。
import java.awt.*;
import java.awt.event.*;
import javflx.swing.*;
class ButtonPanel extends JPanel implements ActionL-
istener{
public ButtonPanel(){
yellowButton=new J Button("Yellow");
blueButton=new JButton("Blue");
redButton=new JButton("Red");
j1=new JLabel("I am from China!");
add(yellowButton);
add(blueButton);
add(redButton);
add(j1);
yellowButtofl.addActionListener(this);
blueButton.addActionListener(this);
redButton.addActionListener(this);
}
public void actionPerformed(ActionEvent evt){
0bject source=evt.getSource();
Color color=getForeground();
if(source= =yellowButton)color=Color.
yellow;
else if(source= =blueButton)color=Color.
blue;
else if(source= =redButton)color=
Color.red;
         ;
         ;
}
private JButton yellowButton;
private JButton blueButton;
private JButton redButton;
private JLabel jl;
}
class ButtonFrame extends JFrame{
public ButtonFrame(){
setTitle("exam l6");
setSize(300,200);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(O);
}
});
Container contentPane=getContentPane();
contentPane.add(new ButtonPanel());
}
}
public class java2{
public static void main(String[]args){
JFrame frame=new ButtonFrame();
frame.show(); 
}
}
2 本题的功能是监听对于菜单项和工具条的操作。窗口中有一个菜单“Color”和一个工具体,菜单“Color”中有菜单项“Yellow”、“Blue”、“Red”和“Exit”,每个菜单项都有对应的图形,单击前三个颜色菜单项,主窗口就变成对应的颜色,单击“Exit”则退出程序。工具条上有4个按钮,分别为三个颜色按钮和一个退出程序的按钮,单击任意一个颜色按钮,主窗口将变成按钮对应的颜色,单击退出程序按钮,则退出程序。
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swin9.*;
public class java3
{
public static void main(String[]args)
{
ToolBarFrame frame=new ToolBarFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_
ON_CLOSE);
frame.show();
}
}
class ToolBarFrame extends JFrame
{
public ToolBarFrame()
{
setTitle("java3");
setSize(DEFAULT_WIDTH,DEFAUlT_
HElGHT);
Container contentPane=getContentPane();
panel=new JPanel();
contentPane.add(panel,BorderLayout.CEN-
TER);
Action blueAction=new ColorAction("Blue".
new ImageIcon("java3-blue-ball.gif"),Color.
BLUE);
Action yellowAction=new ColorAction("
Yellow",
new Imagelcon("java3-yellow-ball.gif"),Col-
or.YELLOW);
Action redAction=new ColorAction("Red".
new Imagelcon("java3-red-ball.gif"),Color.
RED);
Action exitAction=new
AbstractAction("Exit".new Imagelcon("java3-
exit.gif"))
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
};
exitAction.putValue(Action.SH()RT_DESCRIP-
TIoN,"Exit");
JToolBar bar=new JToolBar();
bar.add(blueAction);
bar.add(yellowAction);
bar.add(redAction);
bar.addSeparator();
bar.add(exitAction);
contentPane.addToolBar(bar,BorderLayout.
NoRTH);
JMenu menu=new JMenu("Color"):
menu.add(yellowAction);
menu.add(blueAction);
menu.add(redAction);
menu.add(exitAction);
JMenuBar menuBar=new JMenuBar():
menuBar.add(menu);
SetJ Menu(menuBar);
}
public static final int DEFAULT_WIDTH=300;
public static final int DEFAULT_HEIGHT
=200;
private JPanel panel;
class ColorAction extends AbstractAction
{
public ColorAction(String name,Icon icon,Color
c)
{
putValue(Action.NAME,name);
putValue(Action.SMALL_ICON,icon);
putValue(Action.SHORT_DESCRIPTION,
name+"background");
putValue("Color",c);
}
public void actionPerformed(ActionEvent evt)
{
Color C=(Color)getValue("Color");
panel.setBackcolor(c);
} 
}
}
找试题 · 找答案 · 找资料 在线模考