expected"错误-6ren"> expected"错误-我正在为 Java 入门类(class)做家庭作业。当我尝试编译代码时,出现编译错误。我确信这很简单,但我无法找出错误的原因。 请注意,我收到的错误位于源代码下。 /* Logical Design -6ren">
gpt4 book ai didi

java - foreach 变量在编译时导致 " expected"错误

转载 作者:行者123 更新时间:2023-12-01 19:06:05 24 4
gpt4 key购买 nike

我正在为 Java 入门类(class)做家庭作业。当我尝试编译代码时,出现编译错误。我确信这很简单,但我无法找出错误的原因。

请注意,我收到的错误位于源代码下。

/* Logical Design

start
// Declarations
final String QUESTIONS[] = new String{"Please input your answer using A, B, or C.\nWho is the coolest guy ever? \n\nA. Max\nB. James Bond\nC. Burt Reynolds", "Please input your answer using A, B, or C.\nWho do coolguys' drive?\n\nA. Datuns\nB. Panters\nC. Porsches", "Please input your answer using A, B, or C.\nWhat is the coolest city?\n\nA. Los Angles\nB. Denver\nC.Boulder"};
final String CONGRATS = "You got it right!"
final string FAIL = "Horribly. Horribly wrong. =["
final String ANSWERS[] = new String{"A","A","A"};
final String QUIT = "QUIT";
String tempanswer[] = new String[3]
int index;
int index2;
String keeplaying;


Output "Would you like to play?"
input keeplaying
if(keepplaying == QUIT) {
return;
} else {
for(index = 0; index<QUESTIONS.length; index++) {
Output QUESTIONS[index]
input tempanswer[index];
for(index2 = 0; index2<ANSWERS.length; index2++) {
if tempanswer == ANSWERS[index2] {
output CONGRATS } else { output FAIL }
}
}
}
stop
*/

import javax.swing.*;
import java.awt.event.*;

public class FunQuiz
{
public static void main(String args[])
{
final String QUESTIONS[] = {"Please input your answer using A, B, or C.\nWho is the coolest guy ever? \n\nA. Max\nB. James Bond\nC. Burt Reynolds", "Please input your answer using A, B, or C.\nWhat do coolguys' drive?\n\nA. Datuns\nB. Panters\nC. Porsches", "Please input your answer using A, B, or C.\nWhat is the coolest city?\n\nA. Los Angles\nB. Denver\nC.Boulder"};
final String ANSWERS[] = {"A","A","A"};
final String QUIT = "QUIT";
String tempanswer[] = new String[3];
int index;
int index2;
String keeplaying;

keepplaying = JOptionPane.showInputDialog("Would you like to play?");
if(keepplaying == QUIT) {
return;
} else {
for(index = 0; index<QUESTIONS.length; index++) {
System.out.println(QUESTIONS[index]);
JOptionPane.showInputDialog(tempanswer[index]);
for(index2 = 0; index2<ANSWERS.length; index2++) {
if(tempanswer == ANSWERS[index2]) {
System.out.println("You got question " . index2 . " correct!");} else { System.out.println("You got question " . index2 . " incorrect.");}
}
}
}

System.exit(0);
}
}

这些是我在编译时遇到的错误

FunQuiz.java:57: <identifier> expected
System.out.println("You got question " . index2 . " correct!");} else { System.out.println("You got question " . index2 . " incorrect.");}
^
FunQuiz.java:57: ';' expected
System.out.println("You got question " . index2 . " correct!");} else { System.out.println("You got question " . index2 . " incorrect.");}

最佳答案

使用System.out.println("你有问题 "+ index2 + "正确!");} 而不是

System.out.println("You got question " . index2 . " correct!");};

Java 使用 + 进行连接。

关于java - foreach 变量在编译时导致 "<identifier> expected"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10069636/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com