java课后作业7

9.页实验

下列语句哪一个将引起编译错误?为什么?哪一个会引起运行时错误?为什么?

m=d;

d=m;

d=(Dog)m;

d=c;

c=(Cat)m;

答:

d=m;d=c;引起编译错误;

原因:

 

c=(Cat)m;引起运行时错误;

原因:

 

24.页实验

 

25.页实验

 

26.页实验

 

27.页实验

finally语句块一定会执行吗?

答:不一定,1.只有与 finally 相对应的 try 语句块得到执行的情况下,finally 语句块才会执行。 2.在 try 语句块中执行了 System.exit (0) 语句,终止了 Java 虚拟机的运行。3.当一个线程在执行 try 语句块或者 catch 语句块时被打断(interrupted)或者被终止(killed),与其相对应的 finally 语句块可能不会执行。4.在线程运行 try 语句块或者 catch 语句块时,突然死机或者断电,finally 语句块肯定不会执行了。

 

46.页实验

import java.util.InputMismatchException;

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

// TODO 自动生成的方法存根

int cj;

boolean boo=true;

for(;;)

{

try

{

System.out.println("请输入成绩:");

Scanner input=new Scanner(System.in);

cj=input.nextInt();

if(cj<0)

{

System.out.println("输入的成绩不在0到100的范围内!");

boo=false;

}

else{

if(cj<60)

{

System.out.println("不及格!");

boo=true;

}

else

{

if(cj<=70)

{

System.out.println("及格!");

boo=true;

}

else

{

if(cj<=80)

{

System.out.println("中等!");

boo=true;

}

else

{

if(cj<=90)

{

System.out.println("良好!");

boo=true;

}

else

{

if(cj<=100)

{

System.out.println("优秀!");

boo=true;

}

else

{

System.out.println("输入的成绩不在0到100的范围内!");

boo=false;

}

}

}

}

}

}

}

catch (InputMismatchException e)

{

System.out.println("输入错误!");

boo=false;

}

if(boo==true)

{

break;

}

}

}

}

 

原文地址:https://www.cnblogs.com/gzgz/p/4962533.html