简体   繁体   English

foreach变量导致“<identifier> 预期的“编译错误</identifier>

[英]foreach variable causing "<identifier> expected" error on compile

I am working on a homework assignment for an Intro to Java class. When I try and compile my code I am getting a compilation error.我正在为 Java class 的介绍做家庭作业。当我尝试编译我的代码时,出现编译错误。 I am sure it is something simple, but I cannot figure out the cause of the error.我确信这很简单,但我无法找出错误的原因。

Note, the errors I am receiving are under the source code.请注意,我收到的错误在源代码下。

/* 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);
  }
}

These are the errors I get on compile这些是我在编译时遇到的错误

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.");}

Use System.out.println("You got question " + index2 + " correct;");} instead of使用System.out.println("You got question " + index2 + " correct;");}而不是

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

Java uses + for concatenation. Java 使用 + 进行连接。

In JAVA the concatenation simbol is not "."在 JAVA 中,连接符号不是“.” but "+".但是“+”。
Use System.out.println("You got question " + index2 + " correct;");}使用System.out.println("You got question " + index2 + " correct;");}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM