使用java字符串处理错误
如果输入的不是选项和String/char,我将尝试向java程序添加错误处理。如果输入字符串,我主要需要它。我已经试过了,但我真的不明白。我还加了一句!(kb.hasNextInt())到我的行,而(choice<1&&choice>4)也不起作用。所以我只需要帮助添加错误处理到我的程序。谢谢 这是我的密码使用java字符串处理错误,java,Java,如果输入的不是选项和String/char,我将尝试向java程序添加错误处理。如果输入字符串,我主要需要它。我已经试过了,但我真的不明白。我还加了一句!(kb.hasNextInt())到我的行,而(choice4)也不起作用。所以我只需要帮助添加错误处理到我的程序。谢谢 这是我的密码 import java.util.*; public class HeroesVersusMonsters { private static Hero hero; private static
import java.util.*;
public class HeroesVersusMonsters
{
private static Hero hero;
private static Monster monster;
private static Random rand = new Random();
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
do
{
System.out.println("---------------------------------------");
System.out.println("\tChoose your type of hero");
System.out.println("---------------------------------------");
System.out.println("\t1. Warrior");
System.out.println("\t2. Sorceress");
System.out.println("\t3. Thief");
System.out.println("\t4. Snake");
System.out.println();
System.out.print("Choice --> ");
int choice = kb.nextInt();
kb.nextLine();
while (choice < 1 && choice > 4 )
{
System.out.println("\n" + choice + " is not an option. Please try again.");
System.out.print("Choice --> ");
choice = kb.nextInt();
kb.nextLine();
System.out.println();
}
switch (choice)
{
case 1:
hero = new Warrior();
break;
case 2:
hero = new Sorceress();
break;
case 3:
hero = new Thief();
break;
case 4:
hero = new Snake();
break;
}
switch (rand.nextInt(3))
{
case 0:
monster = new Ogre("Shrek the Ogre");
break;
case 1:
monster = new Skeleton("Bones the Skeleton");
break;
case 2:
monster = new Gremlin("Dobby the Gremlin");
break;
}
System.out.println();
System.out.println(hero.name + ", you will be fighting against " + monster.getName() + "!!!");
System.out.println();
while (hero.getHits() > 0 && monster.getHits() > 0)
{
hero.attack(monster);
monster.attack(hero);
}
System.out.print("Would you like to play again? (yes / no) ");
String play = kb.nextLine().toLowerCase();
play = play.trim();
if (play.equals("no"))
break;
else
System.out.println();
}
while (true);
}
}
import java.util.*;
公共类英雄与怪物
{
私人静态英雄;
私人静态怪物;
私有静态随机兰德=新随机();
公共静态void main(字符串[]args)
{
扫描仪kb=新扫描仪(System.in);
做
{
System.out.println(“---------------------------------------------------”;
System.out.println(“\t选择英雄类型”);
System.out.println(“---------------------------------------------------”;
System.out.println(“\t1.Warrior”);
System.out.println(“\t2.女巫”);
System.out.println(“\t3.thift”);
System.out.println(“\t4.Snake”);
System.out.println();
System.out.print(“选择-->”;
int choice=kb.nextInt();
kb.nextLine();
while(选项<1&&choice>4)
{
System.out.println(“\n”+choice+”不是选项。请重试。“);
System.out.print(“选择-->”;
choice=kb.nextInt();
kb.nextLine();
System.out.println();
}
开关(选择)
{
案例1:
英雄=新战士();
打破
案例2:
英雄=新女巫();
打破
案例3:
英雄=新小偷();
打破
案例4:
英雄=新蛇();
打破
}
开关(兰特nextInt(3))
{
案例0:
怪物=新食人魔(“食人魔史莱克”);
打破
案例1:
怪物=新骨骼(“骨骼骨骼”);
打破
案例2:
怪物=新的小精灵(“小精灵多比”);
打破
}
System.out.println();
System.out.println(hero.name+),您将与“+monster.getName()+”!!!”)战斗;
System.out.println();
while(hero.getHits()>0&&monster.getHits()>0)
{
英雄。攻击(怪物);
怪物攻击(英雄);
}
System.out.print(“您想再次播放吗?(是/否)”);
字符串播放=kb.nextLine().toLowerCase();
play=play.trim();
if(play.equals(“no”))
打破
其他的
System.out.println();
}
虽然(正确);
}
}
您应该查看Java正则表达式:
if(choice.toString().matches("[0-9]+"))
{
//continue
}
else
{
//error message
}
以下是您的主要方法:
public static void main(String ...args){
final Scanner scanner = new Scanner(System.in);
while(true){
final Hero hero = promptHero(scanner);
final Monster monster = getRandomMonster();
fight(hero, monster);
if(!playAgain(scanner))
break;
}
}
现在编写静态方法promptHero
,getrandomonster
,fight
,和playreach
(如果要再次播放,应该返回true
)
以下是您的promptHero
方法的外观(以正确处理错误输入):
专用静态Hero promptHero(最终扫描仪){
while(true){
System.out.println(“---------------------------------------------------”;
System.out.println(“\t选择英雄类型”);
System.out.println(“---------------------------------------------------”;
System.out.println(“\t1.Warrior”);
System.out.println(“\t2.女巫”);
System.out.println(“\t3.thift”);
System.out.println(“\t4.Snake”);
System.out.println();
System.out.print(“选择-->”;
试一试{
final int choice=scanner.nextInt();
如果(选项<1 | |选项>4)
System.out.println(“\n”+选项+
“不是选项。请重试。”);
其他的
return getHero(choice);//返回英雄
}捕获(输入不匹配异常输入法){
最后一行字符串=scanner.nextLine();//需要提前标记
System.out.println(“\n”+行+
“不是选项。请重试。”);
}
}
}
私有静态英雄getHero(最终整数选择){
开关(选择){
案例1:
返回新战士();
案例2:
返回新的女巫();
案例3:
归还新的小偷();
案例4:
返回新的Snake();
}
返回null;
}
请仔细查看您的内部while循环状况
while (choice < 1 && choice > 4 )
choice
如何既小于1又大于4?当它们输入类似于ad;lkfjasdf;alksdf
是他们的选择吗?除非你中断,否则while(true)
将永远持续下去。中断
您只需退出switch语句,而不是do/while
循环。如果将其分解为方法,则编写和调试会容易得多。int不能取消引用如果(choice.toString().equals([0-9]+”)缺少右括号,nextInt
方法能够捕获错误的输入——您不应该在这里使用正则表达式(Java本机库中已经为您提供了正则表达式)。
while (choice < 1 && choice > 4 )
while(choice<1 || choice>4)
ArrayList<Integer> ar=new ArrayList<Integer>(4);
ar.add(1);
ar.add(2);
ar.add(3);
ar.add(4);
while(true)
{
if(ar.contains(choice))
{
//Go On
}
else
{
//Print old stuff
}
}