我需要有关此分配的帮助,我不确定如何修复我的代码。我想确保如果用户输入的数字不是数字,它将提示“ Invalid entry!”。
if(checkPositionAvailability(move))
{
board[move] = 'H'; //make 'H' for player move
break;
}
else
{
System.out.println("Position not available.\nMake a different choice.");
continue;
}
}
else
{
System.out.println("Invalid entry!");
continue;
}
}
public class HelloWorld{
public static void main(String []args){
char c = 'c';
char one = '1';
char two = '2';
System.out.printf("Is available [%c] ? %s\n", c, IsAvailable(c));
// Is available [c] ? false
System.out.printf("Is available [%c] ? %s\n",one, IsAvailable(one));
// Is available [c] ? true
System.out.printf("Is available [%c] ? %s\n",two, IsAvailable(two));
// Is available [c] ? true
}
public static boolean IsAvailable(char character){
String s = Character.toString(character);
try {
int num = Integer.parseInt(s);
return true;
}catch (NumberFormatException e){
// means it is not a number. Thus return false
return false;
}
}
}