• java问题随笔


    1. 类的对象实例化
    如何不加static来调用函数
    2. 如何用当前时间来生成随机数

    3.GitHab账号
    1. java中如何不加static来调用函数?

    加static: 表示这个方法为静态方法,在其它类中可以直接通过类名去调用这个方法。

    例如
    public static void main(String[] args){
    ClassName.prt("abc");
    }
    如果不加static,则只有通过该类的去调用。
    例如
    public static void main(String[] args){
    ClassName name=new ClassName();
    name.prt("abc");
    }

    2.编写一个方法,随机生成1000个数

    for(int i = 0;i < 1000;i++)
    {int c = (int)(Math.random() * 1000);
    System.out.println(c);}

    3.如何用当前时间来生成随机数

    public class RandomDemo {

    public static void main(String[] args) {
    long t = System.currentTimeMillis();
    Random rd = new Random(t);
    System.out.println(rd.nextInt());
    }
    4.杨辉三角
    public class YH
    {
    public static void main(String agrs[])
    {
    int a[5][5],i,j;
    for(i = 0;i < 5 ;i++)
    {
    for(j = 0;j < i;j++)
    {
    if(i == j || j == 1) a[i][j] = 1;
    else
    a[i][j] = a[i][j-1] + a[i-1][j-1];
    System.out.print(a[i][j]);
    }
    System.out.print(' ');
    }
    }
    5。组合数

    public class AssociationTest {
    public static void main(String[] args) {
    int[] num = new int[] { 1, 2, 3, 4, 5 };
    String str = "";
    // 求3个数的组合个数
    count(0, str, num, 3);
    // 求1-n个数的组合个数
    countAll(0, str, num);
    }

    public static void countAll(int i, String str, int[] num) {
    if (i == num.length) {
    System.out.println(str);
    return;
    }
    countAll(i + 1, str, num);
    countAll(i + 1, str + num[i] + ",", num);
    }

    public static void count(int i, String str, int[] num, int n) {
    if (n == 0) {
    System.out.println(str);
    return;
    }
    if (i == num.length) {
    return;
    }
    count(i + 1, str + num[i] + ",", num, n - 1);
    count(i + 1, str, num, n);
    }
    }

  • 相关阅读:
    default.js 下的 setPromise(WinJS.UI.processAll());
    选择排序
    插入排序
    16、css实现div中图片占满整个屏幕
    21、解决关于 vue项目中 点击按钮路由多了个问号
    15、vue项目封装axios并访问接口
    17、在vue中引用移动端框架Vux:
    24、vuex刷新页面数据丢失解决办法
    18、git提交代码并将develop分支合并到master分支上
    20、解决Vue使用bus兄弟组件间传值,第一次监听不到数据
  • 原文地址:https://www.cnblogs.com/kangzhijia/p/5966067.html
Copyright © 2020-2023  润新知