java题.docx

上传人:b****1 文档编号:10659545 上传时间:2023-05-27 格式:DOCX 页数:21 大小:47.30KB
下载 相关 举报
java题.docx_第1页
第1页 / 共21页
java题.docx_第2页
第2页 / 共21页
java题.docx_第3页
第3页 / 共21页
java题.docx_第4页
第4页 / 共21页
java题.docx_第5页
第5页 / 共21页
java题.docx_第6页
第6页 / 共21页
java题.docx_第7页
第7页 / 共21页
java题.docx_第8页
第8页 / 共21页
java题.docx_第9页
第9页 / 共21页
java题.docx_第10页
第10页 / 共21页
java题.docx_第11页
第11页 / 共21页
java题.docx_第12页
第12页 / 共21页
java题.docx_第13页
第13页 / 共21页
java题.docx_第14页
第14页 / 共21页
java题.docx_第15页
第15页 / 共21页
java题.docx_第16页
第16页 / 共21页
java题.docx_第17页
第17页 / 共21页
java题.docx_第18页
第18页 / 共21页
java题.docx_第19页
第19页 / 共21页
java题.docx_第20页
第20页 / 共21页
亲,该文档总共21页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

java题.docx

《java题.docx》由会员分享,可在线阅读,更多相关《java题.docx(21页珍藏版)》请在冰点文库上搜索。

java题.docx

java题

程序结果题

1跳转语句

publicclassDemoBreakStatement{

publicstaticvoidmain(String[]args){

for(inti=0;i<8;i++){

if(i==4){

System.out.println("Theloopisbreakwheni==4");

break;

//continue;

}

System.out.println(i+"\t");

}

System.out.println("outsidetheforloop");

}

}

运行结果:

0

1

2

3

Theloopisbreakwheni==4

outsidetheforloop

//若把break换成continue,则为

0

1

2

3

Theloopisbreakwheni==4

5

6

7

outsidetheforloop

2方法重载

