2-1 数据类型赋值
public class Assign { public static void main (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
class TestNumber { public static void main(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 class CalSoldiery { public static void main(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 class TestLeapYear { public static void isLeapYear(int year)
{ 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 void main(String args[]){ isLeapYear(1900); isLeapYear(1904); isLeapYear(2000); } }
输出:
1900年不是闰年
1904年是闰年
2000年是闰年
2-5
class TestLogicSymbole{ public static void main(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
class TestConditonExpression { public static void main(String[] args) { float sum=1.5f; int num=2; System.out.println((sum<2 ? 1 : num/sum)); } }
输出:
不等,out=10
2-7
public class TestIf { public static void main(String[] args) { int x,y; x=7;y=1; if(x>6) if(y>6) System.out.println("设备正常"); else System.out.println("设备出错"); } }
输出:
设备出错
2-8 for循环结构逻辑测试
public class TestFor { static boolean foo(char c) { System.out.print(c); return true; } public static void main( 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 class CalChicken { public static void main(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 class CountPrime { public static void main(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
17 19 23 29 31 37
41 43 47 53 59 61
67 71 73 79 83 89
97
2-11
class TestReturn{ public static void main(String[] args){ int i = 10; if (i<5){ return ; //i = 6; } else{ //return; } i = 5; } }
2-12
public class TestArgs{ public static void main(String[] args){ for (int i=0;i<args.length ;i++ ) { System.out.println(args[i]); } } }
2-13 打印图形
public class PrintSpecialArray{ public static void main (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<j) t[i][j]= num - i; else t[i][j]= num - j; 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 class PrintXT{ public static void main(String[] args){ int n=5,colSpan=2,colSpanBase=2; int arr[][] = new int[n][n]; arr[0][0]=1; //为数组赋值 for(int i=0;i<n;i++){ //i为行 for(int j=0;j<n;j++){ //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<n;i++){ for(int j=0;j<n;j++){ System.out.print(arr[i][j]); System.out.print(‘\t‘); if(j==n-i-1) break; } System.out.println(""); } } }
输出:
1 3 6 10 15
2 5 9 14
4 8 13
7 12
11
原文:http://www.cnblogs.com/hzau2013310200722/p/5011211.html