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) {
System.out.println(Arrays.toString(radNumbers()));
}
public static int[] radNumbers() {
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) {
List l = new ArrayList();
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("瑤瑤");
Object [] obj = l.toArray();
for(int i=0; i<obj.length; i++) {
String s = (String)obj[i];
System.out.println(s);
}
System.out.println("--------------------");
ListIterator li = l.listIterator();
while(li.hasNext()) {
String s = (String)li.next();
System.out.println(s);
}
System.out.println("---------------------");
for(int i=0; i<l.size(); i++) {
String s = (String)l.get(i);
System.out.println(s);
}
}
}