0

我正在为 Java 入门课程做家庭作业。当我尝试编译我的代码时,我得到一个编译错误。我确信这很简单,但我无法找出错误的原因。

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

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

2 回答 2

2

Use System.out.println("You got question " + index2 + " correct!");} instead of

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

Java uses + for concatenation.

于 2012-04-09T06:17:43.533 回答
2

In JAVA the concatenation simbol is not "." but "+".
Use System.out.println("You got question " + index2 + " correct!");}

于 2012-04-09T06:18:21.957 回答