62,614
社区成员
发帖
与我相关
我的任务
分享
class Myexception extends Exception {
public static void compute(int a) throws Myexception
{
if(a!=1 || a!=2 || a!=3)
{
throw new Myexception();
}
}
}
import java.util.Scanner;
public class GuessFist {
public static void main(String[] args) {
System.out.println("欢迎来到猜拳游戏");
System.out.println("------------------------");
do
{ System.out.println("请选择一个角色进行游戏");
System.out.println("1.孙权");
System.out.println("2.刘备");
System.out.println("3.曹操");
System.out.println("请输入所选角色对应的数字");
try{
Scanner enter = new Scanner(System.in);
int playernum1 = enter.nextInt();
Myexception.compute(playernum1);
break;
}catch(Exception ex){
System.out.println("请从1~3中选择输入数字!!!!");
continue;
}catch(Myexception ex)
{
System.out.println("请从1~3中选择输入数字!!!!");
continue;
}
}while(true);
}
}
package snake;
import java.util.Scanner;
public class GuessFist {
public static void main(String[] args) {
System.out.println("欢迎来到猜拳游戏");
System.out.println("------------------------");
do {
System.out.println("请选择一个角色进行游戏");
System.out.println("1.孙权");
System.out.println("2.刘备");
System.out.println("3.曹操");
System.out.println("请输入所选角色对应的数字");
try {
Scanner enter = new Scanner(System.in);
int playernum1 = enter.nextInt();
Myexception.compute(playernum1);
break;
} catch (Myexception ex) {
System.out.println("请从1~3中选择输入数字----!!!!");
continue;
} catch (Exception ex) {
System.out.println("请从1~3中选择输入数字!!!!");
continue;
}
} while (true);
}
}
class Myexception extends Exception {
public static void compute(int a) throws Myexception {
/*if (a != 1 || a != 2 || a != 3) {
throw new Myexception();
}*/
if (!(a == 1 || a == 2 || a == 3)) {
throw new Myexception();
}//如果是int型的数,但不是1,2,3
}
}