Java强化练习

1、两个变量里的数,不通过第三个变量交换

public static void main(String[] args) {

              int a = 100;

              int b = 200;

              a+=b;

              b=a-b;

              a=a-b;

              System.out.println(a);

              System.out.println(b);

//哈哈,这么简单,是不是感觉到一种淡淡的忧伤

//其实,并不难,只需要换个想法

2、判断学生成绩 成绩>90 A 成绩>80 B 成绩>70 C 成绩>60 D 成绩<60 E

public static void main(String[] args) {

              System.out.println("请输入学生成绩:");

              Scanner scanner = new Scanner(System.in);

              double i = scanner.nextDouble();

              int a = (int) i / 10;

              if (a > 10 || a < 0) {

                     System.out.println("您输入的成绩,闹呢?重新输入");

              } else {

                     if (a > 9) {

                            System.out.println("成绩为A");

                     } else if (a > 8) {

                            System.out.println("成绩为B");

                     } else if (a > 7) {

                            System.out.println("成绩为C");

                     } else if (a > 6) {

                            System.out.println("成绩为D");

                     } else if (a < 6) {

                            System.out.println("成绩为E");

                     }

              }

3、输入一个整数,判断是否是偶数

     Scanner scanner = new Scanner(System.in);

     int a = scanner.nextInt();

              if(a%2==0){

                     System.out.println("是偶数");

              }else{

                     System.out.println("不是偶数");

              }

4、输入学生成绩,使用switch判断

判断学生成绩 成绩>90 A 成绩>80 B 成绩>70 C 成绩>60 D 成绩<60 E

public static void main(String[] args) {

                System.out.println("请输入学生成绩:");

                Scanner scanner = new Scanner(System.in);

                double i = scanner.nextDouble();

                switch ((int)i/10) {

                case 9:System.out.println("成绩为A");             

                     break;

                case 8:System.out.println("成绩为B");             

                    break;

                case 7:System.out.println("成绩为C");             

                    break;

                case 6:System.out.println("成绩为D");             

                    break;

                default:System.out.println("不及格");

                    break;

              }

5、根据年份、月份,求出当月天数

     System.out.println("请输入年份:");

              Scanner scanner = new Scanner(System.in);

              int year = scanner.nextInt();

              System.out.println("请输入月份:");

              int mo = scanner.nextInt();

              switch (mo) {

              case 1:             

              case 3:             

              case 5:             

              case 7:             

              case 8:             

              case 10:             

              case 12:System.out.println("本月31天");             

                     break;      

              case 4:             

              case 6:             

              case 9:             

              case 11:System.out.println("本月30天");             

                     break;

              case 2:      

                     //判断是否是闰年

                     if(year % 4 ==0 && year % 100 != 0 || year%400 == 0){

                            System.out.println("本月29天");

                     }else{

                            System.out.println("本页28天");

                     }

                     break;

              default:System.out.println("输入错误");

                     break;

              }

       }

 6、水仙花数

public static void main(String[] args)

{

int i,j,k,n;

for(n=100;n<1000;n++)

{

i=n/100;/*分解出百位*/

j=n/10%10;/*分解出十位*/

k=n%10;/*分解出个位*/

if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)

   { 

System.out.println(n);

   }

}

System.out.println();

}

7、99乘法表

public static void main(String[] args)

{

System.out.println("99乘法表:");

int x,y;

for(x = 0;x <= 9; x++)

{

for(y = 1;y <= x; y++)

{

System.out.print(y+"*"+x+"="+x*y+"\t");

}

System.out.println();

}

}

8、百钱买百鸡

public static void main(String[] args)

{

int x, y, z;

for (x = 1; x < 20; x++) {

for (y = 1; y < 33; y++) {

for (z = 3; z < 300; z = z + 3) {

if ((5 * x + 3 * y + z / 3 == 100) && (x + y + z == 100)) {

System.out.println("公鸡数:" + x + "母鸡数" + y + "小鸡数" + z);

}

}

}

}

}

posted @ 2016-06-15 16:45  熊猫会功夫  阅读(155)  评论(0编辑  收藏  举报