关于java:cs61b-homework0java-basic-syntax

10次阅读

共计 835 个字符,预计需要花费 3 分钟才能阅读完成。

1. 所有的代码都须要在 class(类)外面实现

2. 你的程序最里面有一个大的 class 类,为了保障运行,你的 class 类名filename.java必须雷同。
比方

public class hellow{public static void triangle(){for(int i=1;i<10;i++){
            int temp = i;
            while(temp>0){System.out.print('*');
                temp--;
            }
            System.out.println();}
    }
    public static void main(String args[]){triangle();
    }
}

那么你的 filename 应该是 hellow.java

3. 当年申明一个函数,必须应用 public static 作为 signature, 比方下面代码中的 triangle 就是 public static int,而 main 函数是 public static void 型

4.System.out.println()输入会换行,System.out.print()输入不换行

5. 所有的变量都要有 static type 作为关键字, 比方 int ,String 等等,与 c ++ 雷同

6. 申明一个数组,并开拓空间,须要用到关键字 new,size 是数组大小
模板如下:

static type[] Arrayname;
Arrayname =new static type[size];

举例:

int[] a;
a=new int[4];

数组外面的元素赋值操作

int[] numbers = new int[3];
numbers[0] = 4;
numbers[1] = 7;
numbers[2] = 10;
System.out.println(numbers[1]);

或者在创立时间接赋值
int[] numbers = new int[]{4, 7, 10};
求数组的长度能够用.length,

int[] numbers = new int[]{4, 7, 10};
System.out.println(numbers.length);
正文完
 0