java程序设计第二章例题程序Word文档下载推荐.docx

上传人:b****2 文档编号:3564410 上传时间:2023-05-02 格式:DOCX 页数:23 大小:19.45KB
下载 相关 举报
java程序设计第二章例题程序Word文档下载推荐.docx_第1页
第1页 / 共23页
java程序设计第二章例题程序Word文档下载推荐.docx_第2页
第2页 / 共23页
java程序设计第二章例题程序Word文档下载推荐.docx_第3页
第3页 / 共23页
java程序设计第二章例题程序Word文档下载推荐.docx_第4页
第4页 / 共23页
java程序设计第二章例题程序Word文档下载推荐.docx_第5页
第5页 / 共23页
java程序设计第二章例题程序Word文档下载推荐.docx_第6页
第6页 / 共23页
java程序设计第二章例题程序Word文档下载推荐.docx_第7页
第7页 / 共23页
java程序设计第二章例题程序Word文档下载推荐.docx_第8页
第8页 / 共23页
java程序设计第二章例题程序Word文档下载推荐.docx_第9页
第9页 / 共23页
java程序设计第二章例题程序Word文档下载推荐.docx_第10页
第10页 / 共23页
java程序设计第二章例题程序Word文档下载推荐.docx_第11页
第11页 / 共23页
java程序设计第二章例题程序Word文档下载推荐.docx_第12页
第12页 / 共23页
java程序设计第二章例题程序Word文档下载推荐.docx_第13页
第13页 / 共23页
java程序设计第二章例题程序Word文档下载推荐.docx_第14页
第14页 / 共23页
java程序设计第二章例题程序Word文档下载推荐.docx_第15页
第15页 / 共23页
java程序设计第二章例题程序Word文档下载推荐.docx_第16页
第16页 / 共23页
java程序设计第二章例题程序Word文档下载推荐.docx_第17页
第17页 / 共23页
java程序设计第二章例题程序Word文档下载推荐.docx_第18页
第18页 / 共23页
java程序设计第二章例题程序Word文档下载推荐.docx_第19页
第19页 / 共23页
java程序设计第二章例题程序Word文档下载推荐.docx_第20页
第20页 / 共23页
亲,该文档总共23页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

java程序设计第二章例题程序Word文档下载推荐.docx

《java程序设计第二章例题程序Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《java程序设计第二章例题程序Word文档下载推荐.docx(23页珍藏版)》请在冰点文库上搜索。

java程序设计第二章例题程序Word文档下载推荐.docx

=30;

