標題:
[隨堂練習]例外處理 (四)
[打印本頁]
作者:
周政輝
時間:
2018-10-13 13:53
標題:
[隨堂練習]例外處理 (四)
對 [作業] 猜數字遊戲 的程式碼, 進行例外處理,
使程式執行時, 萬一使用者不小心將0打成o時, 遊戲得以繼續進行.
原執行狀況:
[attach]5022[/attach]
進行例外處理後:
[attach]5023[/attach]
作者:
王駿愷
時間:
2018-10-13 14:29
package mhjhmtc;
import java.util.Scanner;
public class Mhjhmtc {
public static void main(String[] args) {
// TODO 自動產生的方法 Stub
int answer = (int) (Math.random() * 99 + 1);
int low = 1;
int high = 100;
System.out.println("請輸入" + low + "~" + high + "的數");
int times = 0;
int guess=0;
while (true) {
try {
Scanner scanner = new Scanner(System.in);
guess = scanner.nextInt();
}
catch (Exception ex) {
guess=0;
}
if (guess < answer) {
low = guess;
System.out.println("數字太小了喔");
System.out.println("請輸入" + low + "~" + high + "的數");
times++;
}
else if (guess > answer) {
high = guess;
System.out.println("數字太大了喔");
System.out.println("請輸入" + low + "~" + high + "的數");
times++;
}
else {
System.out.println("答對了");
System.out.println("總共達對了" + times);
}
}
}
}
複製代碼
作者:
吳秉翰
時間:
2018-10-13 14:33
package bn.tw;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO 自動產生的方法 Stub
int ans = (int) (Math.random() * 99 + 1);
System.out.println("猜1~99的數字:");
int small = 1;
int big = 99;
int total = 1;
int guess = 0;
while (true) {
try {
Scanner scanner = new Scanner(System.in);
guess = scanner.nextInt();
} catch (Exception e) {
System.out.println("重新輸入");
guess = 1;
total--;
}
if (guess > ans) {
System.out.println("太大");
System.out.println(small + "~" + guess);
big = guess;
total++;
} else if (guess < ans) {
System.out.println("太小");
System.out.println(guess + "~" + big);
small = guess;
total++;
} else {
System.out.println("GOTCHA!");
System.out.println("一共猜了" + total + "次");
break;
}
}
}
}
複製代碼
作者:
黃安立
時間:
2018-10-13 14:34
[/code][code]
package 顏詢大笨蛋1;
import java.sql.Date;
import java.time.LocalDateTime;
import java.util.Scanner;
public class 顏詢大笨蛋2 {
// 一個專案只會有一個(程式進入點)
public static void main(String[] args){
int answer = (int) (Math.random()*99+1);
System.out.println(answer);
System.out.println("猜一個1~99以內的數字:(顏詢的智商)");
int temp = 99;
int count =1;
int guess =0;
while(true) {
try{
Scanner scanner = new Scanner(System.in);
guess = scanner.nextInt();
}
catch(Exception ex)
{
guess=0;
}
if(guess >answer) {
System.out.println("沒那麼多");
System.out.println("猜一個1~"+ guess +"以內的數");
temp = guess;
count++;
}
else if(guess < answer) {
System.out.println("沒那麼少");
System.out.println("猜一個"+ guess +"~"+temp+"以內的數");
count++;
}
else {
System.out.println("猜對了!");
System.out.println("一共猜了"+ count+"次");
break;
}
}
}
}
複製代碼
作者:
湯東緯
時間:
2018-10-13 14:41
package main;
import java.util.Scanner;
public class Main
{
public static void main(String[] args) throws MyException
{
int times=0;
int small=1;
int big=99;
int num=(int)(Math.random()*99+1);
Scanner scanner=new Scanner(System.in);
int guess=0;
while(true)
{
try
{
System.out.println("猜1個介於"+small+"到"+big+"的數:");
guess=scanner.nextInt();
}
catch(Exception ex)
{
guess=0;
}
if(guess==num){
System.out.println("猜對了");
break;
}
else if(guess>num){
System.out.println("猜得太大了");
big=guess;
}else{
System.out.println("猜的太大了");
small=guess;
}
}
System.out.println("猜了"+times+"次");
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2