JAVA习题.docx

上传人:b****7 文档编号:16387447 上传时间:2023-07-13 格式:DOCX 页数:8 大小:16.05KB
下载 相关 举报
JAVA习题.docx_第1页
第1页 / 共8页
JAVA习题.docx_第2页
第2页 / 共8页
JAVA习题.docx_第3页
第3页 / 共8页
JAVA习题.docx_第4页
第4页 / 共8页
JAVA习题.docx_第5页
第5页 / 共8页
JAVA习题.docx_第6页
第6页 / 共8页
JAVA习题.docx_第7页
第7页 / 共8页
JAVA习题.docx_第8页
第8页 / 共8页
亲,该文档总共8页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

JAVA习题.docx

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

JAVA习题.docx

JAVA习题

实验2

要求输入两个整数,输出这两个整数的和、差、积、商

importjavax.swing.JOptionPane;//programusesJOptionPane

publicclassP2{

//mainmethodbeginsexecutionofJavaapplication

publicstaticvoidmain(Stringargs[])

{

StringfirstNumber;//firststringenteredbyuser

StringsecondNumber;//secondstringenteredbyuser

intnumber1;

intnumber2;

inthe,cha,ji,shang;

//readinfirstnumberfromuserasastring

firstNumber=JOptionPane.showInputDialog("Enterfirstinteger");

//readinsecondnumberfromuserasastring

secondNumber=JOptionPane.showInputDialog("Entersecondinteger");

//convertnumbersfromtypeStringtotypeint

number1=Integer.parseInt(firstNumber);

number2=Integer.parseInt(secondNumber);

he=number1+number2;

cha=number1-number2;

ji=number1*number2;

shang=number1/number2;

//displayresult

System.out.println("和是:

"+he+"\n差是:

"+cha+"\n积是:

"+ji+"\n商是:

"+shang);

JOptionPane.showMessageDialog(null,"和是:

"+he+"\n差是:

"+cha+"\n积是:

"+ji+"\n商是:

"+shang,

"Results",JOptionPane.PLAIN_MESSAGE);

System.exit(0);//terminateapplicationwithwindow

}//endmethodmain

}//endclassAddition

实验3

1、编写一个应用程序,要求用户输入一个圆的半径(double类型),然后计算并输出圆的

直径、周长、面积等信息。

(把一个字符串转换为double类型数据的方法为Double.parseDouble(Strings))

importjavax.swing.JOptionPane;

importjava.text.DecimalFormat;

publicclassP3_1

{

publicstaticvoidmain(Stringargs[])

{

Stringr;

r=JOptionPane.showInputDialog("请输入一个圆的半径:

");

doubleradius;

radius=Double.parseDouble(r);

doublezhijing,l,s;

finaldoubleP=Math.PI;

zhijing=2*radius;

l=2*P*radius;

s=P*Math.pow(radius,2);

DecimalFormatf=newDecimalFormat("0.00");

//System.out.println("圆的半径是:

"+radius+"\n"+"直径是:

"+zhijing+"\n周长是:

"+f.format(l)+"\n面积是:

"+f.format(s));

JOptionPane.showMessageDialog(null,"圆的半径是:

"+radius+"\n"+"直径是:

"+zhijing+"\n周长是:

"+f.format(l)+"\n面积是:

"+f.format(s),

"显示结果",JOptionPane.INFORMATION_MESSAGE);

}

}

2、编写一个应用程序,要求用户输入两个整数,然后输出两个整数中的最大值。

importjavax.swing.JOptionPane;

publicclassP3_2

{

publicstaticvoidmain(Stringargs[])

{

Strings1,s2;

s1=JOptionPane.showInputDialog("请输入第一个整数:

");

s2=JOptionPane.showInputDialog("请输入第二个整数:

");

intn1,n2;

n1=Integer.parseInt(s1);

n2=Integer.parseInt(s2);

intmax;

max=n1>n2?

n1:

n2;

System.out.println(n1+"和"+n2+"的最大值是:

"+max);

}

}实验四

1、输入一个年份,一个月份(使用一个BufferedReader),判断该年该月有多少天(使用switch,

注意要判断2月是多少天)

importjava.io.*;

publicclassP4_1

{

publicstaticvoidmain(Stringargs[])throwsIOException

{

Strings1,s2;

intyear,month;

BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));

System.out.print("请输入一个年份:

");

s1=br.readLine();

System.out.print("请输入一个月份:

");

s2=br.readLine();

year=Integer.parseInt(s1);

month=Integer.parseInt(s2);

switch(month)

{

case1:

case3:

case5:

case7:

case8:

case10:

case12:

System.out.println(year+"年"+month+"月"+"有31天");

break;

case4:

case6:

case9:

case11:

System.out.println(year+"年"+month+"月"+"有30天");

break;

case2:

if((year%4==0&&year%100!

=0)||(year%400==0))

System.out.println(year+"年"+month+"月"+"有29天");

else

System.out.println(year+"年"+month+"月"+"有28天");

break;

default:

System.out.println("你输入的月份是错误的!

");

}

}

}

2、如果一个数按反向顺序放置后仍然与原数相等,称为回文数(如:

12321)。

编程:

入一个5位数,判断此数是否为回文数。

importjava.io.*;

publicclassP4_2

{

publicstaticvoidmain(Stringargs[])throwsIOException

{

BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));

System.out.print("请输入一个5位数:

");

Strings1=br.readLine();

intn=Integer.parseInt(s1);

intn1,n2,n3,n4,n5;

n1=n/10000;

//n2=(n-10000*n1)/1000;

n2=n%10000/1000;

n3=n%1000/100;

n4=n%100/10;

n5=n%10;

if(n1==n5&&n2==n4)

{

System.out.println(n+"是回文数。

");

}

else

{

System.out.println(n+"不是回文数。

");

}

}

}

实验5

3、求S=a+aa+aaa+…+aaaa…a之值,其中a是一个数字。

例如:

2+22+222+2222+22222(此

时n=5),a和n要求用户由键盘输入。

(有规律的式子求和,注意找到前后两项之间的关系,该题中后一项等于前一项的10倍加a)

importjava.io.*;

publicclassP5_1

{

publicstaticvoidmain(Stringargs[])throwsIOException

{

BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));

Strings1,s2;

System.out.print("请输入一个1-9的数字:

");

s1=br.readLine();

System.out.print("请输入项数:

");

s2=br.readLine();

inta,n;

a=Integer.parseInt(s1);

n=Integer.parseInt(s2);

intp=0,sum=0;

for(inti=1;i<=n;i++)

{

p=p*10+a;

sum+=p;

}

for(inti=1;i<=n;i++)

{

for(intj=1;j<=i;j++)

{

System.out.print(a);

}

if(i

System.out.print("+");

elseif(i==n)

System.out.print("=");

}

System.out.println(sum);

}

}

4.编写一个应用程序,要求用户输入5个整数(使用循环结构),输出这五个整数的最大值和最小值。

importjava.io.*;

publicclassP5_2

{

继续阅读

 

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

当前位置:首页 > 自然科学 > 物理

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

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