Java 如果在控制台中键入了非数字,如何使程序继续运行
这是一个“连接四”游戏,如果用户键入数字1-7,则会在棋盘上放置一张光盘。如果号码不是1-7,则弹出“重试”。但是,当用户键入字母或符号时,会出现异常。 这是我的密码:Java 如果在控制台中键入了非数字,如何使程序继续运行,java,arrays,graphics,console,java.util.scanner,Java,Arrays,Graphics,Console,Java.util.scanner,这是一个“连接四”游戏,如果用户键入数字1-7,则会在棋盘上放置一张光盘。如果号码不是1-7,则弹出“重试”。但是,当用户键入字母或符号时,会出现异常。 这是我的密码: import java.util.*; import java.awt.*; public class ConnectFour{ public static void main(String[] args){ //board DrawingPanel panel = new DrawingPa
import java.util.*;
import java.awt.*;
public class ConnectFour{
public static void main(String[] args){
//board
DrawingPanel panel = new DrawingPanel(550,550);
int rowAvailable;
Graphics g = panel.getGraphics();
g.drawLine(0,0,0,427);
g.drawLine(0,0,500,0);
g.drawLine(500,0,500,427);
g.drawLine(0,427,500,427);
for(int i = 0; i< 6; i++){
for(int j= 0; j<= 6; j++){
g.setColor(Color.YELLOW);
g.fillRect(j*71,i*71,71,71);
g.setColor(Color.WHITE);
g.fillOval(j*71,i*71,71,71);
}
}
//setBlankArray
Scanner console = new Scanner(System.in);
char[][] board = new char[6][7];
for(int j = 0;j <= 6; j++){
for(int i= 0; i < 6; i++){
board[i][j] = ' ';
}
}
boolean isBlack = true;
boolean isRed = false;
int column = 0;
boolean playersTurn = true;
boolean rightNum = false;
//oneTurn
while(getWinner(board, playersTurn)){
//while(playersTurn == true){
rightNum = false;
if(isBlack == true){
// displayCurrentPlayer
System.out.println("Black's Turn");
g.setColor(Color.WHITE);
g.drawString("Red Disc's Turn",200, 450);
g.setColor(Color.BLACK);
g.drawString("Black Disc's Turn",200, 450);
}
else{
// displayCurrentPlayer
System.out.println("Red's Turn");
g.setColor(Color.WHITE);
g.drawString("Black Disc's Turn",200, 450);
g.setColor(Color.RED);
g.drawString("Red Disc's Turn",200, 450);
}
System.out.print("Choose a column to place your disk (1-7): ");
while(rightNum == false){
column = (console.nextInt()) -1;
if(column >= 0 && column < 7 && board[0][column] == ' '){
rightNum = true;
}
else{
System.out.print("Try again: ");
}
}
drawBoard(column, board, isBlack, isRed, board, g);
isBlack = !isBlack;
}
if(isBlack == false){
System.out.println("Congratulations Black Player");
}
else
{System.out.println("Congratulations Red Player");
}
// use the while loop to say try again if the column is filled.
}
public static void drawBoard(int column, char[][] board, boolean isBlack, boolean isRed, char[][] availability,Graphics g){
char player = ' ';
if(isBlack == true){
g.setColor(Color.BLACK);
player = 'b';
}
else{
g.setColor(Color.RED);
player = 'r';
}
int x = 0;
int row = 5;
while(board[row-x][column] != ' '){
x++;
}
row = row-x;
g.fillOval((column * 71),(row * 71), 71,71);
board[row][column] = player;
}
public static boolean getWinner(char[][] board, boolean playersTurn){
int verticalCount = 0;
boolean isVertical = false;
for(int i = 0; i <= board[0].length - 1; i++){
verticalCount = 0;
for(int j = board.length - 1; j > 0; j--){
if(board[j][i] == board[j-1][i] && board[j][i] != ' '){
verticalCount++;
} else {
verticalCount = 0;
}
if(verticalCount == 3){
isVertical = true;
}
}
}
int horizontalCount = 0;
boolean isHorizontal = false;
for(int i = board.length-1; i >= 0; i--){
for(int j = 0 ; j < board[0].length-1; j++){
if(board[i][j] == board[i][j+1] && board[i][j] != ' '){
horizontalCount++;
}
else{
verticalCount = 0;
}
if(horizontalCount == 3){
isHorizontal = true;
}
}
}
int diagonalCount = 0;
boolean isDiagonal = false;
// for(int i = 0; i<=6; i++){
// for(int j =0; j<6; j++){
// if(board[i][j-1] == board[i][j]){
// diagonalCount++;
// }
// }
// }
if(isVertical || isHorizontal || isDiagonal){
playersTurn = false;
}
else{
playersTurn = true;}
return playersTurn;
}
}
import java.util.*;
导入java.awt.*;
公共第四类{
公共静态void main(字符串[]args){
//董事会
DrawingPanel=新的DrawingPanel(550550);
int ROWN可用;
Graphics g=panel.getGraphics();
g、 抽绳(0,0,0427);
g、 抽绳(0,0500,0);
g、 抽绳(500,0500427);;
g、 抽绳(0427500427);
对于(int i=0;i<6;i++){
对于(int j=0;j=0;i--){
对于(int j=0;j //对于(int i=0;i您需要验证您的输入:
// check that an int in inputted.
if (console.hasNextInt()==true){
// add validation for number range
// or some other such algorithm
if (console.nextInt()>1= &&console.nextInt()<8){
column = (console.nextInt()) -1;
//etc
//检查是否输入了int。
if(console.hasNextInt()==true){
//添加数字范围的验证
//或者其他类似的算法
如果(console.nextInt()>1=&&console.nextInt()您需要验证您的输入:
// check that an int in inputted.
if (console.hasNextInt()==true){
// add validation for number range
// or some other such algorithm
if (console.nextInt()>1= &&console.nextInt()<8){
column = (console.nextInt()) -1;
//etc
//检查是否输入了int。
if(console.hasNextInt()==true){
//添加数字范围的验证
//或者其他类似的算法
如果(console.nextInt()>1=&console.nextInt()使用console.nextInt()时,您的代码需要一个整数
使用hasNextInt()
确定内联的下一个字符是否为int,如果不是,则使用next()
使用console.nextInt()时,代码需要一个整数
使用hasNextInt()
确定内联的下一个字符是否为int,如果不是,则使用next()
该方法抛出:
- InputMismatchException-如果下一个标记与整数正则表达式不匹配,或超出范围
- NoTouchElementException-如果输入用尽
- IllegalStateException-如果此扫描仪已关闭
您的案例应该抛出第一个异常InputMismatchException
,因此您只需使用try{
块:
while(rightNum == false){
try{
column = (console.nextInt()) -1;
if(column >= 0 && column < 7 && board[0][column] == ' '){
rightNum = true;
}
}catch(InputMismatchException ime)
{
//do nothing because it will achieve the else block
}
while(rightNum==false){
试一试{
column=(console.nextInt())-1;
如果(列>=0&&column<7&&board[0][column]=''){
rightNum=true;
}
}捕获(输入不匹配异常输入法)
{
//什么都不做,因为它将实现else块
}
该方法抛出:
- InputMismatchException-如果下一个标记与整数正则表达式不匹配,或超出范围
- NoTouchElementException-如果输入用尽
- IllegalStateException-如果此扫描仪已关闭
您的案例应该抛出第一个异常InputMismatchException
,因此您只需使用try{
块:
while(rightNum == false){
try{
column = (console.nextInt()) -1;
if(column >= 0 && column < 7 && board[0][column] == ' '){
rightNum = true;
}
}catch(InputMismatchException ime)
{
//do nothing because it will achieve the else block
}
while(rightNum==false){
试一试{
column=(console.nextInt())-1;
如果(列>=0&&column<7&&board[0][column]=''){
rightNum=true;
}
}捕获(输入不匹配异常输入法)
{
//什么都不做,因为它将实现else块
}
您想让程序在输入非数字字符时或当前执行时引发异常,并对其进行更改吗?您想让程序在输入非数字字符时或当前执行时引发异常,并对其进行更改吗?您应该将if
语句移至try
或添加一个continue
到catch
中。否则,当列中剩余的命中时,您将得到一些错误,如果@ChrisStillwell他用0初始化列,但您的建议无论如何都更安全,谢谢。是的,但它是在循环之外初始化的,并且你们两人都不重置因此,如果您输入非法字符,您的移动将成为最后一次播放的移动。您应该将if
语句移动到try
中,或者在catch
中添加continue
。否则,当列中剩余的点击if
@ChrisStillwell>时,您将得到一些错误使用0初始化列
,但您的建议更安全,谢谢。是的,但它是在循环之外初始化的,并且你们都不会在任何地方重置它。因此,如果您输入非法字符,您的移动将成为最后一次播放的移动。