【问题标题】:java public static and private static, stringbuilderjava公共静态和私有静态,stringbuilder
【发布时间】:2013-12-17 02:27:44
【问题描述】:

下面是我要编译的代码,似乎我在方法中使用方法时遇到问题,无法关闭第一个方法。因此,无法编译程序。此外,我不确定我应用的 Stringbuilder 是否正确,因为我无法编译。

import javax.swing.JOptionPane;

public class tryingtoedit{

public static void main( String[] args ){
int block[][] = new int[gridsize][gridsize];
int row = 0;   // The variable assigned for counting rows
int column = 0; // The variable assigned for counting columns
int blockRow,blockColumn;
int score = 0;
final int gridsize = 3;
String inputRow, inputColumn;

//random a set of number
int randomSet[] = new int[100];
for(int i=0;i<randomSet.length;i++){
    randomSet[i] = (int) (Math.random() * gridsize * gridsize) + 1;
}


int round = 0 ;//for counting the round of the game
while( true ){
    ++round;

    //print out the title and score
    System.out.println( "-------------------------" );
    System.out.println( "Divided by 10 - Mini Game" );
    System.out.printf( "----  Score:%8d  ----\n\n", score );

    //show game board
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < gridsize; i++) {
        sb.append(String.format("%4d", i));
}

    //display the coming value
    System.out.printf( "Coming value : %d > %d > %d \n\n", randomSet[round], randomSet[(round + 1)], randomSet[(round + 2)] );



    //check over checking
    private static final boolean isComplete(int[][] grid) {
        for (int row = 0; row < grid.length; row++) {
            for (int col = 0; col < grid[row].length; col++) {
                if (grid[row][col] == 0) {
                    return false;
                }
            }
        }
        return true; // all celss have values.
}
                System.out.println( "------  Game Over  ------" );
                //continue check
                if(isComplete(block)){
                int ContinueCheck = JOptionPane.showConfirmDialog(null, "Do you want to    continue ?", "Continue?", JOptionPane.YES_NO_OPTION);
                if( ContinueCheck == JOptionPane.YES_OPTION){
                    //initialize the game board
                    for(row=0;row<block.length;row++)
                        for(column=0;column<block.length;column++)
                            block[row][column]=0;
                    continue;
                }else{if(ContinueCheck == JOptionPane.NO_OPTION){
                    System.out.println( "-------------------------" );
                    System.out.println("Good Bye !");
                    break;}
                }
            }


    //input value
    while(true){
        inputRow = JOptionPane.showInputDialog( "The number of row you want to put the number" );
        blockRow = Integer.parseInt(inputRow);
        inputColumn = JOptionPane.showInputDialog( "The number of column you want to put the number" );
        blockColumn = Integer.parseInt(inputColumn);
        if(blockRow>=block.length || blockRow<0 || blockColumn>=block.length || blockColumn<0){
            JOptionPane.showMessageDialog(null , "The block you want to enter the number does not exist." , "Error" , JOptionPane.ERROR_MESSAGE );
            continue;
        }else{
            if(block[blockRow][blockColumn]!=0){
                JOptionPane.showMessageDialog(null , "The block you want to enter the number has been entered an number." , "Error" , JOptionPane.ERROR_MESSAGE );
                continue;
            }else{
                block[blockRow][blockColumn] = randomSet[round];
                break;
            }
        }
    }


    //score got check
    int modSumBlock[] = new int[gridsize + gridsize + 2]; // rows, columns, and diagonals
    for (int row = 0; row < gridsize; row++) {
        modSumBlock[       0 + row] = getRowScore(block, row);
    }
    for (int col = 0; col < gridsize; col++) {
        modSumBlock[gridsize + col] = getColumnScore(block, col);
    }
    modSumBlock[gridsize + gridsize]     = getSlashDiagonalScore(block);
    modSumBlock[gridsize + gridsize + 1] = getBackslashDiagonalScore(block);
    //all 'if' is used for checking if all block in the same row/column/diagonal are filled in number

    //counting how many score got and where should be cleared by 8bit (in decimal)
    int scoreCount = 0;
    for(int n=0;n<8;n++){
        if(modSumBlock[n]==0){
            score += 10;
            scoreCount += (int) Math.pow(2,n);
        }else{
            continue;
        }
    }

    //start clear game board
    if(scoreCount>=128){
        block[0][2] = 0;
        block[1][1] = 0;
        block[2][0] = 0;
        scoreCount -= 128;
    }
    if(scoreCount>=64){
        block[0][0] = 0;
        block[1][1] = 0;
        block[2][2] = 0;
        scoreCount -= 64;
    }
    if(scoreCount>=32){
        block[0][2] = 0;
        block[1][2] = 0;
        block[2][2] = 0;
        scoreCount -= 32;
    }
    if(scoreCount>=16){
        block[0][1] = 0;
        block[1][1] = 0;
        block[2][1] = 0;
        scoreCount -= 16;
    }
    if(scoreCount>=8){
        block[0][0] = 0;
        block[1][0] = 0;
        block[2][0] = 0;
        scoreCount -= 8;
    }
    if(scoreCount>=4){
        block[2][0] = 0;
        block[2][1] = 0;
        block[2][2] = 0;
        scoreCount -= 4;
    }
    if(scoreCount>=2){
        block[1][0] = 0;
        block[1][1] = 0;
        block[1][2] = 0;
        scoreCount -= 2;
    }
    if(scoreCount>=1){
        block[0][0] = 0;
        block[0][1] = 0;
        block[0][2] = 0;
        scoreCount -= 1;
    }
}
}
}

我得到的错误信息

tryingtoedit.java:42: error: illegal start of expression
    private static final boolean isComplete(int[][] grid) {
    ^
tryingtoedit.java:42: error: illegal start of expression
    private static final boolean isComplete(int[][] grid) {
            ^
tryingtoedit.java:42: error: ';' expected
    private static final boolean isComplete(int[][] grid) {
                  ^
tryingtoedit.java:42: error: ';' expected
    private static final boolean isComplete(int[][] grid) {
                                           ^
tryingtoedit.java:42: error: ';' expected
    private static final boolean isComplete(int[][] grid) {
                                                        ^
5 errors

Tool completed with exit code 1

【问题讨论】:

  • 使用 IDE,格式化你的代码。
  • “正在尝试编译” 始终复制/粘贴错误和异常输出。
  • 阅读错误信息。看不懂就贴吧。
  • 错误信息是非法的表达式开头,私有静态最终布尔 isComplete(int[][] grid) {
  • 你不能把一个方法放在另一个方法里面。

标签: java private stringbuilder public-method


【解决方案1】:

您缺少括号和/或放错了括号。再次检查您的代码。

例如,您的 isComplete 方法位于 main 方法的 while 循环内。

如果您使用正确的缩进来确定需要放置括号的位置会有所帮助。

【讨论】:

  • 抱歉,刚接触编程。我已经尝试通过放置结束括号来关闭第二种方法“Is”Complete”,但是仍然收到第二种方法的非法表达式开头的错误。
  • 现在你已经把你的isComplete 方法放在你的主方法里面,这是不允许的。因此,在您的 isComplete 方法之前,您需要关闭 main 方法中的所有括号。数一数,开括号的数量必须等于闭括号的数量。
  • 如果我关闭了我的main方法,是不是意味着我的变量不能在main方法的末尾使用?
  • 你在main方法中定义的只能在那个方法中使用。
猜你喜欢
  • 2011-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-19
  • 1970-01-01
  • 1970-01-01
  • 2012-05-14
  • 1970-01-01
相关资源
最近更新 更多