zoukankan      html  css  js  c++  java
  • 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);
    }
    }

  • 相关阅读:
    Swagger UI教程 API 文档神器 搭配Node使用 web api 接口文档 (转)
    C#测试web服务是否可用(转)
    使用Fiddler测试WebApi接口
    .Net 序列化(去除默认命名空间,添加编码)
    Win10 下Cisco AnyConnect Secure Mobility Client问题(转)
    jQuery.extend 函数详解(转)
    Oracle 取Group By 第一条
    EFProf用法
    c#和JS数据加密(转)
    C#中字符数组,字节数组和string之间的转化(转)
  • 原文地址:https://www.cnblogs.com/kangzhijia/p/5966067.html
Copyright © 2011-2022 走看看