i++,k++){//利用循环,在3行输出数字1~30,每行10个数字。

g.drawString("

i="

+i,xPos,yPos);

xPos+=35;

if((k+1)%10==0){//如果每行显示的元素有10个后,修改坐标显示则在下一行。

xPos=20;

yPos+=20;

//下一行y坐标增加行间隔dy。

}

2.3

publicclassToBinary{

publicstaticvoidmain(String[]args){

printBinary(256);

printBinary(257);

printBinary

(1);

/*该方法将整型参数i以二进制的形式输出,方法没有返回值*/

staticvoidprintBinary(inti){

System.out.print(i+"

的2进制数表示为:

for(intj=31;

j>

=0;

j--)

if(((1<

<

j)&

i)!

=0)

System.out.print("

1"

else

0"

System.out.println();

//换行

2.4

publicclassToBiHex{

Strings1="

111"

111

(2)="

+Integer.valueOf(s1,2).intValue());

111(16)="

+Integer.valueOf(s1,16).intValue());

111(10)="

+Integer.valueOf(s1,10).intValue());

intk=20;

20(10)的二进制表示:

+Integer.toString(k,2));

20(10)的十六进制表示:

"

+Integer.toString(k,16));

2.5

publicclassParseNum{

1.23e8"

s2="

7.8f"

s3="

12345"

doublex=Double.parseDouble(s1);

floaty=Float.parseFloat(s2);

intz1=Integer.parseInt(s3);

intz2=Integer.valueOf(s3).intValue();

x="

+x+"

\ty="

+y+"

\tz1="

+z1+"

\tz2="

+z2);

2.6

importjava.text.DecimalFormat;

publicclassFormatNum{

publicstaticvoidmain(String[]av){

doublex=12345/7.0;

DecimalFormatform1=newDecimalFormat("

0000.0"

DecimalFormatform2=newDecimalFormat("

0,000.000"

DecimalFormatform3=newDecimalFormat("

0,000"

+x);

+form1.format(x));

+form2.format(x));

+form3.format(x));

2.7

importjava.math.BigInteger;

publicclassBigInt{

longx=123456789987654321L,y=123456789999999L;

x*y="

+(x*y));

BigIntegerbigX=newBigInteger("

123456789987654321"

BigIntegerbigY=newBigInteger("

123456789999999"

BigIntegerbigXY=bigX.multiply(bigY);

bigXY="

+bigXY);

2.8

publicclassFactorial{

publicstaticvoidmain(Stringargs[]){

longfact=1;

for(inti=2;

i<

=15;

i++)

fact*=i;

15!

="

+fact);

2.9

publicclassFindE{

doublee=1.0,d=1.0;

intn=1;

while(d>

1.e-8){

d/=n;

e+=d;

n++;

e="

+e);

E="

+Math.E);

2.10

publicclassFibonacci{

longfirst=1,second=1,third;

//初始化前两项first、second为1,

System.out.print(first+"

+second+"

//输出第一、二项

for(inti=3;

=10;

i++){

third=first+second;

//后一项是前两项之和

System.out.print(third+"

//输出所求的后一项

first=second;

second=third;

2.11

publicclassPositiveXY{

intx,y,r1=3,r2=4;

for(x=1;

x<

r2;

x++){

for(y=1;

y<

y++){

if(x*x+y*y>

r1*r1&

&

x*x+y*y<

r2*r2)

y="

+y);

2.12

publicclassBuyChicken{

intcock,hen,chicken;

for(cock=1;

cock<

=20;

cock++){

for(hen=1;

hen<

=33-cock;

hen++){

chicken=100-cock-hen;

if(15*cock+9*hen+chicken==300)

公鸡:

+cock+"

母鸡:

+hen+"

小鸡:

+chicken);

2.13

publicclassPerfectNum{

intsum;

//sum:

存放一个数的所有因子的和

for(intn=1;

n<

=10000;

n++){

sum=0;

for(intk=1;

k<

n;

k++){//求num的所有因子的和

if(n%k==0)//是因子则加到因子和sum中

sum+=k;

if(n==sum)//

System.out.println(n+"

是完全数"

);

2.14

publicclassPrime{

next:

for(intn=100;

n<

=150;

n++){//外层循环对1~100的数,逐个判断是否素数。

for(intk=2;

k<

n;

k++){//内层循环判断外层循环给定的数i是否为素数。

if(n%k==0)//如果i有因子,则不是素数,跳转,判断下一个i

continuenext;

System.out.print(n+"

2.15

publicclassFact1{

if(args.length<

1){

请在命令行输入一个整数。

System.exit(0);

intx=Integer.parseInt(args[0]);

System.out.println(x+"

的阶乘:

+factorial(x));

staticlongfactorial(intk){

=k;

returnfact;

2.16

importjava.io.IOException;

importjava.io.BufferedReader;

importjava.io.InputStreamReader;

publicclassFact2{

publicstaticvoidmain(Stringargs[])throwsIOException{

BufferedReaderbuf;

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

Stringstr;

intx;

输入一个整数,回车后求该数的阶乘。

输入quit,回车后退出程序运行。

while(true){

str=buf.readLine();

if(str.equals("

quit"

))

break;

x=Integer.parseInt(str);

2.17

publicclassComput{

2){

请在命令行输入2个整数。

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

inty=Integer.parseInt(args[1]);

booleancontinueWhile=true;

intn;

请从键盘输入序号1、2、3并回车,执行选择的任务。

==================="

1.求两个数的商"

2.求两个数的余"

3.退出程序"

while(continueWhile){

n=Integer.parseInt(str);

switch(n){

case1:

/"

+(x/y));

case2:

%"

+(x%y));

case3:

continueWhile=false;

2.18

importjava.io.*;

publicclassLeapYear{

publicstaticvoidmain(Stringargs[])throwsIOException{

intyear;

输入一个年份,回车后判断是否为闰年。

year=Integer.parseInt(str);

if(IsLeapYear(year))

System.out.println(year+"

年是闰年。

不是年是闰年。

staticbooleanIsLeapYear(intyear){

if((year%4==0)&

(year%100!

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

returntrue;

returnfalse;

2.19

publicclassUseArray{

int[]x={1,2,3,4,5};

int[]a=addElement1(x);

1.数组a和数组x指向同一个地址引用,其元素值相同:

数组a的元素值:

printArray(a);

数组x的元素值:

printArray(x);

int[]y={1,2,3,4,5};

int[]b=addElement2(y);

\n2.数组b和数组y指向不同的地址引用,其元素值不同:

数组b的元素值:

printArray(b);

数组y的元素值:

printArray(y);

staticint[]addElement1(intdata[]){

int[]array=data;

//array和data指向同一个地址引用

for(inti=0;

i<

array.length;

i++){

array[i]+=10;

returnarray;

staticint[]addElement2(intdata[]){

int[]array=newint[data.length];

staticvoidprintArray(int[]data){

for(inti=0;

data.length;

i++){

+data[i]);

//输出数组元素

2.20

publicclassUseObj{

Aobj1=newA(10,"

java"

Aobj2=newA(100,"

ok"

Aobj3=obj1.add2(obj2);

show("

obj1"

obj1);

obj2"

obj2);

obj3"

obj3);

staticvoidshow(Strings,Aa){

System.out.println(s+"

x="

+a.x+"

\tstr="

+a.str);

classA{

A(intx1,Strings1){

x=x1;

str=s1;

Aadd2(Aa1){

Aa2=newA(0,"

a2.x=x+a1.x;

a2.str=str+a1.str;

returna2;

2.21

publicclassOverloadMethod{

double[]y={1.1,2.2,3.3,4.4,5.5};

String[]str={"

I"

"

like"

!

};

printArray(str);

staticvoidprintArray(double[]data){

staticvoidprintArray(String[]data){

2.22

System.out.

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

当前位置:首页 > 总结汇报 > 学习总结

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

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