返回列表 發帖

[隨堂練習]例外處理 (四)

對 [作業] 猜數字遊戲 的程式碼, 進行例外處理,
使程式執行時, 萬一使用者不小心將0打成o時, 遊戲得以繼續進行.

原執行狀況:

進行例外處理後:

附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. package main;

  2. import java.util.Scanner;

  3. public class Main
  4. {
  5.         public static void main(String[] args) throws MyException
  6.         {
  7.                 int times=0;
  8.                 int small=1;
  9.                 int big=99;
  10.                 int num=(int)(Math.random()*99+1);
  11.                 Scanner scanner=new Scanner(System.in);
  12.                 int guess=0;
  13.                 while(true)
  14.                 {
  15.                     try
  16.                     {
  17.                            System.out.println("猜1個介於"+small+"到"+big+"的數:");
  18.                     guess=scanner.nextInt();
  19.                     }   
  20.                         catch(Exception ex)
  21.                         {
  22.                                 guess=0;
  23.                         }
  24.                        
  25.                        
  26.                         if(guess==num){
  27.                                 System.out.println("猜對了");
  28.                                 break;
  29.                         }
  30.                         else if(guess>num){
  31.                                 System.out.println("猜得太大了");
  32.                                 big=guess;
  33.                         }else{
  34.                                 System.out.println("猜的太大了");
  35.                                 small=guess;
  36.                         }                                       
  37.                 }
  38.                 System.out.println("猜了"+times+"次");
  39.         }
  40. }
複製代碼

TOP

  1. [/code][code]
  2. package  顏詢大笨蛋1;

  3. import java.sql.Date;
  4. import java.time.LocalDateTime;
  5. import java.util.Scanner;
  6. public class  顏詢大笨蛋2 {

  7.         // 一個專案只會有一個(程式進入點)
  8.         public static void main(String[] args){
  9.                
  10.                 int answer = (int) (Math.random()*99+1);
  11.                 System.out.println(answer);
  12.                 System.out.println("猜一個1~99以內的數字:(顏詢的智商)");
  13.                 int temp = 99;
  14.                 int count =1;
  15.                 int guess =0;
  16.                 while(true) {
  17.                         try{
  18.                         Scanner scanner = new Scanner(System.in);
  19.                         guess = scanner.nextInt();
  20.                         }
  21.                         catch(Exception ex)
  22.                         {
  23.                                 guess=0;
  24.                         }
  25.                         if(guess >answer) {
  26.                                 System.out.println("沒那麼多");
  27.                                 System.out.println("猜一個1~"+ guess +"以內的數");
  28.                                 temp = guess;
  29.                                 count++;
  30.                         }
  31.                         else if(guess < answer) {
  32.                                 System.out.println("沒那麼少");
  33.                                 System.out.println("猜一個"+ guess +"~"+temp+"以內的數");
  34.                                 count++;
  35.                         }
  36.                         else {
  37.                                 System.out.println("猜對了!");
  38.                                 System.out.println("一共猜了"+ count+"次");
  39.                                 break;
  40.                                 
  41.               
  42.                         }
  43.                         
  44.                 }
  45.                
  46.                
  47.                
  48.                
  49.         }

  50. }
複製代碼

TOP

  1. package bn.tw;

  2. import java.util.Scanner;

  3. public class Main {

  4.         public static void main(String[] args) {
  5.                 // TODO 自動產生的方法 Stub
  6.                 int ans = (int) (Math.random() * 99 + 1);
  7.                 System.out.println("猜1~99的數字:");
  8.                 int small = 1;
  9.                 int big = 99;
  10.                 int total = 1;
  11.                 int guess = 0;
  12.                 while (true) {
  13.                         try {
  14.                                 Scanner scanner = new Scanner(System.in);
  15.                                 guess = scanner.nextInt();
  16.                         } catch (Exception e) {
  17.                                 System.out.println("重新輸入");
  18.                                 guess = 1;
  19.                                 total--;
  20.                         }

  21.                         if (guess > ans) {
  22.                                 System.out.println("太大");
  23.                                 System.out.println(small + "~" + guess);
  24.                                 big = guess;
  25.                                 total++;
  26.                         } else if (guess < ans) {
  27.                                 System.out.println("太小");
  28.                                 System.out.println(guess + "~" + big);
  29.                                 small = guess;
  30.                                 total++;
  31.                         } else {
  32.                                 System.out.println("GOTCHA!");
  33.                                 System.out.println("一共猜了" + total + "次");
  34.                                 break;
  35.                         }
  36.                 }
  37.         }
  38. }
複製代碼

TOP

  1. package mhjhmtc;

  2. import java.util.Scanner;

  3. public class Mhjhmtc {
  4.         public static void main(String[] args)  {
  5.                 // TODO 自動產生的方法 Stub
  6.                 int answer = (int) (Math.random() * 99 + 1);
  7.                 int low = 1;
  8.                 int high = 100;
  9.                 System.out.println("請輸入" + low + "~" + high + "的數");
  10.                 int times = 0;
  11.                 int guess=0;
  12.                 while (true) {
  13.                         try {
  14.                                 Scanner scanner = new Scanner(System.in);
  15.                         guess = scanner.nextInt();
  16.                         }
  17.                         catch (Exception ex) {
  18.                   guess=0;
  19.                         }
  20.                         if (guess < answer) {
  21.                                 low = guess;
  22.                                 System.out.println("數字太小了喔");
  23.                                 System.out.println("請輸入" + low + "~" + high + "的數");
  24.                                 times++;
  25.                         }
  26.                         else if (guess > answer) {
  27.                                 high = guess;
  28.                                 System.out.println("數字太大了喔");
  29.                                 System.out.println("請輸入" + low + "~" + high + "的數");
  30.                                 times++;
  31.                         }
  32.                        
  33.                         else {
  34.                                 System.out.println("答對了");
  35.                                 System.out.println("總共達對了" + times);
  36.                         }
  37.                 }
  38.         }
  39. }
複製代碼

TOP

返回列表