gpt4 book ai didi

java - 如果在控制台中输入非数字,如何使程序继续运行

转载 作者:行者123 更新时间:2023-12-01 07:55:32 26 4
gpt4 key购买 nike

这是一款四连子游戏,如果用户输入数字 1-7,则棋盘上会放置一个圆盘。如果数字不是1-7,则会弹出“重试”。但是,当用户输入字母或符号时,就会出现异常。这是我的代码:

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;
}
}

最佳答案

当您使用console.nextInt()时,您的代码需要一个整数

使用 hasNextInt() 确定下一个内联字符是否为 int,如果不是,则使用 next() 跳过该字符

if(console.hasNextInt()){
column = (console.nextInt()) -1;
/* ... */
} else {
console.next();
}

关于java - 如果在控制台中输入非数字,如何使程序继续运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30654015/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com