每天中午都不知要吃什麼好,乾脆將常吃的幾家餐廳統計一下,用簡單的程式來博杯今天要吃哪一家吧!!
package com.alan;
import java.util.Random;
import java.io.*;
public class LunchRandon{
private static void riceChoiceRandom(int a){
String[] riceArray = {"A","B","C","D","E"};
System.out.println(riceArray[a]);
}
private static void noodleChoiceRandom(int a){
String[] noodleArray = {"A","B","C"};
System.out.println(noodleArray[a]);
}
/**
*args1 : 飯、麵分類選擇
*args2 : 飯、麵目前登錄於程式中的餐廳
*/
public static void getRandom(int lunchCategory){
Random r = new Random();
int n = (int)(10.0 * Math.random());
int limit = 0;
if(lunchCategory == 1){
limit = 5;
}else if(lunchCategory == 2){
limit = 3;
}
if(n < limit){
if(lunchCategory == 1){
LunchRandon.riceChoiceRandom(n);
}else{
LunchRandon.noodleChoiceRandom(n);
}
}else{
LunchRandon.getRandom(lunchCategory);
}
}
public static void main(String[] args){
try{
System.out.println("吃飯還是吃麵 ??");
System.out.println("吃飯: 1 ; 吃麵 : 2");
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
int lunchCategory = Integer.parseInt(buf.readLine());
LunchRandon.getRandom(lunchCategory);
}catch(Exception e){
System.err.println(e.toString());
}
}
}