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('\n');
}
}
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);
}
}

posted on 2016-10-16 10:27  zhijia  阅读(82)  评论(0编辑  收藏  举报

导航