Java習題練習(一)
阿新 • • 發佈:2018-11-10
輸出[1,100]內所有的質數,並統計
求斐波拉契數列的第十個數字大小System.out.println(1); int sum = 0; for(int a = 2; a < 101; a++) { int b = 2; while (b < a) { if (a % b == 0) { break; } b++; } if (a == b) { System.out.println(b); sum += 1; } } System.out.println("在[1,100]內,質數總數為:" + (sum+1));
public static void main(String[] args) {
int n =10;
System.out.println(func(n));
}
private static int func(int n) {
if (n == 1 || n== 2) {
return 1;
}
else {
return func(n-1) + func(n-2);
}
}
判斷三位數的各個位置,順便求個水仙花。
不是題目,熟悉一下方法呼叫for(int i = 100; i < 1001; i++) { int a = i / 100; int b = i / 10 % 10; int c = i % 10; if (i == a*a*a + b*b*b +c*c*c) { System.out.println(i); } }
集合裡面,隨機數生成:public static void main(String[] args) { String[] names = new String[8]; getName(names); printNames(names); String name = rdnames(names); System.out.println("___________________"); System.out.println(name); } public static void getName(String[] names) { names[0] = "A"; names[1] = "B"; names[2] = "C"; names[3] = "D"; names[4] = "E"; names[5] = "F"; names[6] = "G"; names[7] = "H"; } public static void printNames(String[] names) { for(int i = 0; i < names.length; i++) { System.out.println(names[i]); } } public static String rdnames(String[] names) { int index = new Random().nextInt(names.length); return names[index]; }
public static void main(String[] args) {
ArrayList<studentName> array = new ArrayList<studentName>();
add(array);
for( int i = 0; i < array.size(); i++) {
studentName s = array.get(i); //返回的是類!
System.out.println(s.name + " " + s.age);
}
Random rd = new Random();
studentName m = array.get(rd.nextInt(array.size()));
System.out.println(m.name + " " + m.age);
}
public static void add(ArrayList<studentName> array) {
studentName sn1 = new studentName();
studentName sn2 = new studentName();
studentName sn3 = new studentName();
studentName sn4 = new studentName();
studentName sn5 = new studentName();
sn1.name = "yi";
sn1.age = 1;
sn2.name = "er";
sn2.age = 2;
sn3.name = "san";
sn3.age = 3;
sn4.name = "si";
sn4.age = 4;
sn5.name = "wu";
sn5.age = 5;
array.add(sn1);
array.add(sn2);
array.add(sn3);
array.add(sn4);
array.add(sn5);
}
鬥地主打牌
public class Doudizhu {
public static void main(String[] args) {
//1. 組合牌
//建立Map集合,鍵是編號,值是牌
HashMap<Integer,String> pooker = new HashMap<Integer, String>();
//建立List集合,儲存編號
ArrayList<Integer> pookerNumber = new ArrayList<Integer>();
//定義出13個點數的陣列
String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
//定義4個花色陣列
String[] colors = {"♠","♥","♣","♦"};
//定義整數變數,作為鍵出現
int index = 2;
//遍歷陣列,花色+點數的組合,儲存到Map集合
for(String number : numbers){
for(String color : colors){
pooker.put(index, color+number);
pookerNumber.add(index);
index++;
}
}
//儲存大王,和小王
pooker.put(0, "大王");
pookerNumber.add(0);
pooker.put(1, "小王");
pookerNumber.add(1);
//洗牌,將牌的編號打亂
Collections.shuffle(pookerNumber);
//發牌功能,將牌編號,發給玩家集合,底牌集合
ArrayList<Integer> player1 = new ArrayList<Integer>();
ArrayList<Integer> player2 = new ArrayList<Integer>();
ArrayList<Integer> player3 = new ArrayList<Integer>();
ArrayList<Integer> bottom = new ArrayList<Integer>();
//發牌採用的是集合索引%3
for(int i = 0 ; i < pookerNumber.size() ; i++){
//先將底牌做好
if(i < 3){
//存到底牌去
bottom.add( pookerNumber.get(i));
//對索引%3判斷
}else if(i % 3 == 0){
//索引上的編號,發給玩家1
player1.add( pookerNumber.get(i) );
}else if( i % 3 == 1){
//索引上的編號,發給玩家2
player2.add( pookerNumber.get(i) );
}else if( i % 3 == 2){
//索引上的編號,發給玩家3
player3.add( pookerNumber.get(i) );
}
}
//對玩家手中的編號排序
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);
//看牌,將玩家手中的編號,到Map集合中查詢,根據鍵找值
//定義方法實現
look("A",player1,pooker);
look("B",player2,pooker);
look("C",player3,pooker);
look("底牌",bottom,pooker);
}
public static void look(String name,ArrayList<Integer> player,HashMap<Integer,String> pooker){
//遍歷ArrayList集合,獲取元素,作為鍵,到集合Map中找值
System.out.print(name+" ");
for(Integer key : player){
String value = pooker.get(key);
System.out.print(value+" ");
}
System.out.println();
}
}