標題:
例外處理 (三)
[打印本頁]
作者:
tonyh
時間:
2020-9-25 20:26
標題:
例外處理 (三)
本帖最後由 tonyh 於 2020-9-25 20:50 編輯
對
猜數字遊戲
的程式碼,進行例外處理,
使程式執行時,萬一使用者不小心將0打成o時,遊戲得以繼續進行。
原執行狀況:
[attach]9305[/attach]
進行例外處理後:
[attach]9306[/attach]
本帖隱藏的內容需要回復才可以瀏覽
作者:
孫嘉駿
時間:
2020-9-25 20:46
import java.util.Scanner;
public class Ch01 {
public static void main(String[] args)
{
int x=(int)(Math.random()*99+1);
int y, n=0;
int a=1, b=99;
while(true)
{
try
{
Scanner s=new Scanner(System.in);
n++;
System.out.print("猜一個"+a+"~"+b+"之間的數: ");
y=s.nextInt();
if(y<x)
{
System.out.println("太小了!");
a=y+1;
}else if(y>x)
{
System.out.println("太大了!");
b=y-1;
}
else
{
System.out.println("正確答案!");
break;
}
}catch(Exception e)
{
System.out.println("請輸入一整數!");
}
}
System.out.println("猜了"+n+"次");}
}
複製代碼
作者:
陳宥穎
時間:
2020-9-25 20:46
import java.math.MathContext;
import java.util.Scanner;
public class Ch30 {
public static void main(String[] args) {
int gu,b=1,c=99,n=0;
int y=(int)(Math.random()*100);
while(true)
{
Scanner s=new Scanner(System.in);
n++;
try{System.out.println("輸入一個"+b+"~"+c+"的數");
gu=s.nextInt();
if(gu<y)
{
System.out.println("輸入太小了");
b=gu+1;
}
else if(gu>y)
{
System.out.println("輸入太大了");
c=gu-1;
}
else
{
System.out.println("猜對了");
break;
}
}catch(Exception e)
{
System.out.println("輸入整數!");
}
}
System.out.println("共輸入"+n+"次");
}
}
複製代碼
作者:
沈子晏
時間:
2020-9-25 20:47
import java.util.Scanner;
public class Ch01 {
public static void main(String[] args) {
int a=1, b=99;
int r=(int)(Math.random()*99+1);
int guess,n=0;
while(true)
{
try{
Scanner s=new Scanner(System.in);
n++;
System.out.println("猜一個"+a+"~"+b+"之間的整數:");
guess=s.nextInt();
if(guess<r){
System.out.println("猜得太小了");
a=guess+1;
}
else if(guess>r){
System.out.println("猜得太大了");
b=guess-1;
}
else{
System.out.println("恭喜你猜對了!");
break;
}
}
catch(Exception e)
{
System.out.println("請輸入一整數!");
}
}
System.out.println("總共猜了"+n+"次");
}
}
複製代碼
作者:
夏子涵
時間:
2020-9-25 20:48
import java.util.Scanner;
public class Ch01 {
public static void main(String[] args) {
int target=(int)(Math.random()*99+1);
int a=1, b=99;
int guess, n=0;
while(true){
n++;
Scanner s=new Scanner(System.in);
System.out.print("猜一個"+a+"~"+b+"之間的數字: ");
try{
guess=s.nextInt();
if(guess>target){
System.out.println("猜得太大了");
b=guess-1;
}else if(guess<target){
System.out.println("猜得太小了");
a=guess+1;
}
else{
System.out.println("恭喜你猜對了!");
break;
}
}catch(Exception e){
System.out.println("請輸入一整數!");
}
}System.out.println("總共猜了"+n+"次!");
}
}
複製代碼
作者:
黃辰昊
時間:
2020-9-25 20:49
import java.util.Scanner;
public class Ch01 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int a=1, b=99;
int target=(int)(Math.random()*99+1); //1~99
int guess, n=0;
while(true)
{
n++;
try
{
System.out.print("猜一個"+a+"~"+b+"之間的數字: ");
guess=s.nextInt();
if(guess>target)
{
System.out.println("猜得太大了");
b=guess-1;
}else if(guess<target)
{
System.out.println("猜得太小了");
a=guess+1;
}else
{
System.out.println("恭喜你猜對了!");
break;
}
}catch(Exception e)
{
System.out.println("請輸入數字");
}
}
System.out.println("總共猜了"+n+"次!");
}
}
複製代碼
作者:
李宇澤
時間:
2020-9-25 20:52
import java.util.Scanner;
public class Ch01 {
public static void main(String[] args)
{
int x=(int)(Math.random()*99+1);
int y, n=0;
int a=1, b=99;
while(true)
{
try
{
Scanner s=new Scanner(System.in);
n++;
System.out.print("猜一個"+a+"~"+b+"之間的數: ");
y=s.nextInt();
if(y<x)
{
System.out.println("太小了!");
a=y+1;
}else if(y>x)
{
System.out.println("太大了!");
b=y-1;
}
else
{
System.out.println("正確答案!");
break;
}
}catch(Exception e)
{
System.out.println("請輸入一整數!");
}
}
System.out.println("猜了"+n+"次");}
}
複製代碼
作者:
黃柏叡
時間:
2020-9-25 21:02
import java.rmi.server.ExportException;
import java.util.Scanner;
import javax.swing.tree.ExpandVetoException;
public class Ch02 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int a=1,b=99;
int target=(int)(Math.random()*99+1);
int guess,n=0;
while (true)
{
n++;
try
{
System.out.print("猜一個"+a+"~"+b+"之間的整數");
guess=s.nextInt();
if(guess>target)
{
System.out.println("猜得太大了");
a=guess-1;
}
else if(guess<target)
{
System.out.println("猜得太小了");
b=guess+1;
}else
{
System.out.println("恭喜猜對了");
break;
}
}catch(Exception e)
{
System.out.println("請輸入數字");
}
System.out.println("總共猜了"+n+"次");
}
}
}
複製代碼
作者:
董宸佑
時間:
2020-9-25 21:03
import java.util.Scanner;
public class Ch01 {
public static void main(String[] args) {
int x=(int)(Math.random()*99+1);
int sum=1, ans, a=1, b=99;
System.out.println(x);
while(true)
{
Scanner s=new Scanner(System.in);
System.out.print("猜一個"+a+"~"+b+"的數字: ");
ans=s.nextInt();
try{
if(ans<x)
{
System.out.println("猜得太小");
sum++;
a=ans+1;
}
else if(ans>x)
{
System.out.println("猜得太大");
sum++;
b=ans-1;
}
else
{
System.out.println("猜對了!!!!!!!!!!!!!!!!!!!!!!!");
break;
}
}catch(Exception e)
{
System.out.println("請輸入一整數!!!!!!!!!!!");
}
}
System.out.println("總共猜了"+sum+"次");
}
}
複製代碼
作者:
黃宥華
時間:
2020-9-27 23:57
import java.util.Scanner;
public class Ch01 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int a=1, b=99;
int target=(int)(Math.random()*99+1);
int guess, n=0;
while(true)
{
n++;
try
{
System.out.print("猜一個"+a+"~"+b+"之間的數字: ");
guess=s.nextInt();
if(guess>target)
{
System.out.println("太大了");
b=guess-1;
}else if(guess<target)
{
System.out.println("太小了");
a=guess+1;
}else
{
System.out.println("猜對了!");
break;
}
}catch(Exception e)
{
System.out.println("請輸入數字");
}
}
System.out.println("總共猜了"+n+"次!");
}
}
複製代碼
作者:
劉愷鈞
時間:
2020-10-16 18:54
import java.util.Scanner;
public class Ch01 {
public static void main(String[] args) {
int a=1,b=99;
int guess,n=0;
int target=(int)(Math.random()*99+1);
while(true)
{
n++;
Scanner s=new Scanner(System.in);
System.out.print("猜一介於"+a+"~"+b+"間的數字:");
try
{
guess=s.nextInt();
if(guess>target)
{
System.out.println("猜得太大了");
b=guess-1;
}
else if(guess<target)
{
System.out.println("猜得太小了");
a=guess+1;
}
else
{
System.out.println("恭喜你,答對了!");
break;
}
}catch(Exception e)
{
System.out.println("請輸入一整數!");
}
}
System.out.println("總共花了"+n+"次!");
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2