JAVA考试题库Word格式.docx
《JAVA考试题库Word格式.docx》由会员分享,可在线阅读,更多相关《JAVA考试题库Word格式.docx(90页珍藏版)》请在冰点文库上搜索。
volume=area*height;
volume=(int)(volume*100)/100.0;
JOptionPane.showMessageDialog(null,"
半径为:
+radius+
"
高度为:
+height+
"
的圆柱体积是:
+volume);
}
3.编写程序读入球的半径,计算球的体积和表面积,并显示结果。
packageeayang;
publicclassTest1{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODO自动生成方法存根
doubler;
doublearea,volume;
r=Double.parseDouble(JOptionPane.showInputDialog("
area=4*3.14*r*r;
volume=4*3.14*r*r/3.0;
JOptionPane.showMessageDialog(null,"
此圆的面积为:
+area+"
此圆的体积:
+volume);
}
4.从命令行读入一些参数,打印参数个数和参数列表。
publicclassTest2{
publicstaticvoidmain(String[]arg){
for(inti=0;
i<
arg.length;
i++){
System.out.println(arg[i]);
5.编写程序读入英尺数,转换为米数并显示结果。
一英尺等于0.305米。
publicclassExercise2_3{
Stringfoot=JOptionPane.showInputDialog(null,"
输入英尺数:
doublem=0.305*Double.parseDouble(foot);
Stringoutput=foot+"
英尺是"
+m+"
米"
;
}
6.编写程序将磅转换为千克。
程序提示用户输入磅数,转换成千克并显示结果。
一磅等于
0.454千克。
publicclassExercise2_4{
Stringpond=JOptionPane.showInputDialog(null,"
请输入英镑数:
Stringoutput=pond+"
英镑是"
+Double.parseDouble(pond)*0.454+"
千克"
7.编写一个程序,读入费用与提成率,计算提成与总费用,例如:
如果使用者键入10作为费用,15%作为提成率,计算结果显示1.5作为提成费,11.5作为总费用。
publicclassExercise2_5{
Doublepay=Double.parseDouble(JOptionPane.showInputDialog(null,"
请输入费用:
Doubletax=Double.parseDouble(JOptionPane.showInputDialog(null,"
请输入提成:
提成费为:
+pay*tax+"
\n总费用是:
+(pay+pay*tax);
8.(求一个整数各位的和)编写程序读入0到1000之间的一个整数,并将其各位数字加起来。
例如整数932,各位数字之和为14。
//Exercise2_6.java:
Summarizealldigitsinaninteger<
1000
publicclassExercise2_6{
//Mainmethod
//Readanumber
StringnumberString=JOptionPane.showInputDialog(null,
Enteranintegerbetween0and1000:
Exercise2_6"
JOptionPane.QUESTION_MESSAGE);
intnumber=Integer.parseInt(numberString);
//Findalldigitsinnumber
intlastDigit=number%10;
intremainingNumber=number/10;
intsecondLastDigit=remainingNumber%10;
remainingNumber=remainingNumber/10;
intthirdLastDigit=remainingNumber%10;
//Obtainthesumofalldigits
intsum=lastDigit+secondLastDigit+thirdLastDigit;
//Displayresults
System.out.println("
Thesumofalldigitsin"
+number
+"
is"
+sum);
System.exit(0);
9.(将大写字母转换为小写字母)编写一个方法将大写字母转换为小写字母。
publicclassExercise2_7{
charuppercase='
E'
intoffset=(int)'
a'
-(int)'
A'
charlowercase=(char)((int)uppercase+offset);
字符"
+uppercase+"
转化为小写字母为:
+lowercase);
10.(从ASCII求出对应的字符)编写程序读入一个ASCII码(从1到128的整数)并显示它表示的字符。
例如,如果用户输入97,程序显示字母a。
importjava.util.Scanner;
publicclassExercise2_8{
Scannerinput=newScanner(System.in);
intnum=input.nextInt();
if(num<
0||num>
127){
输入有误!
程序终止运行"
ASCII码为:
+num+"
的字符是:
+(char)num);
11.(判断三角形〉编写程序输入三角形的三个边,判断输入是否有效。
mportjavax.swing.JOptionPane;
publicclassExercise3_1{
doubleedge1=Double.parseDouble(JOptionPane.showInputDialog(null,"
输入第一条边长:
doubleedge2=Double.parseDouble(JOptionPane.showInputDialog(null,"
输入第二条边长:
doubleedge3=Double.parseDouble(JOptionPane.showInputDialog(null,"
输入第三条边长:
booleanisTriangle=isTriangle(edge1,edge2,edge3);
Candeges"
+edge1+"
"
+edge2+"
and"
+edge3+"
fromatriangle?
+
isTriangle;
privatestaticbooleanisTriangle(doubleedge1,doubleedge2,doubleedge3){
doubletemp;
if(edge1<
edge2){
temp=edge1;
edge1=edge2;
edge2=temp;
edge3){
edge1=edge3;
edge3=temp;
if(edge2<
temp=edge2;
edge2=edge3;
edge3=temp;
(edge2+edge3))
returntrue;
else
returnfalse;
12.(判断一个数是否为偶数)编写程序读入一个整数并判断其是否为偶数。
publicclassExercise3_2{
intnum=Integer.parseInt(JOptionPane.showInputDialog(null,"
输入一个整数"
Is"
+num+"
anevennumber?
+isEven(num);
privatestaticbooleanisEven(intnum){
if(num%2==0)
13.编写程序输入一个整数,判断其是否能被5和6整除,是否能被5或6整除,是否能被5或6整除但不能被5和6整除。
publicclassExercise3_3{
divisibleby5and6?
+isDivisibleBoth(num)+
\nIs"
divisibleby5or6?
+isDivisibleOr(num)+
divisibleby5or6,butnotboth?
+isDivisible(num);
JOptionPane.showMessageDialog(null,output);
privatestaticbooleanisDivisible(intnum){
if(num%5==0^num%6==0)
privatestaticbooleanisDivisibleBoth(intnum){
if(num%5==0&
&
num%6==0)
privatestaticbooleanisDivisibleOr(intnum){
if(num%5==0||num%6==0)
else
14.编写程序,读入资金额(现值)、年利率和年数,显示终值(将来的资金额),计算公
式如下:
终值=现值*(1+年利率)年数
15(三个整数排序)编写程序对三个整数排序。
整数由输入对话框读入,并分别存入变量num1、
num2和num3,对它们进行排序,使得num1<
=num2<
=num3。
publicclassExercise3_8{
intnum1,num2,num3,temp;
num1=Integer.parseInt(JOptionPane.showInputDialog(null,"
输入第一个数:
num2=Integer.parseInt(JOptionPane.showInputDialog(null,"
输入第二个数:
num3=Integer.parseInt(JOptionPane.showInputDialog(null,"
输入第三个数:
Stringoutput=num1+"
+num2+"
+num3;
if(num1>
num2){
temp=num1;
num1=num2;
num2=temp;
}
num3){
num1=num3;
num3=temp;
if(num2>
temp=num3;
num3=num2;
output+="
排序的结果是:
\n"
+
num1+"
16(计算三角形的周长)编写程序,读入三角形的三边,如果输入有效,计算它的周长;
否则,显示输入无效。
如果任意两边的和大于第三边,输入有效。
publicclassExercise3_9{
doubleside1,side2,side3;
booleanisTriangle=false;
side1=Double.parseDouble(JOptionPane.showInputDialog(null,"
side2=Double.parseDouble(JOptionPane.showInputDialog(null,"
side3=Double.parseDouble(JOptionPane.showInputDialog(null,"
isTriangle=((side1+side2>
side3)&
(side1+side3>
side2)&
(side2+side3>
side1));
if(isTriangle)
JOptionPane.showMessageDialog(null,"
三角形的周长为:
+(side1+side2+side3));
输入的数据,不能组成三角形"
17(查找当月的天数)编写程序,提示用户输入年和月,而后显示该月的天数。
例如,如果用户输入2000年2月时,应该显示2000年2月有29天。
如果用户输入2005年3月时,应该显示2005年3月有31天。
publicclassExercise3_11{
intday=0;
intyear=Integer.parseInt(JOptionPane.showInputDialog("
输入年份:
intmonth=Integer.parseInt(JOptionPane.showInputDialog("
输入月份:
booleanisLeapYear=((year%4==0&
year%100!
=0)||(year%400==0));
if(isLeapYear&
month==2)
day=29;
elseif(month==4||month==6||month==9||month==11)
day=30;
elseif(month==2)
day=28;
elseday=31;
Stringoutput=year+"
年"
+month+"
月有"
+day+"
天"
18(统计正数和负数的个数并计算这些数的平均数)编写程序,读入个数不确定的整数,求出读人的正数和负数个数,并计算它们的总和及平均值,0不参与计数。
当输入为0时,
程序结束。
将平均值作为一个浮点数来显示。
(例如,如果输入1、2和0,平均值应当
为1.5。
)
publicclassExercise4_2{
intsum=0,count1=0,count2=0,num;
Stringoutput="
while(true){
num=Integer.parseInt(JOptionPane.showInputDialog("
输入整数求平均数,以0为结束标志"
if(num==0)
break;
sum+=num;
output+=num+"
if(num>
0)
count1++;
else
count2++;
output+="
的平均数为:
+(double)sum/(count1+count2)+
"
\n正数的个数为:
+count1+
\n负数的个数为:
+count2;
19(千克转换成磅)编一个显示下列表格的程序(注意,1千克为2.2磅):
千克磅
12.2
36.6
197433.4
199437.8
publicclassExercise4_3{
//TODOAuto-generatedmethodstub
System.out.println("
\t千克\t磅"
for(inti=1;
i<
200;
i+=2){