以下代码的运行结果为: public class ex2 { public static void main(String[] args) { int total = 0; for(int i = 0,j = 10;total > 30;++i,--j){ System.out.println("I = " + i + " ;j = " + j); total += (i+j); } System.out.println("Total: " + total); } }
A、产生运行错误
B、产生编译错误
C、输出“Total: 0”
D、输出如下结果: i = 0 ;j = 10 i = 1 ;j = 9 i = 2 ;j = 8 Total: 30
第1题
比较出结果。例如:输入第1个整数为12,第2个整数为33。比较结果显示:
12!=33
12<33
12<=33
程序有多个遗漏和错误。本题要求改一个错,填三个空。
注意:不改动程序结构,不得增行或删行。
import javax.swing.JOptionPane;
public class ex3
{
public static void main(String args[])
{
String firstNumber, //用户输入第1个字符串
secondNumber, //户输入第2个字符串
result; // a string containing the output
int number1, //较的第1个数
number2; //的第2个数
//读用户输入的第1个字符串read first number from user as a string
firstNumber=
JOptionPane.showlnputDialog("Enter first integer:");
//用户输入的第2个字符串read second number from user as a string
secondNumber=
JOptionPane.showInputDialog("Enter second integer:");
//字符串类型转换成整数类型
number1=Integer.parseInt(firstNumber);
number2=Integer.parseInt(secondNumber);
result="";
if(String.valueOf(numberl)=String.valueOf(number2))
result=String.valueOf(numberl)+"= ="+String.valueOf(number2);
if(String.valueOf(number1)!=String.valueOf(number2))
result=String.valueOf(number1)+" !="+String.valueOf(number2);
if(String.valueOf(numberl)< String.valueOf(number2))
result=result+"\n" +String.valueOf(numberl)+"<"
+String.valueOf(number2);
if(String.valueOf(numberl)>String.valueOf(number2))
result=result+"\n" +String.valueOf(numberl)+" >"
+String.valueOf(number2);
if(String.valueOf(numberl)<=String.valueOf(number2))
result=result+"\n"+String.valueOf(numberl)+" <="
+String.valueOf(number2);
if(String.valueOf(numberl)>=String.valueOf(number2))
result=result+"\n"+String.valueOf(numberl)+ ">="
+String.valueOf(number2);
______(null,result,"比较结果",______INFORMATION_MESSAGE);
//程序正常退出
______;
}
}
第2题
【说明】本程序ExceptionTester实现功能:读入两个整数,第1个数除以第2个数,之后输出。若第2个数为0,则自动进行异常处理。
程序如下:
(1);
public class ExceptionTester{
public static void main(String args[]){
int result;
int number[]=new int[2];
boolean valid;
for(int i=0;i<2;i++){
valid=(2);
while(!valid){
try{
System.out.println("Enter number"+(i+1));
number[i]=Integer.valueOf(Keyboard.getString()).intValue();
valid=true;
}catch(NumberFormatExceptione){
System.out.println("Invalid integer entered.Please try again.");
}
}
}
by{
result=number[0]/number[1];
System.out.print(number[0]+"/"+number[1]+"="+result);
}catch((3)){
System.out.println("Second number is 0,cannot do division!");
}
}
}
其中,Keyboard类的声明为:
impon java.io.*;
public class Keyboard{
static BufferedReader inputStream=new (4)
(new InputStreamReader(System.in));
public static int getInteger(){
try{
return(Integer,valueOf(inputStream.readLlne().trim()).intValue());
}catch(Exceptione){
e.printStackTrace();
return 0;
}
}
public (5) {
by{
return(inputStream.readLine());
} catch(IOExceptione)
{return "0";}
}
}
第3题
注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。
源程序文件代码清单如下:
import java.awt.*;
import java.awt.event.*;
public class ex7_2 extends Frame. implements ActionListener
{
private Label 1;
private TextField tf,tf2;
public static void main(String args[])
{
ex7_2 obj7_2=new ex7_2();
}
public ex7_2()
{
setTitle("ex7_2");
Panel p1;
Button b=new Button("OK");
p1=new Panel();
tf=new TextField(8);
p1.add(tf);
tf2=new TextField(8);
p1.add(tf2);
b.addActionListener(this);
p1.add(b);
b=new Button("Exit");
b.addActionListener(this);
p1.add(b);
add("North",p1);
Panel p2=new Panel();
l=new Label("最大公因数:");
p2.add(l);
add("Center",p2);
setsize(500,300);
show();
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Exit"))
//比较ae对象的标识名,是否为Exit
System.exit(0);
else if(ae.getActlonCommand().equals("OK"))
{
try
{
int a=Integer.parseInt(tf.getText());
int b=Integer.parseInt(tf2.getText());
int r,i;
while(b>0){
______;
______;
b=r;
}
l.setText("最大公约数: "+Integer.toString(a));
}catch{NumberFormatException nfe)
{
l.setText("请正确输入!");
}
}
}
}
第4题
以下代码运行结果是 class Exsuper{ String name; String nick_name; public Exsuper(String s,String t){ name = s; nick_name = t; } public String toString(){ return name; } } public class Example extends Exsuper { public Example(String s,String t){ super(s,t); } public String toString(){ return name+" a.k.a "+nick_name; } public static void main(String[] args) { Exsuper a = new Exsuper("First","1"); Example b = new Example("Second","2"); System.out.println("a is "+a.toString()); System.out.println("b is "+b.toString()); } }
A、编译错误
B、ClassCastException异常
C、输出 a is First b is Second
D、输出 a is First a.k.a 1 b is Second a.k.a 2
E、输出 a is First b is Second a.k.a 2
第5题
第6题
A、A.输出1
B、B.输出0
C、C.输出0.5
D、D.编译失败
第7题
A、x=0, y=1, z=1
B、x=0, y=0, z=1
C、x=0, y=2, z=2
D、x=0, y=2, z=1
第8题
A、catch(Exception x){ }
B、catch(Test x) { }
C、catch(E1 x){ }
D、catch(E2 x){ }
第9题
B、b
C、null
D、names
第10题
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!