Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查用户输入是否与同一列中的任何数字匹配_Java_Netbeans - Fatal编程技术网

Java 检查用户输入是否与同一列中的任何数字匹配

Java 检查用户输入是否与同一列中的任何数字匹配,java,netbeans,Java,Netbeans,程序是10x10板,我需要检查用户的输入是否与同一列中的任何数字重复,但我无法找出它。当我尝试它时,它总是选中相同的框。例如,如果我在[1][1](按10x10网格)中输入了4,它会在输入后立即自动检查[1][1]是否与我的输入相同,并将其删除。我的教授想让我用“CheckWinner”方法检查一下。这是迄今为止我在eventhandler中的代码: /* * To change this template, choose Tools | Templates * and open the t

程序是10x10板,我需要检查用户的输入是否与同一列中的任何数字重复,但我无法找出它。当我尝试它时,它总是选中相同的框。例如,如果我在[1][1](按10x10网格)中输入了4,它会在输入后立即自动检查[1][1]是否与我的输入相同,并将其删除。我的教授想让我用“CheckWinner”方法检查一下。这是迄今为止我在eventhandler中的代码:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package firstgui;
import java.awt.event.*;
import java.util.Scanner;
import javax.swing.*;


/**
 *
 * @author douglas moody
 */
 public class EventHandler  implements ActionListener{
     // EventBoard is the name of the array containing all the JButton withi teh EventHandler
     // board (in the other program)  is the name of the array containing all the JButton withi the FirstGui program
     JButton[][] EventBoard;
     private  static String player = " ";
     // this method is called from FirstGui to tell the Eventhandler the board array
     public void setEventBoard (JButton[][] inboard){
        EventBoard = inboard;
     }      

     public  void actionPerformed(ActionEvent e) {
        JButton clickedbutton;

        // clickedbutton will now point to the Button actually clicked
        clickedbutton = (JButton) e.getSource();


        player = JOptionPane.showInputDialog("Enter a number from 1-10");
        if(player.matches("[1-9]|10")){
            clickedbutton.setText(player);
        }else{
            JOptionPane.showMessageDialog(null, "Invalid Input", "Invalid Input", JOptionPane.ERROR_MESSAGE);
        }

        // call the routine to check if the player who just moved won
        if (CheckWinner(player)){
            JOptionPane.showMessageDialog(null,   "Player: " + player + " won");
        }
    }   

    private boolean CheckWinner(String inplayer) {
        int count = 0, count2 = 0;
        // this loop checks the columns on the Board
        for (int i=0; i<=9; i++){
            for (int j=0; j<=9; j++) {
                if (EventBoard[i][j].getText().equals( inplayer )){
                    JOptionPane.showMessageDialog(null, "copy");
                    EventBoard[i][j].setText("");
                }
            }
            if (count == 10 && count2 == 10) return true;
        }
        return false;
    }
 }
/*
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
第一个图形用户界面;
导入java.awt.event.*;
导入java.util.Scanner;
导入javax.swing.*;
/**
*
*@作者道格拉斯·穆迪
*/
公共类EventHandler实现ActionListener{
//EventBoard是包含所有带有EventHandler的JButton的数组的名称
//board(在另一个程序中)是包含第一个GUI程序中的所有JButton的数组的名称
JButton[][]事件板;
私有静态字符串播放器=”;
//此方法从FirstGui调用,以告知Eventhandler板阵列
公共无效设置事件板(JButton[][]内置){
事件板=内侧;
}      
已执行的公共无效操作(操作事件e){
b按钮点击按钮;
//clickedbutton现在将指向实际单击的按钮
clickedbutton=(JButton)e.getSource();
player=JOptionPane.showInputDialog(“输入1-10之间的数字”);
如果(玩家匹配(“[1-9]| 10”)){
单击按钮。设置文本(播放器);
}否则{
showMessageDialog(null,“无效输入”,“无效输入”,JOptionPane.ERROR\u消息);
}
//打电话给例行程序,检查刚刚移动的玩家是否赢了
如果(胜利者(玩家)){
showMessageDialog(空,“玩家:“+Player+”赢得”);
}
}   
私有布尔校验获胜者(字符串输入层){
int count=0,count2=0;
//这个循环检查电路板上的列

对于(int i=0;i将触发操作的
JButton
引用传递给
CheckWinner
方法,并在执行检查时忽略它

例如

private boolean CheckWinner(JButton source, String inplayer) {
    //...
    if (EventBoard[i][j] != source && 
            EventBoard[i][j].getText().equals( inplayer )){
        JOptionPane.showMessageDialog(null, "copy");
        EventBoard[i][j].setText("");
    }
已更新…

要检查给定列中的值,您需要知道要检查哪个列。我没有测试过这个,但下面的示例应该会有所帮助

private boolean CheckWinner(JButton source, String inplayer) {
    int buttonColumn = -1;
    for (int col = 0; col < EventBoard.length; col++) {
        for (JButton item : EventBoard[col]) {
            if (item == source) {
                buttonColumn = col;
                break;
            }
        }
    }
    boolean match = false;
    for (JButton item : EventBoard[buttonColumn]) {
        if (source != item && item.getText().equals(inplayer)) {
            match = true;
            break;
        }
    }
    return match;
}
private boolean CheckWinner(JButton源代码,字符串输入层){
int buttonColumn=-1;
for(int col=0;col
我要更改什么”for(int I=0;我会设想您需要找出原始按钮所在的列,并仅将其用作checkI EventBoard[0][j]的列值,仅检查第一列,但当我在同一行上输入相同的数字时,它显示为“复制”。此外,当它显示为“复制”时,它会删除以前输入的数字,而不是刚刚输入的数字。但原始按钮位于哪一列?这是重要的部分。从您的代码中,很难知道它是否
[col][row]
[row][col]
…我刚检查过,是[col][row]。