欢迎来到天天文库
浏览记录
ID:55631653
大小:72.50 KB
页数:14页
时间:2020-05-21
《Java练习题、编程题.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、第二章注意问题:main函数是Java应用程序执行的入口。(int)23.7结果为23(int)-45.89f结果为-45+”除字符串相加功能外,还能把非字符串转换成字符串,如:“x”+123;的结果是“x123”。如果对负数取模,可以把模数负号忽略不记,如:5%-2=1。但被模数是负数就另当别论了1,三元运算符classTernaryOp{publicstaticvoidmain(Stringargs[]){intsalary,daysPresent=30;salary=daysPresent==20?2000:3000;System.out.println("您本月薪资为$"+sal
2、ary);}}2,编写一个程序,这个程序从键盘上输入2个数,然后求这2个数的和,程序结束。提示:调用System.in.read();可以读取到一个从键盘上输入字符对应的整数。通过这个程序,你一定能体会到在什么importjava.io.*;publicclassCh1{publicstaticvoidmain(String[]args)throwsIOException{BufferedReaderke=newBufferedReader(newInputStreamReader(System.in));Stringx;intp,q,m;System.out.print("请输入第一个
3、数");x=ke.readLine();p=Integer.parseInt(x);System.out.print("请输入第二个数");x=ke.readLine();q=Integer.parseInt(x);m=p+q;System.out.println("两个数的和为:"+m);}}3,数组publicclassLo{publicstaticvoidmain(String[]args){int[]aa={32,55,69};for(inti=0;i4、}}4,输入一个年份,判断它是不是闰年。闰年:能被4整除但不能被100整除,或者能被400整除。publicclassex3_1{publicstaticvoidmain(String[]args)throwsIOException{intyear;booleanIsLeapYear;System.out.println("Entertheyear:");BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));year=(newInteger(in.readLine())).intValue();IsLeap5、Year=((year%4==0&&year%100!=0)6、7、(year%400==0));if(IsLeapYear){System.out.print(year);System.out.println("isaleapyear");}else{System.out.print(year);System.out.println("isnotaleapyear");}}}5,输入两个整数比较大小importjava.io.*;publicclassex3_2{publicstaticvoidmain(String[]args)throwsIOException{intx,y;Buffe8、redReaderin=newBufferedReader(newInputStreamReader(System.in));System.out.println("Enterxandy:");x=(newInteger(in.readLine())).intValue();y=(newInteger(in.readLine())).intValue();if(x!=y)if(x>y)System.out.println("x>y");elseSystem.out.println("x9、分数等级。90-100分为A级;80-89分为B级;70-79分为C级;60-69分为D级;0-59分为E级publicclassIfElseDemo{publicstaticvoidmain(String[]args){inttestscore=76;chargrade;if(testscore>=90){grade='A';}elseif(testscore>=80){grade='B';}elseif(testscore>=7
4、}}4,输入一个年份,判断它是不是闰年。闰年:能被4整除但不能被100整除,或者能被400整除。publicclassex3_1{publicstaticvoidmain(String[]args)throwsIOException{intyear;booleanIsLeapYear;System.out.println("Entertheyear:");BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));year=(newInteger(in.readLine())).intValue();IsLeap
5、Year=((year%4==0&&year%100!=0)
6、
7、(year%400==0));if(IsLeapYear){System.out.print(year);System.out.println("isaleapyear");}else{System.out.print(year);System.out.println("isnotaleapyear");}}}5,输入两个整数比较大小importjava.io.*;publicclassex3_2{publicstaticvoidmain(String[]args)throwsIOException{intx,y;Buffe
8、redReaderin=newBufferedReader(newInputStreamReader(System.in));System.out.println("Enterxandy:");x=(newInteger(in.readLine())).intValue();y=(newInteger(in.readLine())).intValue();if(x!=y)if(x>y)System.out.println("x>y");elseSystem.out.println("x9、分数等级。90-100分为A级;80-89分为B级;70-79分为C级;60-69分为D级;0-59分为E级publicclassIfElseDemo{publicstaticvoidmain(String[]args){inttestscore=76;chargrade;if(testscore>=90){grade='A';}elseif(testscore>=80){grade='B';}elseif(testscore>=7
9、分数等级。90-100分为A级;80-89分为B级;70-79分为C级;60-69分为D级;0-59分为E级publicclassIfElseDemo{publicstaticvoidmain(String[]args){inttestscore=76;chargrade;if(testscore>=90){grade='A';}elseif(testscore>=80){grade='B';}elseif(testscore>=7
此文档下载收益归作者所有