预览加载中,请您耐心等待几秒...
1/10
2/10
3/10
4/10
5/10
6/10
7/10
8/10
9/10
10/10

亲,该文档总共14页,到这已经超出免费预览范围,如果喜欢就直接下载吧~

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

第二章 注意问题: 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("您本月薪资为$"+salary); } } 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("请输入第一个数"); 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;i<aa.length;i++){ System.out.print(aa[i]+”“); } System.out.println();} } 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(); IsLeapYear=((year%4==0&&year%100!=0)||(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; BufferedReaderin=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("x<y"); elseSystem.out.println("x=y"); } } 6,已知一个学生的分数,给出其分数等级。90-100分为A级;80-89分为B级;70-79分为C级;60-69分为D级;0-59分为E级 publicclassIfElseDemo{ publicstaticvoidmain(String[]args){ inttestscor