java输出斜塔_第二章 Java语言基础(1)

2-1 数据类型赋值

public classAssign

{public static voidmain (String args [] )

{int x , y ; //定义x,y两个整型变量

float z = 1.234f ; //指定变量z为float型,且赋初值为1.234

double w = 1.234 ; //指定变量w为double型,且赋初值为1.234

boolean flag = true ; //指定变量flag为boolean型,且赋初值为true

char c ; //定义字符型变量c

String str ; //定义字符串变量str

String str1 = "Hi" ; //指定变量str1为String型,且赋初值为Hi

c = 'A' ; //给字符型变量c赋值'A'

str = "bye" ; //给字符串变量str赋值"bye"

x = 12 ; //给整型变量x赋值为12

y = 300; //给整型变量y赋值为300

}

}

2-2

classTestNumber

{public static voidmain(String[] args)

{

System.out.print(Math.ceil(5.2)+" ");

System.out.print(Math.ceil(5.6)+" ");

System.out.print(Math.ceil(-5.2)+" ");

System.out.print(Math.ceil(-5.6)+" ");

System.out.print(Math.floor(5.2)+" ");

System.out.print(Math.floor(5.6)+" ");

System.out.print(Math.floor(-5.2)+" ");

System.out.print(Math.floor(-5.6)+" ");

System.out.print(Math.round(5.2)+" ");

System.out.print(Math.round(5.6)+" ");

System.out.print(Math.round(-5.2)+" ");

System.out.print(Math.round(-5.6)+" ");

}

}

输出:

6.0 6.0 -5.0 -5.0 5.0 5.0 -6.0 -6.0 5 6 -5 -6

2-3取余运算:韩信点兵,不足百人。三人一行多一个,七人一行少两个,五人一行正好,问多少人?

public classCalSoldiery

{public static voidmain(String args[])

{for (int i=1;i<100 ;i++)

{if (i%3==1&&i%7==5&&i%5==0)

{

System.out.println("应有士兵"+i+"人");break;

}

}

}

}

输出:应有士兵40人

2-4 判断闰年

public classTestLeapYear

{public static void isLeapYear(intyear)

{boolean n1 = (year%4==0);boolean n2 = (year%100==0);boolean n3 = (year%400==0);if ((n1==true&&n2!=true)||(n2==true&&n3==true))

{System.out.println(year+"年是闰年");}else{System.out.println(year+"年不是闰年");}

}public static voidmain(String args[]){

isLeapYear(1900);

isLeapYear(1904);

isLeapYear(2000);

}

}

输出:

1900年不是闰年

1904年是闰年

2000年是闰年

2-5

classTestLogicSymbole{public static voidmain(String[] args)

{int out =10;boolean b1=false;if ((b1==true)&&(out+=10)==20)

{

System.out.println("相等,out="+out);

}else{

System.out.println("不等,out="+out);

}

}

}

输出:

不等,out=10

2-6

classTestConditonExpression

{public static voidmain(String[] args)

{float sum=1.5f;int num=2;

System.out.println((sum<2 ? 1 : num/sum));

}

}

输出:

不等,out=10

2-7

public classTestIf

{public static voidmain(String[] args)

{intx,y;

x=7;y=1;if(x>6)if(y>6)

System.out.println("设备正常");elseSystem.out.println("设备出错");

}

}

输出:

设备出错

2-8 for循环结构逻辑测试

public classTestFor {static boolean foo(charc) {

System.out.print(c);return true;

}public static voidmain( String[] argv ) {int i =0;for ( foo('A'); foo('B')&&(i<2); foo('C')){

i++;

foo('D');

}

}

}

输出:

ABDCBDCB

2-9百鸡问题:公鸡5元/只,母鸡3元/只,小鸡3元/只,若买100只鸡,问其中公鸡、母鸡、小鸡各多少只?

public classCalChicken

{public static voidmain(String args[]){int z=0;boolean isAnswer=false;for (int i=1;i<=20 ;i++)

{for (int j=1;j<=33 ;j++)

{

z= 100-i-j;if (z%3==0&&(5*i+3*j+z/3==100))

{

System.out.println("公鸡"+i+"只,母鸡"+j+"只,小鸡"+z+"只");

isAnswer= true;

}

}

}if (!isAnswer)

{

System.out.println("本题无解");

}

}

}

输出:

公鸡4只,母鸡18只,小鸡78只

公鸡8只,母鸡11只,小鸡81只

公鸡12只,母鸡4只,小鸡84只

2-10 打印1~100中所有素数

public classCountPrime {public static voidmain(String[] args){int n=1,m,j,i; //n用来计算得到的素数个数

boolean h ; //是素数的标志

System.out.print(2+"\t");for (i=3;i<=100;i+=2)

{

m= (int)Math.sqrt(i); //找到i的平方根

h = true;//在2和m之间进行遍历,如果能被i整除,则i不是素数

for (j=2;j<=m;j++)

{if (i%j==0) //如果能被整除,则不是素数,找下一个数

{

h= false;break;

}

}if (h) //说明找到了素数

{if (n%6==0) //打印格式控制

{

System.out.println("");

}

System.out.print(i+"\t");

n++;

}

}

}

}

输出:

2 3 5 7 11 13

171923293137

414347535961

677173798389

97

2-11

classTestReturn{public static voidmain(String[] args){int i = 10;if (i<5){return;//i = 6;

}else{//return;

}

i= 5;

}

}

2-12

public classTestArgs{public static voidmain(String[] args){for (int i=0;i

{

System.out.println(args[i]);

}

}

}

2-13 打印图形

public classPrintSpecialArray{public static voidmain (String args[]){final int num=8;int[][] t= new int[2*num+1][2*num+1];//赋值

for (int i=0;i<=num;i++)

{for (int j=0;j<=num; j++)

{if(i

t[i][2*num-j] =t[i][j];

t[2*num-i][j] =t[i][j];

t[2*num-i][2*num-j] =t[i][j];

}

}//打印

for (int i=0;i<2*num+1;i++)

{for (int j=0;j<2*num+1; j++)

{

System.out.print(t[i][j]);

}

System.out.println("");

}

}

}

输出:

88888888888888888

87777777777777778

87666666666666678

87655555555555678

87654444444445678

87654333333345678

87654322222345678

87654321112345678

87654321012345678

87654321112345678

87654322222345678

87654333333345678

87654444444445678

87655555555555678

87666666666666678

87777777777777778

88888888888888888

2-14 输出数字斜塔

public classPrintXT{public static voidmain(String[] args){int n=5,colSpan=2,colSpanBase=2;int arr[][] = new int[n][n];

arr[0][0]=1;//为数组赋值

for(int i=0;i

for(int j=0;j

if(j==n-i-1) break; //数组对角线位置

arr[i][j+1]=arr[i][j]+colSpan;

colSpan+=1; //列加数因子调整

}if(i==n-1) break; //i为最后一行退出

arr[i+1][0]=arr[i][0]+i+1; //为下行首位赋值

colSpanBase +=1; //调整行加数因子

colSpan = colSpanBase; //调整列加数因子

}//数组内容输出

for(int i=0;i

System.out.print(arr[i][j]);

System.out.print('\t');if(j==n-i-1) break;

}

System.out.println("");

}

}

}

输出:

1 3 6 10 15

25914

4813

712

11

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值