數(shù)組惩阶、 類
目的
更好的掌握java語言數(shù)組的運(yùn)用挎狸,同時對類有一個清晰的概念。了解什么是對象和創(chuàng)建對象断楷。
技術(shù)
1.數(shù)組的定義和數(shù)組的使用
2.類的定義
3.創(chuàng)建對象
4.方法的定義
技術(shù)運(yùn)行
- 數(shù)組的定義
//數(shù)組一旦被定義 大小就確定了锨匆,無法更改
int[] score = {1, 2, 3}; //靜態(tài)
String[] names = {"jack", "merry"};
//動態(tài) 內(nèi)容動態(tài)添加
float[] height = new float[5];
height[0] = 165.5f;
height[1] = 170f;
- 數(shù)組的使用
用數(shù)組輸出一組隨機(jī)數(shù)
//隨機(jī)數(shù)
Random r = new Random();
r.nextInt(100);
//盡量不要擴(kuò)大變量的作用域
int[] count = new int[10];
for (int i = 0; i < 10; i++) {
count[i] = r.nextInt(100);
}
//數(shù)組輸出方式1
for (int i = 0;i<10;i++){
System.out.print(count[i]+" ");
}
System.out.println();
//數(shù)組輸出方式2
for (int temp : count) {
System.out.print(temp + " ");
}
System.out.println();
//數(shù)組輸出方式3
System.out.println(Arrays.toString(count));
- 定義一個類
class Person{
String name;
int age;
float score;
private String password;
};
- 創(chuàng)建Person類的一個對象
Person xw = new Person(); - 給這個對象對應(yīng)的屬性賦值
xw.name = "小王";
xw.age = 20;
xw.score = 86.5f;
System.out.println(xw.name);
System.out.println(xw.age);
System.out.println(xw.score);
- 方法的使用
調(diào)用誰(哪個對象)的方法
在類里面可以直接調(diào)用自己的方法
在外部必須通過對象來調(diào)用 - 方法的定義
// 修飾符 public static final private
public void test(){
System.out.println("沒有返回值,沒有參數(shù)的方法");
}
public void test2(){
//在自己的類里面可以調(diào)用這個類里面的所有資源
// this.test();
test();
}
//沒有返回值 接收一個參數(shù)
public void test3(String name){
}
//有一個參數(shù)
public int test4(String name){
return 0;
}
//有多個參數(shù) 每個參數(shù)用逗號隔開
public int test5(String name,int age){
return 0;
}
//可變參數(shù) 相當(dāng)于數(shù)組
public int test6(int ... counts){
int sum = 0;
for (int i = 0;i<counts.length;i++){
sum += counts[i];
}
return sum;
}
- 猜數(shù)字游戲
//猜數(shù)字游戲
class GuessNumber{
public static void main(String[] args){
//保存原始數(shù)字
int[] org = new int[4];
//保存用戶猜測的數(shù)字
int[] guess = new int[4];
//產(chǎn)生四個隨機(jī)數(shù)
Random random = new Random();
for (int i = 0; i<4;i++){
//判斷是否重復(fù)
int temp = random.nextInt(10);
//判斷是否存在
boolean result= contains(org,temp);
if(result){
//確保i對應(yīng)的位置能夠得到一個不重復(fù)的數(shù)字
i--;
}else{
org[i] = temp;
}
}
//排序
Arrays.sort(org);
System.out.println(Arrays.toString(org));
//定義一個scanner掃描儀對象
Scanner scanner = new Scanner(System.in);
//開始游戲
while (true) {
int countA = 0;
int countB = 0;
System.out.print("請輸入猜測的數(shù)字");
for (int i = 0; i < 4; i++) {
guess[i] = scanner.nextInt();
}
//判斷用戶輸入
// 1.判斷是否存在
for (int i = 0; i < 4; i++) {
boolean result = contains(guess, org[i]);
if (result == true){
//數(shù)字存在
//判斷位置是相同
int index = indexOf(guess,org[i]);
if(index == i){
//數(shù)字和位置都相同
countA++;
}else{
countB++;
}
}
}
if (countA == 4){
System.out.println("恭喜答對6病恐锣!");
break;
}else{
System.out.println(countA+"A"+countB+"B");
}
}
}
public static boolean contains(int[] val,int obj){
for (int i = 0;i<val.length;i++){
if (val[i] == obj){
//重復(fù)了
return true;
}
}
return false;
}
/**
* 在一個數(shù)組里面查找某個對象的索引值
* @param val 數(shù)組
* @param obj 查找的對象
* @return 索引值
*/
public static int indexOf(int[] val,int obj){
for (int i = 0;i<val.length;i++){
if (val[i] == obj){
return i;
}
}return -1;
}
}
心得
通過兩天的學(xué)習(xí),總算對java有了一個初步的認(rèn)識舞痰,應(yīng)該能夠運(yùn)用java語言寫一些簡單的小demo了土榴。不過在寫代碼的時候還是不太習(xí)慣,沒有用C語言那么順暢匀奏。今天在寫代碼的時候鞭衩,盡量使用了快捷鍵学搜,相信我會越來越熟練的。