publicclassOverloadDemo{

voidtest(){System.out.println("没有参数");}

voidtest(inta){System.out.println("值a:

"+a);}

voidtest(inta,intb){System.out.println("整型a和b的值为:

"+a+""+b);}

doubletest(doublea){System.out.println("双精度值a:

"+a);returna*a;}

publicstaticvoidmain(String[]args){

OverloadDemoob=newOverloadDemo();

doubleresult;

ob.test();

ob.test(10);

ob.test(10,20);

result=ob.test(123.25);

System.out.println("ob.test(123.25)方法执行的结果为:

"+result);

}

}

运行结果:

没有参数

值a:

10

整型a和b的值为:

1020

双精度值a:

123.25

ob.test(123.25)方法执行的结果为:

15190.5625

3静态变量

publicclassStaticDemo{

staticintx=0;

StaticDemo(){++x;}

publicstaticvoidmain(String[]args){

System.out.println(StaticDemo.x);

StaticDemod1=newStaticDemo();

StaticDemod2=newStaticDemo();

System.out.println(StaticDemo.x);

d1.x=100;

StaticDemo.x=200;

System.out.println(d2.x);

}

}

运行结果:

0

2

200

4异常

(1)

publicclassExceptionDemo{

staticintcalculate(inta,intb){

intc=a/b;returnc;

}

publicstaticvoidmain(String[]args){

try{

intc=calculate(9,0);

System.out.println(c);

}

catch(Exceptione){

System.err.println("发生异常:

"+e.toString());

e.printStackTrace();

}

}

}

答案:

发生异常:

java.lang.ArithmeticException:

/byzero

java.lang.ArithmeticException:

/byzero

atexception.ExceptionDemo.calculate(ExceptionDemo.java:

5)

atexception.ExceptionDemo.main(ExceptionDemo.java:

9)

(2)

publicclassCatch22{

publicstaticvoidmain(String[]args){

try{

Stringnum=args[0];

intnumValue=Integer.parseInt(num);

System.out.println("平方为"+numValue*numValue);

}

catch(ArrayIndexOutOfBoundsExceptionne){

System.out.println("未提供任何参数!

");

}

catch(NumberFormatExceptionnb){

System.out.println("不是数字!

");

}

}

}

答案:

未提供任何参数!

......

 

(3)

publicclassFinallyDemo{

intno1,no2;

FinallyDemo(Stringa[]){

try{

no1=10;

no2=5;

//no1=10;

//no2=0;

////no1=Integer.parseInt(a[0]);//代表要输入的数字公式,写程序时不用写此句

////no2=Integer.parseInt(a[1]);//

System.out.println("相除结果为"+no1/no2);

}

catch(ArithmeticExceptioni){

System.out.println("不能除以0");

}

finally{

System.out.println("Finally已执行");

}

}

publicstaticvoidmain(String[]args){

newFinallyDemo(args);

}

}

答案:

相除结果为2

Finally已执行

//不能除以0

//Finally已执行

(4)

publicclassThrowsDemo{

staticvoidthrowOne()throwsIllegalAccessException{

System.out.println("在throwOne中.");

thrownewIllegalAccessException("非法访问异常");

}

publicstaticvoidmain(Stringargs[]){

try{

throwOne();

}

catch(IllegalAccessExceptione){

System.out.println("捕获"+e);

}

}

}

答案:

在throwOne中.

捕获java.lang.IllegalAccessException:

非法访问异常

(5)

publicclassArraySizeExceptionextendsNegativeArraySizeException{

ArraySizeException(){

super("您传递的是非法的数组大小");

}

}

publicclassUserExceptionDemo{

privatestaticString[]arg;

intsize,array[];

UserExceptionDemo(ints){

size=s;

try{checkSize();}

catch(ArraySizeExceptione)

{System.out.println(e);}

}

voidcheckSize()throwsArraySizeException{

if(size<0)thrownewArraySizeException();

array=newint[size];

for(inti=0;i

array[i]=i+1;

System.out.print(array[i]+"");

}

}

publicstaticvoidmain(String[]args){

newUserExceptionDemo(Integer.parseInt(arg[0]));

}

}

答案:

 

编程题

1排序

publicclassSortArray{

publicstaticvoidmain(String[]args){

intiArray[]={2,9,3,6,5,4,6,8,1,12};

intlen=iArray.length;//存放数组的长度

System.out.println("排序前的顺序是:

\n");

for(inti=0;i

System.out.println(iArray[i]+"\t");

}

intiTemp=0;

for(inti=0;i

for(intj=i;j

if(iArray[i]

iTemp=iArray[i];

iArray[i]=iArray[j];

iArray[j]=iTemp;

}

}

}

System.out.println("\n排序后的顺序是:

\n");

for(inti=0;i

System.out.println(iArray[i]+"\t");

}

}

}

运行结果:

排序前的顺序是:

2

9

3

6

5

4

6

8

1

12

排序后的顺序是:

12

9

8

6

6

5

4

3

2

1

2构造方法

classEmployee{

Stringname;

doublebasic;

Stringaddress;

Employee(){}

Employee(Stringstr,doublesal,Stringaddr){

name=str;

basic=sal;

address=addr;

}

voidshow(){

System.out.println("姓名:

\t\t\t"+name);

System.out.println("地址:

\t\t\t"+address);

System.out.println("薪资:

\t\t\t"+basic);

}

}

classManagerextendsEmployee{

Stringdepartment;

Manager(){}

Manager(Stringstr,doublesal,Stringaddr,Stringdept){

super(str,sal,addr);

department=dept;

}

voidshow(){

super.show();

System.out.println("部门:

\t\t\t"+department);

}

}

classDirectorextendsEmployee{

doubletransportAllowance;

Director(){}

Director(Stringstr,doublesal,Stringaddr,doubleallowance){

super(str,sal,addr);

transportAllowance=allowance;

}

voidshow(){

super.show();

System.out.println("交通津贴:

\t\t\t"+transportAllowance);

}

}

publicclassEmployeeTest{

EmployeeTest(){}

publicstaticvoidmain(String[]args){

ManagermgrObj=newManager("刘海松",5500.65,"郑州","会计部");

System.out.println("\n经理详细信息");

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

mgrObj.show();

DirectordirObj=newDirector("刘红霞",32564.00,"邢台",8000);

System.out.println("\n\n经理详细信息");

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

dirObj.show();

}

}

运行结果:

经理详细信息

==================

姓名:

刘海松

地址:

郑州

薪资:

5500.65

部门:

会计部

 

经理详细信息

====================

姓名:

刘红霞

地址:

邢台

薪资:

32564.0

交通津贴:

8000.0

3字符串的增删改查

publicclassTestString{

publicstaticvoidmain(String[]args){

TestStringtest=newTestString();

test.testStartsOrEndsWith();

test.testInser();

test.testDelete();

test.testReplace();

}

//查找

publicvoidtestStartsOrEndsWith(){

Stringsites[]={"","ftp:

//",""};

for(inti=0;i

{

if(sites[i].startsWith("http:

"))

{

System.out.println("使用http协议的网站地址:

"+sites[i]);

if(sites[i].endsWith("com")){

System.out.println("使用http协议并且以com结束的网站地址:

"+sites[i]);

}

}

}

}

//增加

publicvoidtestInser(){

Stringsites[]={"","ftp:

//",""};

/*Stringstr1=sites[1];

str1="";*/

for(inti=0;i

StringBufferbuffer=newStringBuffer("增加的网站是:

");

//System.out.println(buffer.toString());

buffer.insert(7,sites[i]);

System.out.println(buffer.toString());

}

//for(inti=0;i

//System.out.println("增加的网站是:

"+sites[i]+"\t");

}

//删除

publicvoidtestDelete(){

Stringsites[]={"","ftp:

//",""};

for(inti=0;i

StringBufferbuffer=newStringBuffer(sites[i]);

buffer.delete(0,12);

System.out.println(buffer.toString());

}

}

//修改

publicvoidtestReplace(){

Stringsites[]={"","ftp:

//",""};

for(inti=0;i

System.out.println("修改前的sites[i]的值是:

"+sites[i]);

sites[i]=sites[i].replace('t','T');

}

for(inti=0;i

System.out.println("修改后的sites[i]的值是:

"+sites[i]);

//或者

Stringstemp=",ftp:

//,";

System.out.println(stemp);

Stringtemp=stemp.replace('w','8');

System.out.println(temp);

}

}

运行结果:

使用http协议的网站地址:

使用http协议并且以com结束的网站地址:

增加的网站是:

增加的网站是:

ftp:

//

增加的网站是:

修改前的sites[i]的值是:

修改前的sites[i]的值是:

ftp:

//

修改前的sites[i]的值是:

修改后的sites[i]的值是:

hTTp:

//www.iT

修改后的sites[i]的值是:

fTp:

//fTp.cerneT

修改后的sites[i]的值是:

hTTps:

//

ftp:

//,

ftp:

//,

或者。

publicclassCheck{

publicstaticvoidmain(String[]args){

//StringsTemp="InGotwetrustGot";

//System.out.println("输出的字符串为:

"+sTemp);

//intlength=sTemp.length();

Checkstem=newCheck();

stem.stemSeek();

stem.stemReplace();

stem.stemInsert();

stem.stemDelete();

}

//查找

publicvoidstemSeek(){

StringsTemp="InGotwetrustGot";

intlength=sTemp.length();

for(inti=0;i

i=sTemp.indexOf("Got",i);

if(i<0)return;

System.out.println("查找字符串\""+sTemp+"\"中Got的索引值:

"+i+"\t");

}

}

//修改

publicvoidstemReplace(){

StringsTemp="InGotwetrustGot";

intlength=sTemp.length();

Stringtemp=sTemp.replace("Got","M");

System.out.println("修改后的字符串为:

"+temp);

}

//增加

publicvoidstemInsert(){

StringsTemp="InGotwetrustGot";

Stringtemp="youtrust";

intlength=sTemp.length();

StringBufferbuffer=newStringBuffer(sTemp);

//System.out.println("增加的字符串为:

"+buffer.toString());

//buffer.append(sTemp);

//System.out.println("增加后的字符串为:

"+buffer.substring(0));

buffer.insert(19,temp);//把字符串temp放到下标为19的位置

System.out.println("增加后的字符串为:

"+buffer.toString());

}

//删除

publicvoidstemDelete(){

StringsTemp="InGotwetrustGot"

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

当前位置:首页 > 外语学习 > 日语学习

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2