1. 程式人生 > >Scanner、Random、ArrayList的基礎編程練習題

Scanner、Random、ArrayList的基礎編程練習題

imp 循環 eth string ++ str 練習 amp ==

生成6個1~33之間的隨機整數,添加到集合,並遍歷

package ArrayList;

import java.util.ArrayList;
import java.util.Random;
/*
* 思路
* 6個--->6.fori
* 1~33之間--->xxx.nextInt(33)+1;
* 隨機整數--->Random xxx = new Random();
* 添加到集合--->ArrayList<Integer> xxx = new ArrayList<>(); list.add();
* 遍歷--->xxx.fori;--->list.get(索引);
*
* */
public class ArrayList_4 {
    public static void main(String[] args) {
        Random r = new Random();
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            int num = r.nextInt(33)+1;
            list.add(num);
        }
        System.out.println(list);
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i)+" ");
        }
    }
}

自定義4個學生對象,添加到集合,並遍歷

package ArrayList;

import Class_Objiect.Student;
import java.util.ArrayList;
/*
* 思路
* 自定義4個學生對象--->Student one = new Student();
*                   Student two = new Student();
*                   Student three = new Student();
*                   Student four = new Student();
* 添加到集合--->ArrayList<Student> list = new ArrayList<>(); &  list.add(內容);
* 遍歷--->list.fori--->list.get(index);
* */
public class ArrayList_5 {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<Student>();
        Student one = new Student("Andy",18);
        Student two = new Student("Bill",19);
        Student three = new Student("Lily",32);
        Student four = new Student("Jane",12);
        list.add(one);
        list.add(two);
        list.add(three);
        list.add(four);
        for (int i = 0; i < list.size(); i++) {
            System.out.println("姓名:"+list.get(i).getName()+"  "+"年齡:"+list.get(i).getAge()+"歲");
        }
    }
}

定義以指定格式打印集合的方法(ArrayList類型作為參數),使用{}擴起集合,使用@分隔每個元素。格式參照 {元素@元素@元素}。

package ArrayList;

import java.util.ArrayList;

/*
* 思路
* ArrayList<數據類型> xxx = new ArrayLIstanbul<>();
* */
public class ArrayList_6 {
    public static void main(String[] args) {
        //創建集合
        ArrayList<Integer> list = new ArrayList<>();
        //添加元素
        list.add(1);
        list.add(3);
        list.add(5);
        list.add(7);
        //調用方法
        print(list);
    }
    public static void print(ArrayList<Integer> list) {
        System.out.print("{");//打印  {
        for (int i = 0; i < list.size(); i++) {
            if (i == list.size() - 1) {
                System.out.print(list.get(i));//打印數字
            } else {
                System.out.print(list.get(i) + "@");//打印  "數字@"
            }
        }
        System.out.println("}");//打印 }
    }
}

定義1~30之間(包括1和30),所有偶數元素集合的方法(ArrayList類型作為返回值)

package ArrayList;

import java.util.ArrayList;
import java.util.Random;

/*
* 思路
* 1-30之間--->xxx.nextInt(30)+1;
* 所有偶數元素--->if(i%2==0){偶數}
* 方法 public static String method(ArrayList<String> list)
* */
public class ArrayList_7 {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 1; i <=30; i++) {
            if(i%2==0){
                list.add(i);
            }
        }
        System.out.println(list);
    } 
}

遊戲開始時,會隨機生成一個1-100之間的整數 number 。玩家猜測一個數字guessNumber ,會與 number 作比 較,系統提示大了或者小了,直到玩家猜中,遊戲結束

package Random;
/*思路
隨機--->Random r = new Random();
1~100-->int number = r.nextInt(100)+1;
猜測 --->Scanner sc = new Scanner(System.in);--->int guessNumber = sc.nextInt();
if(guessNumber>number){大了}
if(guessNumber<number){小了}
if(guessNumber==number){猜對了}
guessNumber應該是一個循環;
* */
import java.util.Random;
import java.util.Scanner;

public class Random_4 {
    public static void main(String[] args) {
        Random r = new Random();
        Scanner sc = new Scanner(System.in);
        int number =r.nextInt(100)+1;
        System.out.println("請輸入:");
        for (int i = 0; i <100 ; i++) {
            int guessNumber = sc.nextInt();
          if(guessNumber>number){
              System.out.println("大了");
          }else if(guessNumber<number){
              System.out.println("小了");
            }else if(guessNumber==number){
              System.out.println("猜對了");
                }
            }
        }
    }

Scanner、Random、ArrayList的基礎編程練習題