java泛型循环break continue_循环结构练习(示例代码)
青鳥迷你游戲平臺開發
1.選擇游戲 ----- switch選擇結構
public class Test {
public static void main(String[] args) {
System.out.println("歡迎進入青鳥游迷你戲平臺");
Scanner input = new Scanner(System.in);
System.out.println("
請選擇您喜愛的游戲:
");
System.out.println("* * * * * * * * * * * * * * * * * * * * * ");
System.out.println(" 1.斗地主");
System.out.println(" 2.斗牛");
System.out.println(" 3.泡泡龍");
System.out.println(" 4.連連看");
System.out.println("* * * * * * * * * * * * * * * * * * * * * ");
System.out.print("
請選擇,輸入數字: ");
int num = input.nextInt();
switch (num) {
case 1:
System.out.println("您已進入斗地主房間!");
break;
case 2:
System.out.println("您已進入斗牛房間!");
break;
case 3:
System.out.println("您已進入泡泡龍房間!");
break;
case 4:
System.out.println("您已進入連連看房間!");
break;
case 5:
main(args);
break;
default:
System.out.println("對不起,您的輸入錯誤");
break;
}
}
}
2.玩游戲并晉級 ------ 多重if /? break
public class Jinji {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = 1;
int count = 0; //計數
int score = 0; //游戲成績
String answer;
System.out.println("青鳥迷你游戲平臺>游戲晉級
");
do {
System.out.print("您正在玩第" + n + "局,成績為:");
score = input.nextInt(); //錄入游戲得分
if (score > 80) { //統計80分以上的局數
count++;
}
n++;
if (n > 5) {
System.out.println("游戲結束");
} else {
System.out.print("繼續玩下一局嗎?(yes/no) ");
answer = input.next();
if (answer.equals("no")) {
System.out.println("您已經中途退出游戲");
break;
} else {
System.out.println("進入下一局");
}
}
} while (n <= 5);
double rate = count / 5.0; //計數達到80分以上的比率
if (n > 5) {
if (rate > 0.8) {
System.out.println("
恭喜通過一級");
} else if (rate > 0.6) {
System.out.println("
通過二級,繼續努力!");
} else {
System.out.println("
對不起,您未能晉級,繼續加油啊!");
}
} else {
System.out.println("
對不起,您未能晉級,繼續加油啊!");
}
}
}
3.玩游戲并支付游戲幣 ----if結構、continue語句
public class Count {
public static void main(String[] args) {
System.out.println("青鳥迷你游戲平臺>游戲幣支付");
Scanner input=new Scanner(System.in);
System.out.println("請選擇您玩的游戲類型: ");
System.out.println(" 1.牌類");
System.out.println(" 2.休閑競技類");
int num=input.nextInt();
System.out.println("請輸入您游戲時長: ");
int time=input.nextInt();
switch(num) {
case 1:
if (time>10) {
System.out.println("您玩的是牌類游戲,時長是:" + time + "小時,可以享受5折優惠,您需要支付" + (0.5 * time * 10) + "個游戲幣");
} else {
System.out.println("您玩的是牌類游戲,時長是:" + time + "小時,可以享受8折優惠,您需要支付"+ (0.8 * time * 10) + "個游戲幣");
}
break;
case 2:
if (time<= 10) {
System.out.println("您玩的是休閑競技類游戲,時長是:" + time + "小時,可以享受8折優惠,您需要支付" + (0.8 * time * 20) +"個游戲幣");
} else {
System.out.println("您玩的是休閑競技類游戲,時長是:" + time + "小時,可以享受5折優惠,您需要支付" + (0.5 * time * 20) + "個游戲幣");
}
break;
}
System.out.println("游戲結束");
}
}
4.添加用戶信息
public class User{
public static void main(String[] args) {
System.out.println("青鳥迷你游戲平臺>添加用戶信息
");
int zs ;//整數
int age ;//年齡
int score ;//積分
Scanner input = new Scanner(System.in);
System.out.println("請輸入要錄入用戶的數量`: ");
int count = input.nextInt();
for (int i = 0; i < count; i++) {
System.out.println("請輸入用戶編號(<4位整數>): ");
zs = input.nextInt();
if (zs<=9999&&zs>=1000) {
}else{
System.out.println("輸入有誤");
continue;
}
System.out.println("請輸入用戶年齡: ");
age = input.nextInt();
if (age <= 10 || age > 100) {
System.out.println("很抱歉,您的年齡不適宜玩游戲");
System.out.println("錄入信息失敗
");
continue;
}
System.out.println("請輸入會員積分: ");
score = input.nextInt();
System.out.println("您錄入的會員信息是:
");
System.out.println("用戶編號:" + zs + " 年齡:" + age
+ " 積分:" + score + "
");
}
}
}
注:靈活使用continue與break
總結
以上是生活随笔為你收集整理的java泛型循环break continue_循环结构练习(示例代码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java用的原码还是反码_java 原码
- 下一篇: hadoop的python框架指南_Py