資源簡介
JAVA拿火柴的小游戲,使用JDK編寫,JAVA初學者的范例程序,有出錯循環(huán),
代碼片段和文件信息
import?java.util.*;
public?class?Test8{
public?static?void?main(String?args[]){
Random?rand=new?Random();
Scanner?reader=new?Scanner(System.in);
boolean?sw=false;
int?num?=?0play=0num_computercom_randplaynum;
int?amount=rand.nextInt(30)+20;
do{
?sw=false;
??try{
? Scanner?input?=?new?Scanner(System.in);?
System.out.println(“1是人先走,2是計算機先走:“);?
?? play?=?input.nextInt();
if(play>2||play<1){
sw=true;
System.out.println(“輸入錯誤,請輸入1~2之間的數(shù)。請重試。“);
}
? ???}
??catch?(InputMismatchException?e){
??? System.out.println(“對不起輸入錯誤。輸入的要數(shù)字,請重試“);//出錯提示
sw=true;
?? ??}
}
while(sw==true);?????????????//whofirstplay認證完畢
/*?比賽部分開始*/
while(amount!=0){
playnum=0;
switch(play){
case?1:
do{
sw=false;
? try{
? Scanner?input?=?new?Scanner(System.in);?
System.out.println(“目前有“+amount+“根火柴,您可以拿1~3根火柴“);?
?? playnum?=?input.nextInt();
if(playnum>3||playnum<1||amount sw=true;
System.out.println(“對不起輸入錯誤,請輸入1~3之間的數(shù)并且小于目前火柴數(shù)請重試“);
}
? ?? ?}?
?? catch?(InputMismatchException?e){
??? System.ou
評論
共有 條評論