Java作業 1104/1105

package org.westos.Homework;
import java.util.Scanner;

/**
 * 1:需求:請設計一個方法,可以實現獲取任意範圍內的隨機數。
 * */
public class Homework01 {
    public static void main(String[] args) throws NumberFormatException{
        Scanner sr = new Scanner(System.in);
        //做一個動態循環
        while(true) {
            //給用戶 輸入的規則
            System.out.println("請輸入一個範圍:(例如1-100)");
            String s = sr.nextLine();
            //正則表達式判斷
            String s1 ="[0-9]+\\-[0-9]+";
            if(s.matches(s1)) {
                //用分割功能取出用戶輸入的範圍
                String [] arrString = s.split("-");
                int min=Integer.parseInt(arrString[0]);
                int max=Integer.parseInt(arrString[1]);
                while(true) {
                    //取一個隨機數
                    int result = (int)(Math.random()*max);
                    if(result >= min) {
                        //如果該數在這個範圍裏 就輸入他並且結束當前循環
                        System.out.println(result);
                        break;
                    }
                }
                //如果結束上一個循環就退出外層循環
                break;
                //如果用戶輸入不正確則重新輸入
            }else {
                System.out.println("輸入錯誤請重新輸入");
            }
        }
    }
}

這裏寫圖片描述

2:下面代碼執行的結果是:
public static void main(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("hello");
        System.out.print(s1 == s2);
        System.out.print(",");
        System.out.println(s1.equals(s2));
    }
}
false,true
3:下面代碼執行的結果是:
public static void main(String arg[]) {
        StringBuffer a = new StringBuffer("A");
        StringBuffer b = new StringBuffer("B");
        operate(a, b);
        System.out.println(a + "," + b);
    }
    static void operate(StringBuffer x, StringBuffer y) {
        x.append(y);
        y = x;
    }
    AB,B
4:下面代碼執行的結果是
6、下列代碼的執行結果是:
    String str1 = "This is a test!";
    StringBuffer str2 =new StringBuffer( "This is a test!");
    str1 = str1+"Hi";
    str2.append("Hi");
    System.out.println("str1 == " + str1);
    System.out.println("str2 == " + str2);
    This is a test!Hi
    This is a test!Hi
7:下面代碼能最後打印的值是?
    public class TestValue {
    private static int a;

    public static void main(String[] args) {
        modify(a);
        System.out.println(a);
    }

    public static void modify(int a) {
        a++;
    }

}
c
A)編譯錯誤  B)null  C)0         D)1
package org.westos.Homework;

import java.util.Arrays;

/**
 * 獲取10個1-20之間的隨機數,要求不能重複
 * */
public class Homework02 {
    public static void main(String[] args) {
        //用Arrays裏的toString方法輸出該數組
        System.out.println(Arrays.toString(radNumbers()));
    }
    //定義一個獲取隨機數的方法
    public static int[] radNumbers() {
        //定義一個10長度的數組
        int [] arr = new int [10];
        for(int i=0; i<arr.length; i++) {
            //生成一個隨機數
            int ran = (int)(Math.random()*20+1);
            //如果該隨機數沒重複就添加進該數組
            if(ifRepeat(arr, ran)==false) {
                arr[i] = ran;
            }else {
                i=i-1;
            }
        }
        return arr;
    }
    //定義一個判斷是否重複的方法
    public static boolean ifRepeat(int[] arr , int result) {
        for(int i=0; i<arr.length; i++) {
            if(arr[i]==result) {
                return true;
            }
        }
        return false;
    } 
}

這裏寫圖片描述

package org.westos.Homework;
/**
 * 1:集合的嵌套遍歷
  需求:
        我們班有學生,每一個學生是一個對象。所以我們可以使用一個集合表示我們班級的學生。ArrayList<Student>
        但是呢,我們旁邊是不是還有班級,每個班級是不是也是一個ArrayList<Student>。
        而我現在有多個ArrayList<Student>。也要用集合存儲,怎麼辦呢?
 * */
import java.util.ArrayList;
import java.util.List;

public class Homework03 {
    public static void main(String[] args) {
        //定義一個大集合l, l用來儲存小集合l1 l2
        List l = new ArrayList();
        //l1 l2用來儲存學生的信息  l1儲存 s1 s2 l2儲存s3 s4 l1和l2就相當於班級
        List l1 = new ArrayList();
        List l2 = new ArrayList();
        Student s1 = new Student("流通",20);
        Student s2 = new Student("趙歡",19);
        Student s3 = new Student("老王",21);
        Student s4 = new Student("瑤瑤",22);
        l1.add(s1);
        l1.add(s2);
        l2.add(s3);
        l2.add(s4);
        l.add(l1);
        l.add(l2);
        //遍歷大集合中的元素
        for(int i=0; i<l.size(); i++) {
            //向下轉型  
            List ll = (List)l.get(i);
            for(int j=0; j<ll.size(); j++) {
                System.out.println(ll.get(j));
            }
        }
    }
}

這裏寫圖片描述

package org.westos.Homework;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

/**
 * 使用ArrayList集合存儲自定義對象並遍歷(三種方式去實現)
 * */
public class Homework04 {
    public static void main(String[] args) {
        List l = new ArrayList();
        l.add("流通");
        l.add("趙歡");
        l.add("瑤瑤");
        //第一種: toArray()方法
        Object [] obj = l.toArray();
        for(int i=0; i<obj.length; i++) {
            String s = (String)obj[i];
            System.out.println(s);
        }
        System.out.println("--------------------");
        //第二種使用迭代器 Iterator
        ListIterator li = l.listIterator();
        while(li.hasNext()) {
            String s = (String)li.next();
            System.out.println(s);
        }
        System.out.println("---------------------");
        //第三種 List集合中的get() size()方法
        for(int i=0; i<l.size(); i++) {
            String s = (String)l.get(i);
            System.out.println(s);
        }
    }
}

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章