导航菜单
首页 >  » 正文

Java试题 JAVA基础测试题

Java试题

public class Main{

public static void main(String[] args) {
//test
float[] f = {4.2f, 3.3f, 4.1f};
System.out.println("SUM:" sum(f));
System.out.println("AVG:" avg(f));
System.out.println("MIN:" min(f));
}

public static float avg(float[] arr) {
return sum(arr)/arr.length;
}

public static float sum(float[] arr) {
float total = 0;
for(int i = 0;i<arr.length;i ) {
total = arr[i];
}
return total;
}

public static float min(float[] arr) {
float min = arr[0];
for(int i = 0;i<arr.length;i ) {
if(min>arr[i])
min = arr[i];
}
return min;
}
}

JAVA基础测试题

1 跟java运行的机制有关,java先加载静态域,static int x=10; ,然后静态块static { x =5;},static { x/=3;}(虽然位置在后但先运行) ,然后静态方法static void main
所以x=10,x=15,x=5.
2 A因为int [ ] x = new int[25],数组建立后,每个元素默认值为0;x[24]=0;x[25] 下标越界,x[0]=0;
3 switch(i)不接受long型,int或者枚举型可以。i=Integer.parseInt(args[0]);虽然这样每课时i还是long型的。
4 c,没什么好说的,就是这么规定的!

java试题,十万火急~100分

实现程序Exam01答案:

import javax.swing.*;
import java.awt.*;
import java.io.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Test extends JFrame {
public Test(){
setTitle("考核题目一");
setBounds(0, 0, 400, 200);
JPanel panelnorth=new JPanel();
JPanel panelcentor=new JPanel();

JLabel labelorg=new JLabel("源文件");//构造图形控件
final JTextField textfileorg=new JTextField("c:\a.txt",10);
JLabel labelres=new JLabel("目标文件");
final JTextField textfileres=new JTextField("c:\b.txt",10);
panelnorth.setLayout(new FlowLayout());
panelnorth.add(labelorg);
panelnorth.add(textfileorg);
panelnorth.add(labelres);
panelnorth.add(textfileres);

final JTextArea textarea=new JTextArea("打开文件内容显示在这里",5,5);
JScrollPane scorllpane=new JScrollPane(textarea);//建立滚动的文本域,注意JScrollPane类申请的时候,及要写入控件

panelcentor.setLayout(new FlowLayout());
JButton buttonopen=new JButton("打开");
buttonopen.addActionListener(new ActionListener() {//对按钮进行触发时间
public void actionPerformed(ActionEvent e) {
try {//读取文件,并且写入到文本域
FileReader filereader = new FileReader(textfileorg.getText());
BufferedReader reader=new BufferedReader(filereader);
StringBuffer buffer=new StringBuffer("");
String str=null;
while((str=reader.readLine())!=null ){
buffer.append(str " ");
}
textarea.append(buffer.toString());
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
panelcentor.add(buttonopen);

JButton buttoncopy=new JButton("复制");
buttoncopy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {//读取文本域写入文件
FileWriter filewriter = new FileWriter(textfileres.getText());
BufferedWriter writer=new BufferedWriter(filewriter);
writer.write(textarea.getText());
writer.close();
System.out.println("写入成功");
} catch (IOException e1) {
e1.printStackTrace();
}

}
});
panelcentor.add(buttoncopy);

JButton buttonclose=new JButton("关闭");
buttonclose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
panelcentor.add(buttonclose);

add(panelnorth,BorderLayout.NORTH);
add(panelcentor,BorderLayout.CENTER);
add(scorllpane,BorderLayout.SOUTH);
setVisible(true);
}
public static void main(String[] args) {
new Test();
}

}

绝对自己写的,第二题就很容易了。自己琢磨琢磨,不会的给我发私信

相关推荐: