This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
宣告阵列 | |
int score[]; //宣告score为一个一维阵列 | |
score = new int[3]; //分配内存空间, 元素个数为3 | |
或 | |
int score[] = new int[3]; | |
或 | |
int score[] = {1,2,3}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Java常用方法大全 | |
字符串 | |
1、获取字符串的长度 | |
length() | |
2 、判断字符串的前缀或后缀与已知字符串是否相同 | |
前缀 startsWith(String s) | |
后缀 endsWith(String s) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1.变量可能未初始化 | |
错误提示:Abc.java:9: variable i might not have been initialized | |
System.out.println(i); | |
中文说明:变量i可能没有赋值就使用了。 | |
例子说明: | |
int i; | |
System.out.println(i); | |
2.变量重复定义 | |
错误提示:Abc.java:9: i is already defined in main(java.lang.String[]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
import javax.swing.JOptionPane; | |
public class TestNote { | |
public static void main(String [] args) { | |
NoteFrame noteframe = new NoteFrame(); | |
noteframe.setVisible(true); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List用法 | |
1.List可以添加任何对象, 包含自己定义的class | |
class Person{...} | |
class Test { | |
Person p1 = new Person(); | |
Person p2 = new Person(); | |
List list = new ArrayList(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this用法 | |
this可以用来区分局部变量与成员变量 | |
this只能用在方法的方法体内, 当new了一个物件之后, JVM会给该物件分配一个引用自身的指针, this就是这个指针, | |
this 只能在class的非静态方法中使用 | |
同一个class中的不同method有不同的this | |
this是指向对象本身的一个指针 | |
class Person { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Text1 { | |
public static void main(String [] args) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.print("請輸入月份 : "); | |
int mon = scanner.nextInt(); | |
Text1 t = new Text1(); | |
System.out.print("兔仔總數 :" + t.num(mon)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Text2 { | |
public static void main(String [] args) { | |
int startNum = 100; | |
int endNum = 200; | |
int sum = 0; | |
System.out.println(startNum + " 到 " + endNum + "的質數 : "); | |
for(int i = startNum ; i < endNum ; i++) { | |
int flag = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Text3 { | |
private static int num; | |
public static void main(String [] args) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.print("請輸入正整數 : "); | |
num = scanner.nextInt(); | |
System.out.print(num + " = "); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ResetEquals { | |
public static void main(String [] args) { | |
Person p1 = new Person("張三", 20); | |
Person p2 = new Person("張三", 20); | |
System.out.println(p1.equals(p2)? "是同一人" : "不是同一人"); | |
} | |
} | |
class Person { |