当前位置:首页 > 建筑/环境 > 工程监理 > 我的java基础题和答案详解
1If语句相关训练1.(标识符命名)下面几个变量中,那些是对的?那些是错的?错的请说明理由(CDF)A.ILoveJavaB.$20C.learn@javaD.antony.leeE.Hello_WorldF.2tigers答:标识符中不能有@,不能含有点号,开头只能是字母和$2.(Java程序的编译与运行)假设有如下程序:packagecom.corejava.chp1;publicclassHelloWorld{publicstaticvoidmain(Stringargs[]){System.out.println(HelloWorld);}}问:1)假设这个代码存在hello.java文件中,那这个程序能够编译通过?为什么?如果编译不通过,应该如何改进?答:不能,含有public的类文件名必须要和类名一致;应将hello.java改写成HelloWorld.java2)假设这个.java文件放在C:\javafile\目录下,CLASSPATH=.,则生成的.class文件应该放在什么目录下?如何运行?答:.class应该存放在C:\javafile\目录下3.(if语句)读入一个整数,判断其是奇数还是偶数publicclassTest{intn;If(n%2==0){System.out.println(n+“是偶数”);}else{System.out.println(n+“是奇数”);}}4.(操作符)有如下代码:inta=5;intb=(a++)+(--a)+(++a);问执行完之后,b的结果是多少?答:16解析a先把5赋值给b让后再自增1相当于(b=5+(--6)+(++5))25.(基本类型的运算)一家商场在举行打折促销,所有商品都进行8折优惠。一位程序员把这个逻辑写成:shortprice=...;//先计算出原价shortrealPrice=price*8/10;//再计算出打折之后的价格问:这段代码是否正确?如果正确,假设price为100,那计算之后的realPrice值为多少?如果不正确,应该怎么改正?答:不正确,应为int类型不能自动转换成short类型应该为:publicclassDiscountTest{publicstaticvoidmain(String[]args){shortprice=100;shortrealPrice=(short)(price*8/10);System.out.println(realPrice);}}6题忽略7.(操作符)有如下代码:a=(ab)?a:b;请问这段代码完成了什么功能。答:这段代码的作用是取最大值,当ab成立时,a=a;当ab不成立时,a=b;38.(if语句)读入一个整数,表示一个人的年龄。如果小于6岁,则输出“儿童”,6岁到13岁,输出“少儿”;14岁到18岁,输出“青少年”;18岁到35岁,输出“青年”;35岁到50岁,输出“中年”;50岁以上输出“中老年”。答:publicclassAgeTest{publicstaticvoidmain(String[]args){intn=12;if(n6){System.out.println(n+岁属于儿童);}elseif(n=6&&n=13){System.out.println(n+岁属于少儿);}elseif(n=14&&n=18){System.out.println(n+岁属于青少年);}elseif(n18&&n=35){System.out.println(n+岁属于青年人);}elseif(n35&&n=50){System.out.println(n+岁属于中年人);}elseif(n50){System.out.println(n+岁属于中老年人);}}}9.(switch语句)读入一个整数,如果是1~5之间,则分别输出5个福娃的名字,否则输出“北京欢迎你”。4答:publicclassBlessingTest{publicstaticvoidmain(String[]args){intn=2;switch(n){case1:System.out.println(贝贝);break;case2:System.out.println(晶晶);break;case3:System.out.println(焕焕);break;case4:System.out.println(莹莹);break;case5:System.out.println(妮妮);break;default:System.out.println(北京欢迎你);}}}10.(if语句,赋值操作)*读入三个整数,输出这三个整数中最大的一个答:publicclassMaxTest{publicstaticvoidmain(String[]args){inta=2;intb=5;intc=6;if(ab&&ac){System.out.println(a+是三个数中的最大值);}elseif(ab&&bc){System.out.println(b+是三个数中的最大值);}else{System.out.println(c+是三个数中的最大值);}5}}11.(if语句)*读入一个表示年份的整数,判断这一年是否是闰年。如何判断一个年份是否是闰年:1.如果这个年份能够被4整除,且不能被100整除,则这一年是闰年。例如,1996年是闰年,而相应的,1993年就不是闰年。2.如果这个年份能够被100整除,则这个数必须要能被400整除,才是闰年。例如,2000年是闰年,1900年不是闰年。答:publicclassLeapYearTest{publicstaticvoidmain(String[]args){intyear=1900;if(year%4==0&&year%100!=0||year%100==0&&year%400==0){System.out.println(year+年是闰年);}else{System.out.println(year+年是平年);}}}12.(switch语句)*完成一个简单的计算器程序。程序要求如下:1.读入两个整数2.提示用户选择对这两个整数的操作,即输出1:+2:-3:*4:/请输入您的选择:读入用户的选择,输出运算结果。答:publicclassCalculator{privatestaticintoperators;publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);System.out.println(请输入要计算的第一个数字:);inta=sc.nextInt();System.out.println(请输入要计算的第二个数字:);intb=sc.nextInt();System.out.println(请选择要运算的方式:1,2,3,4分别代表加6减乘除);operators=sc.nextInt();doublesum=CalculatorMethod(a,b);System.out.println(a+和+b+运算的结果是:+sum);sc.close();}privatestaticdoubleCalculatorMethod(inta,intb){doublesum=0;switch(operators){case1:sum=a+b;break;case2:sum=a-b;break;case3:sum=a*b;break;case4:sum=a/b;}returnsum;}}13.(if语句)*托运计费问题:当货物重量小于20公斤的时候,收费5元,大于20公斤小于100公斤的时候超出20公斤的部分按每0.2元每公斤计费,如果超出100公斤的时候,超出的部分按照每公斤0.15元计算。读入货物的重量,输出计算之后货物的运费。答:publicclassCheckFee{publicstaticvoidmain(String[]args){doublew=130;doubleprice=0;if(w=20){price=5;}elseif(w20&&w=100){price=5+(w-20)*0.2;}elseif(w100){price=5+(100-20)*0.2+(w-100)*0.15;}System.out.println(price);}}
共122篇文档
格式: doc
大小: 95.0 KB
时间: 2020-01-01
本文标题:我的java基础题和答案详解
链接地址:https://www.777doc.com/doc-2442759 .html