java 练习的习题 7_15 循环练习代码

//神奇的数列
package com.geminno.day03;

public class AmazingNumber {

public static void main(String[] args) {
    print_1();
}

static void print_1(){
    int base = 9;
    int total = 1;

    for(int i=1;i<10;i++){
        System.out.println(total +"*"+base +"+"+(i+1) + "="+(total * base +(i+1)));

        total = total*10 +(i+1);
    }

运行的结果:  

1*9+2=11
12*9+3=111
123*9+4=1111
1234*9+5=11111
12345*9+6=111111
123456*9+7=1111111
1234567*9+8=11111111
12345678*9+9=111111111
123456789*9+10=1111111111

}

99乘法表

package com.geminno.day03;

public class TestBreak {

public static void main(String[] args) {

    //break 是跳出当前循环
    for (int i = 1; i < 10; i++) {

        for (int j = 1; j <= i; j++) {
            System.out.print(i+"*"+j+"="+i*j+" ");
            /*if(j == 5){
                break;
            }*/

        }

        System.out.println(" ");

        /*if(i == 5){
            break; //跳出当前循环
        }*/
    }

    System.out.println("============================================");

    for (int i = 1; i < 10; i++) {
        if(i == 7){
            continue;// 当==7 时 跳出本次循环,进行下一次循环
        }
        for (int j = 1; j <= i; j++) {
            System.out.print(i+"*"+j+"="+i*j+" ");

        }

        System.out.println(" ");
    }








}

}
运行的结果
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

============================================
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

Switch的事例:
package com.geminno.day03;

import java.util.Date;

public class TestSwitch {

/**
 * @param args
 */
public static void main(String[] args) {
    Date date = new Date();
    System.out.println(date);
    int d = date.getDay();
    System.out.println(d);

    /*if(d == 0){
        System.out.println("星期日");
    }else if(d == 1){
        System.out.println("星期一");
    }else if(d == 2){
        System.out.println("星期二");
    }else if(d == 3){
        System.out.println("星期三");
    }else if(d == 4){
        System.out.println("星期四");
    }else if(d == 5){
        System.out.println("星期五");
    }else if(d == 6){
        System.out.println("星期六");
    }*/

    switch (d) {// byte 。char ,short int enum ,String(JDK1.7以上版本)
    case 0:
        System.out.println("星期日");
        break;
    case 1:
        System.out.println("星期一");
        break;
    case 2:
        System.out.println("星期二");
        break;
    case 3:
        System.out.println("星期三");
        break;
    case 4:
        System.out.println("星期四");
        break;
    case 5:
        System.out.println("星期五");
        break;
    case 6:
        System.out.println("星期六");

    default:
        System.out.println("系统时间错误。。。。。");
        break;
    }

    //定义一个成绩,打印出等级90-100 优秀 ,80--90良好  70 --80 中等  60--70 及格  其他  不及格
    int score = 46;
    switch (score/10) {
    case 10:
    case 9:
        System.out.println("优秀");
        break;
    case 8:
        System.out.println("良好");
        break;  
    case 7:
        System.out.println("中等");
        break;
    case 6:
        System.out.println("及格");
        break;
    default:
        System.out.println("不及格");
        break;
    }



    System.out.println("###########################################3");
    int count = randomData();//3----18

System.out.println(count);
switch (count) {
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
System.out.println(“小”);
break;
case 9:
case 10:
case 11:
case 12:
case 13:
System.out.println(“中”);
break;
case 14:
case 15:
case 16:
case 17:
case 18:
System.out.println(“大”);
break;
default:
break;
}

}


/*
 * 产生三个随机数的和
 */
static int randomData(){
    int sum = 0;

    for (int i = 0; i < 3; i++) {
        int a = (int)(Math.random()*6+1);
        sum += a;
    }

    return sum;
}

}
运行结果
Mon Jul 17 18:34:52 GMT+08:00 2017
1
星期一
不及格

#####################################3

12

while的事例:
package com.geminno.day03;

import java.util.Scanner;

public class TestWhile {

public static void main(String[] args) {
    /*int count = 1;
    int sum = 0;

    while(count <= 100){

        sum  += count; 
        count++;

    }
    System.out.println(sum);*/
    Scanner sc = new Scanner(System.in);

    while(true){
        showMenu();

        System.out.println("请输入操作:");
        String operator = sc.nextLine();

        switch (operator) {
        case "1":

            break;
        case "2":
            break;

        case "3":
            break;

        case "4":

            break;
        case "5":
            System.out.println("欢迎下次再来!!");
            System.exit(0);
            break;


        default:
            break;
        }

    }




}



static void showMenu(){
    //Scanner sc = new Scanner(System.in);
    System.out.println("1.求最大值: ");
    System.out.println("2.求最小值: ");
    System.out.println("3.求指数值: ");
    System.out.println("4.求平方根: ");
    System.out.println("5.退        出 :  ");
}

}
运行结果
1.求最大值:
2.求最小值:
3.求指数值:
4.求平方根:
5.退 出 :
请输入操作:

打印斐波那契数列
package com.geminno.homework;

public class Fibonaqi {

/**
 * @param args
 */
public static void main(String[] args) {
    Math a = null;
    fibonaqi();
}

/**
 * 打印斐波那契数列
 */
static void fibonaqi(){
    int a = 0;
    int b =1;

    System.out.print(a + " "+b+" ");
    for(int i=1;i<1000;i++){
        if((a+b)==i){
            System.out.print(i + "  ");
            a = b;
            b = i;
        }
    }
}

/**
 * 打印乘法口诀表
 */
static void multiTable(){
    for (int i = 1; i < 10; i++) {

        for (int j = 1; j <= i; j++) {
            System.out.print(i+"*"+j+"="+i*j + " ");
        }
        System.out.println();
    }
}

static void testFlower(){
    boolean flag = false;
    int count = 0;

    //循环2---1000
    for (int i = 2; i <=1000; i++) {
        flag = false;
        //使用每一个数据去整除2---本数据的平方根
        for(int j=2;j<=Math.sqrt(i);j++){
            if(i % j == 0){
                //不是质数
                flag = true;
                break;
            }
        }

        //判断标识flag ,
        if(!flag){
            System.out.print(i+"  ");
            count++;

            if(count % 10 == 0){
                System.out.println();
            }

        }
    }

}

}
运行结果
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

水仙花数
package com.geminno.homework;

public class TestFlower {

/**
 * @param args
 */
public static void main(String[] args) {

    for (int i = 1; i < 1000; i++) {
        int num = getFlower(i);
        if(num != 0){
            System.out.println(i);
        }
    }


    double num = add(2.3,3.2);
    System.out.println(num);
}


static int getFlower(int num){
    int h =0;
    int t = 0;
    int z = 0;

    h = num /100;
    t = (num/10)%10;
    z = num % 10;

    if(h*h*h + t*t*t + z*z*z == num){
        return num;
    }

    return 0;
}

static double add(double a ,double b){

    double c = a+b;

    return c;
}

}
运行结果
1
153
370
371
407

1000以内的质数
package com.geminno.homework;

public class TestPrime {

/**
 * @param args
 */
public static void main(String[] args) {

    boolean flag = false;
    int count = 0;

    //循环2---1000
    for (int i = 2; i <=1000; i++) {
        flag = false;
        //使用每一个数据去整除2---本数据的平方根
        for(int j=2;j<=Math.sqrt(i);j++){
            if(i % j == 0){
                //不是质数
                flag = true;
                break;
            }
        }

        //判断标识flag ,
        if(!flag){
            System.out.print(i+"  ");
            count++;

            if(count % 10 == 0){
                System.out.println();
            }

        }
    }


}

}
运行结果
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 101 103 107 109 113
127 131 137 139 149 151 157 163 167 173
179 181 191 193 197 199 211 223 227 229
233 239 241 251 257 263 269 271 277 281
283 293 307 311 313 317 331 337 347 349
353 359 367 373 379 383 389 397 401 409
419 421 431 433 439 443 449 457 461 463
467 479 487 491 499 503 509 521 523 541
547 557 563 569 571 577 587 593 599 601
607 613 617 619 631 641 643 647 653 659
661 673 677 683 691 701 709 719 727 733
739 743 751 757 761 769 773 787 797 809
811 821 823 827 829 839 853 857 859 863
877 881 883 887 907 911 919 929 937 941
947 953 967 971 977 983 991 997

随机产生3个【1,6】之间的随机数,判断三个数之和是小、中、大
三个人压住、小、中、大,判断谁压对了
package com.geminno.math;

import java.util.Scanner;

public class TestMath {
public static void main(String[] args) {
/*double num = Math.random()*6;
int i = (int)num + 1;
System.out.println(i);*/

    double m = Math.random()*10;
    m = Math.ceil(m);

    System.out.println(m);

    //随机产生3个 【1,6】之间的随机数,判断三个数之和是 小、中、大 
    //三个人压住,小、中、大 ,判断谁压对了
    //3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18

    //System.out.println(4>>2);
    Scanner sc = new Scanner(System.in);

    String zhangsan = "";
    String lisi = "";
    String wangwu = "";

    System.out.println("请张三下注(下注请输入:大、中、小):");
    zhangsan = sc.nextLine();
    System.out.println("请李四下注(下注请输入:大、中、小):");
    lisi = sc.nextLine();
    System.out.println("请王五下注(下注请输入:大、中、小):");
    wangwu = sc.nextLine();

    //调用方法,得到三个随机数的和
    int data = randomData();

    String result = "";
    if(data >= 3 && data <=8){ // 3<=data<=8 
        result = "小";
    }else if(data >= 9 && data <= 13){
        result = "中";
    }else {
        result = "大";
    }

    boolean flag = false;

    if(result.equals(zhangsan)){
        System.out.println("恭喜张三,押对了。。。。");
        flag = true;
    }

    if(result.equals(lisi)){
        System.out.println("恭喜李四,押对了。。。。");
        flag = true;
    }

    if(result.equals(wangwu)){
        System.out.println("恭喜王五,押对了。。。。");
        flag = true;
    }

    if(!flag){
        System.out.println("很遗憾,全猜错了。。。。");
    }

    System.out.println("点数是:"+data);
}

/*
 * 产生三个随机数的和
 */
static int randomData(){
    int sum = 0;

    for (int i = 0; i < 3; i++) {
        int a = (int)(Math.random()*6+1);
        sum += a;
    }

    return sum;
}

}
运行结果
请张三下注(下注请输入:大、中、小):

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值