忽略内容,但下面是我的代码,我不断得到解析错误,但在我看来,我的所有括号都已正确关闭,我已经一次又一次地查看它,也许我只是在这里吃完火腿,任何帮助将不胜感激。
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");
}
}
}
答案 0 :(得分:3)
您的方法括号已关闭。
我看到的第一件事,你在这里有错误:
public static void main(String[] args);
应该是
public static void main(String[] args) {
--------------
--------------
}
答案 1 :(得分: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");
}
}
}
答案 2 :(得分:0)
以else
开头(但不是else if
)的每一行都有{
,但没有匹配的}
。
答案 3 :(得分:0)
考虑使用for
循环和array
来保存答案。你真的根本不需要数组来解决这个问题,但如果你想要值,你可以匹配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++;
}
}