販賣機版
public class Method01 {
int drink;//品名
int num;//數量
int sum;//小計
int total;//總計
Scanner sc = new Scanner(System.in);
//點餐(無返回值)
void order() {
System.out.println("請選擇飲料:1[咖啡]2[奶茶]3[果汁]");
drink = sc.nextInt();
System.out.println("請選擇數量:");
num = sc.nextInt();
money1();//執行其他方法
}
//點餐小計
void money1() {
if(drink==1) {
sum = 150*num;
System.out.println("咖啡總計= "+sum);
}
else if(drink==2) {
sum = 80*num;
System.out.println("奶茶總計= "+sum);
}
else if(drink==3) {
sum = 100*num;
System.out.println("果汁總計= "+sum);
}
total+=sum;
}
public static void main(String[] args) {
Method01 m1 = new Method01();
m1.order();
System.out.println("本次消費共計:"+m1.total+"元, 謝謝。");
}
}
猜數字
public class Game {
//成員變數
int input;
int ans;
//成員方法
void keyin() {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入0-99間任一整數");
input = sc.nextInt();
check();
}
void check() {
if(input==ans) {
System.out.println("猜對了, 好棒~~");
}
else if(input>ans) {
System.out.println("數字太大,請重猜!");
keyin();
}
else {
System.out.println("數字太小,請重猜!");
keyin();
}
}
public static void main(String[] args) {
//先建立物件
Game game = new Game();
//產生亂數答案
game.ans =(int)(Math.random()*100);
//呼叫keyin()
game.keyin();
}
}
沒有留言:
張貼留言