请问该怎么将数组内两个相同的数字输出为红色


 public void search()
   {
       int n =Integer.parseInt(text_count.getText());
       int columns =tablemodel.getColumnCount();
       int rows =(n%columns == 0) ? n/columns :n/columns+1;
       for (int i = 0; i < Integer.parseInt(text_count.getText()); 
i++)
           for (int j = 0; j < (Integer.parseInt(this.text_count.getText() )- 1); 
j++)
               if ( number[j] >  number[j + 1])
               {
                   temp =  number[j];
                  number[j] = number[j + 1];
                   number[j + 1] = temp;
               }

       for(int i = 0; i < Integer.parseInt(text_count.getText()); 
i++)
           if(number[i+1]==number[i])
           {
               this.setForeground(java.awt.Color.red);     
           }
   }

帮你改了:

public void search() {
        int n = Integer.parseInt(this.text_count.getText());
        int row = n / 10 + 1;
        int arr[][] = new int[row][10]; // 定义一个二维数组,数组里面标1的就是要标红的

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i != j && this.number[i] == this.number[j]) {
                    int rowI = i / 10; // i 所在的行
                    int columnI = i % 10; // i 所在的列

                    int rowJ = j / 10; // i 所在的列
                    int columnJ = j % 10; // j 所在的列
                    arr[rowI][columnI] = 1; // 标记1,要标红
                    arr[rowJ][columnJ] = 1; // 标记1,要标红
                }
            }
        }

        //新建列表现器------------------------//
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer() {
            public Component getTableCellRendererComponent(JTable table, Object value,
                                                           boolean isSelected, boolean hasFocus, int row, int column) {
                Component cell = super.getTableCellRendererComponent
                        (table, value, isSelected, hasFocus, row, column);
                if (arr[row][column] == 1 && cell.isBackgroundSet())//设置变色的单元格
                    cell.setBackground(Color.RED);
                else
                    cell.setBackground(Color.WHITE);

                return cell;
            }
        };

        int columns = tablemodel.getColumnCount();
        for (int m = 0; m < columns; m++) {
            jtable.getColumn(title[m]).setCellRenderer(tcr);
        }

    }

img


如有帮助,欢迎采纳哈!

在这里插入图片描述

本人的开源项目,欢迎star支持下!!!


system.err.print