-4

忽略内容,但下面是我的代码,我不断收到解析错误,但在我看来,我的所有括号都正确关闭,我一遍又一遍地查看它,也许我只是在这里吃火腿,任何帮助将不胜感激。

class java_ca

{

public static void main(String[] args);

        char ans1, ans2, ans3, ans4, ans5, ans6, ans7, ans8, ans9, ans10, ans11, ans12;
        int totaly, totaln;



                {while ((ans1 != 'y') && (ans1 != 'n'))

                    {
                        System.out.println("1) Do You like Computers?");
                        ans1=Keyboard.readChar();
                            if(ans1=='y'){totaly++;}
                            else if(ans1=='n'){totaln++;}
                            else{System.out.println("That is an invalid selection, please enter y or n to answer the questions");

                    }
                    {
                        System.out.println("1) Do You like Computers?");
                        ans2=Keyboard.readChar();
                            if(ans2=='y'){totaly++;}
                            else if(ans2=='n'){totaln++;}
                            else{System.out.println("That is an invalid selection, please enter y or n to answer the questions");

                    }
                    {
                        System.out.println("1) Do You like Computers?");
                        ans3=Keyboard.readChar();
                            if(ans3=='y'){totaly++;}
                            else if(ans3=='n'){totaln++;}
                            else{System.out.println("That is an invalid selection, please enter y or n to answer the questions");

                    }
                    {
                        System.out.println("1) Do You like Computers?");
                        ans4=Keyboard.readChar();
                            if(ans4=='y'){totaly++;}
                            else if(ans4=='n'){totaln++;}
                            else{System.out.println("That is an invalid selection, please enter y or n to answer the questions");

                    }
                    {
                        System.out.println("1) Do You like Computers?");
                        ans5=Keyboard.readChar();
                            if(ans5=='y'){totaly++;}
                            else if(ans5=='n'){totaln++;}
                            else{System.out.println("That is an invalid selection, please enter y or n to answer the questions");

                    }
                    {
                        System.out.println("1) Do You like Computers?");
                        ans6=Keyboard.readChar();
                            if(ans6=='y'){totaly++;}
                            else if(ans6=='n'){totaln++;}
                            else{System.out.println("That is an invalid selection, please enter y or n to answer the questions");

                    }
                    {
                        System.out.println("1) Do You like Computers?");
                        ans7=Keyboard.readChar();
                            if(ans7=='y'){totaly++;}
                            else if(ans7=='n'){totaln++;}
                            else{System.out.println("That is an invalid selection, please enter y or n to answer the questions");

                    }
                    {
                        System.out.println("1) Do You like Computers?");
                        ans8=Keyboard.readChar();
                            if(ans8=='y'){totaly++;}
                            else if(ans8=='n'){totaln++;}
                            else{System.out.println("That is an invalid selection, please enter y or n to answer the questions");

                    }
                    {
                        System.out.println("1) Do You like to read?");
                        ans9=Keyboard.readChar();
                            if(ans9=='y'){totaly++;}
                            else if(ans9=='n'){totaln++;}
                            else{System.out.println("That is an invalid selection, please enter y or n to answer the questions");

                    }
                    {
                        System.out.println("10. Did you like this surevy?");
                        ans10=Keyboard.readChar();
                            if(ans10=='y'){totaly++;}
                            else if(ans1=='n'){totaln++;}
                            else{System.out.println("That is an invalid selection, please enter y or n to answer the questions");
                    }
            }
}
4

4 回答 4

3

您的方法大括号已关闭。

我看到的第一件事,你在这里有错误:

public static void main(String[] args);

它应该是

public static void main(String[] args) {

    --------------
    --------------

}
于 2013-11-04T17:07:01.030 回答
1

你在错误的地方大量过量{},你的代码看起来像是复制粘贴工作出错的结果,然后在错误的地方修复(即,通过添加东西而不是删除东西)。

它或多或少应该是这样的。请不要这样 1)我仍然对程序逻辑本身有疑问 2)因为我怀疑这是家庭作业,还有很多工作可以纠正其他逻辑错误,我试图只关注这个{}问题。正如@Mark 已经说过的那样:正确格式化您的代码或使用为您格式化代码的编辑器,它将帮助您快速发现此类问题。

public static void main(String[] args) {
    char ans1, ans2, ans3, ans4, ans5, ans6, ans7, ans8, ans9, ans10, ans11, ans12;
    int totaly, totaln;


    while ((ans1 != 'y') && (ans1 != 'n')) {
        System.out.println("1) Do You like Computers?");
        ans1 = Keyboard.readChar();
        if (ans1 == 'y') {
            totaly++;
        } else if (ans1 == 'n') {
            totaln++;
        } else {
            System.out.println("That is an invalid selection, please enter y or n to answer the questions");

        }

        System.out.println("1) Do You like Computers?");
        ans2 = Keyboard.readChar();
        if (ans2 == 'y') {
            totaly++;
        } else if (ans2 == 'n') {
            totaln++;
        } else {
            System.out.println("That is an invalid selection, please enter y or n to answer the questions");

        }
        System.out.println("1) Do You like Computers?");
        ans3 = Keyboard.readChar();
        if (ans3 == 'y') {
            totaly++;
        } else if (ans3 == 'n') {
            totaln++;
        } else {
            System.out.println("That is an invalid selection, please enter y or n to answer the questions");

        }
        System.out.println("1) Do You like Computers?");
        ans4 = Keyboard.readChar();
        if (ans4 == 'y') {
            totaly++;
        } else if (ans4 == 'n') {
            totaln++;
        } else {
            System.out.println("That is an invalid selection, please enter y or n to answer the questions");

        }
        System.out.println("1) Do You like Computers?");
        ans5 = Keyboard.readChar();
        if (ans5 == 'y') {
            totaly++;
        } else if (ans5 == 'n') {
            totaln++;
        } else {
            System.out.println("That is an invalid selection, please enter y or n to answer the questions");

        }
        System.out.println("1) Do You like Computers?");
        ans6 = Keyboard.readChar();
        if (ans6 == 'y') {
            totaly++;
        } else if (ans6 == 'n') {
            totaln++;
        } else {
            System.out.println("That is an invalid selection, please enter y or n to answer the questions");

        }
        System.out.println("1) Do You like Computers?");
        ans7 = Keyboard.readChar();
        if (ans7 == 'y') {
            totaly++;
        } else if (ans7 == 'n') {
            totaln++;
        } else {
            System.out.println("That is an invalid selection, please enter y or n to answer the questions");

        }
        System.out.println("1) Do You like Computers?");
        ans8 = Keyboard.readChar();
        if (ans8 == 'y') {
            totaly++;
        } else if (ans8 == 'n') {
            totaln++;
        } else {
            System.out.println("That is an invalid selection, please enter y or n to answer the questions");

        }
        System.out.println("1) Do You like to read?");
        ans9 = Keyboard.readChar();
        if (ans9 == 'y') {
            totaly++;
        } else if (ans9 == 'n') {
            totaln++;
        } else {
            System.out.println("That is an invalid selection, please enter y or n to answer the questions");

        }
        System.out.println("10. Did you like this surevy?");
        ans10 = Keyboard.readChar();
        if (ans10 == 'y') {
            totaly++;
        } else if (ans1 == 'n') {
            totaln++;
        } else {
            System.out.println("That is an invalid selection, please enter y or n to answer the questions");
        }
    }
}
于 2013-11-04T17:20:42.893 回答
0

考虑使用for循环和 anarray来保存答案。对于这个问题,你真的根本不需要数组,但如果你想要值,你可以与你的ans.

char[] array = new char[12];

for (int i = 0; i < 12; i++){
    System.out.println("1) Do You like Computers?");

     array[i] = Keyboard.readChar();

     while (array[i] != 'y' && array[i] != 'n'
         && array[i] != 'Y' && array[i] != 'N'){

         System.out.println("That is an invalid selection," + 
                            "please enter y or n to answer the questions");

         array[i] = Keyboard.readChar();
     }

     if(array[i] == 'y' || array[i] == 'Y'){
         totaly++;
     }
     else {
         totaln++;
     }

}
于 2013-11-04T17:27:21.360 回答
0

else以(但不是)开头的每一行else if都有一个{不匹配的}

于 2013-11-04T17:10:40.553 回答