A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

Za124123

初级黑马

  • 黑马币:

  • 帖子:

  • 精华:

© Za124123 初级黑马   /  2019-8-19 18:16  /  1695 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

身为一个小白对Jave也写不出什么高深的技术贴,所以只能将这一段时间自己敲的代码,写的案例给大家分享分享,这些案例都比较适合小白,我相信在解题的过程中你会就慢慢的爱上java。

1.        已知2019年是猪年,请在控制台输出从1949年到2019年中所有是猪年的年份。

代码展示:
public class Work1{
   public static void main(String[] args){
      int pig =2031;
      for(int i=2019; i>=1949;i--){
         pig -=12;
         System.out.println(pig + "是猪年");
      }
   }
}
2.有一个输出语句System.out.print("@")。使用这个语句,在控制台打印出一个四行五列的长方形,
// 效果如下://使用for循环的嵌套完成。

// @@@@@
// @@@@@
// @@@@@
// @@@@@

代码展示:
public class Work1{
      public static void main(String[] args){
         for(int hang=1;hang<5;hang++){
            for(int lie=1;lie<6;lie++){
               System.out.print("@");
            }
            System.out.println();
         }
      }
   }

3. 有一个输出语句System.out.print("@")。使用这个语句,在控制台打印出一个五行的三角形,效果如下:
// @
// @@
// @@@
// @@@@
// @@@@@
代码展示:
public static void main(String[] args) {
     for(int i=1;i<6;i++){
         for (int j=0;j<i;j++){
             System.out.print("@");
         }
         System.out.println();
     }

4. 小明手中有一张10元纸币。需要到商店兑换零钱。商店只有1元纸币和5角硬币。那么请列举出所有的兑换方式。打印效果如下:
//换1元纸币1张,5角硬币18个
//换1元纸币2张,5角硬币16个
//换1元纸币3张,5角硬币14个
//...   ...
//换1元纸币9张,5角硬币2个

代码展示:
public static void main(String[] args) {
    for(int i=1;i<10;i++){
        System.out.println(i + "张一元纸币," + (10-i)*2 + "个五毛");
        }
    }
5. 中国有闰年的说法。闰年的规则是:四年一闰,百年不闰,四百年再闰。
// (年份能够被4整除但不能被100整除算是闰年,年份能被400整除也是闰年)。请打印出1988年到2019年的所有闰年年份。

代码展示:
public static void main(String[] args) {
    int year = 1988;
    while (year<2020){
       if(year%4==0 && year%100!=0 || year%400==0 ){
           System.out.println(year+"是闰年");
            }
        year++;
    }
}
6. 有一个容量为10L的空水桶。水桶的上面开始往里灌水,同时下面开始往出流水。第一分钟灌水的速度是1L/min,第
// 二分钟灌水的速度是2L/min,第三分钟灌水的速度是3L/min,以此类推。而流水的速度固定是3L/min。那么几分钟之后,
// 水桶里能保持灌满水的状态?

代码展示:
public static void main(String[] args) {
    int temp=0; //现有水量
    int count =0; //计算分钟
    while (temp<10){
        count++;
        temp =temp + count -3;
        if(temp <0){
            temp =0; //前三分钟 桶里都是无水的
        }
    }
    System.out.println(count);
}

7. 定义 int a = 5  b = 3   c = 8
//1、利用if语句获取最小值打印
//2、利用三元运算符获取最大值打印

代码展示:
public static void main(String[] args) {
    int a = 5, b = 3, c = 8;*/
//第①个
    /*int temp = a < b ? a : b;
    int min = temp < c ? temp : c;
    System.out.println("最小值为:" + min);*/
//第②个
    /*if (a < b && a < c) {
        System.out.println("最小值为:" + a);
    } else if (b < a && b < c) {
        System.out.println("最小值为:" + b);
    } else {
        System.out.println("最小值为:" + c);
    }
}

8. 获取10~99(包含10和99)的“总和”与“偶数”的个数,并在控制台打印输出

代码展示:
public static void main(String[] args) {
     int count = 0;
     for (int i = 10; i <= 99; i++) {
         if (i % 2 == 0){
             System.out.println(i);
         }
         count++;
     }
     System.out.println("10~99共有" + count + "个");
}

9. 一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在
// 第10次落地时,共经过多少米?第10次反弹多高?

代码展示:
public static void main(String[] args) {
     double pass = 0 ;
     double height = 100;
     for (int i = 1; i <= 10; i++) {
            pass += height +height/2;
            height =height/2;
            if(i==10){
                pass=pass-height;
            }
         }
     System.out.println("共经过" + pass + "第十次反弹高度为:" + height );
     }

10. 在数学中10!代表10的阶乘。既代表1*2*3*4....*10;
//现在要求编程求出8!。

代码展示:
public static void main(String[] args) {
    int sum = 1;
    for(int i = 1; i < 9; i++){
        sum *=i ;
    }
    System.out.println("resault:" + sum);
}

11. 有1、2、3、4四个数字,能组成多少个互不相同且无重复数字的三位数?把这些数都输出出来,并且输出总共的个数。

代码展示:
public static void main(String[] args){
    for (int i = 0; i <4 ; i++) {
        for (int j = 0; j <4 ; j++) {
            for (int k = 0; k <4 ; k++) {
                if(i!=j && i!=k && j!=k){
                    System.out.println(i*100+j*10+k);
                }
            }
        }
    }
}

12. 分析以下需求,并用代码实现:在控制台输出九九乘法表

代码展示:
public static void main(String[] args) {
    for(int i = 1; i < 10; i++ ){
        for(int j = 1; j <=i; j++){
            System.out.print(i + " * " + j + " = " + (i*j) + "\t");
        }
        System.out.println();
    }
}

13. 析以下需求,并用代码实现:
//
// *
// **
// ***
// ****
// *****

代码展示:
public static void main(String[] args) {
     for (int i = 1; i < 6; i++) {
         for (int j = 0; j < i; j++) {
             System.out.print("*");
         }
         System.out.println();
     }
}

14.分析以下需求,并用代码实现:
//  *   
// ***   
//*****

代码展示:
public static void main(String[] args) {
    for (int i=1 ;i<6;i++){
        for (int j=0; j<i;j++){
            if(i%2==0){
                continue;
            }
                System.out.print("*");
        }
        System.out.println();
    }
       }

15. 键盘录入任意一个整数,程序能输出这个数的位数。例如输入123   输出:123是一个3位数

代码展示:
public static void main(String[] args) {
     Scanner sc = new Scanner(System.in);
     int number = sc.nextInt();
     int number1 =number;
     int count = 1;
     while (number>0){
         if(number<10){
             System.out.println(number1 + "是"+ count+"位数");
             break;
         } else if (number >10){
             number /=10;
             count++;
         }
     }
}

16.键盘录入任意一个整数,程序能输出这个数的位数。例如输入123   输出:123是一个3位数

代码展示:
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        int number1 = number;
        int count = 1;
        while (number > 0) {
            if (number < 10) {
                System.out.println(number1 + "是" + count + "位数");
                break;
            } else if (number > 10) {
                number /= 10;
                count++;
            }
        }
    }
17.要求输出1到10000中所有既能被3整除又能被7整除的偶数
  
代码展示:  
public static void main(String[] args) {
        for (int i = 1; i <= 10000; i++) {
           if(i%3==0 && i%7==0){
               System.out.println(i);
           }
        }
    }、
    // 第十九题:要求求出1到100中所有既能被3整除又能被7整除的偶数的和sum1,以及十位
    // 数为7并且能被3整除的所有的奇数的和sum2。要求最后输出sum1和sum2的差值

    /*public static void main(String[] args) {
        int sum1 = 0;
        int sum2 = 0;
        for (int i = 1; i <= 101; i++) {
            if (i % 3 == 0 && i % 7 == 0 && i % 2 == 0) {
                sum1 += i;
            }
            if (i / 10 % 10 == 7 && i % 3 == 0 && i % 2 != 0) {
                sum2 += i;
            }
        }
        System.out.println("sum1:"+ sum1 + " sum2:" + sum2);
    }*/
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马