我已尝试查看其他主题并尝试自行修复此错误。你能帮我解决下面的Java代码吗?
我收到以下错误error: illegal start of expression
很抱歉,如果我没有正确发布这个问题,我就不知道在这个论坛上哪里可以找到行编号功能。
import java.util.*;
public class PracFive {
private static boolean continueGame()
{
System.out.println("Do you want to play again? (Y/N)");
String answer = new Scanner(System.in).next();
return answer.trim().toLowerCase().equals("y");
}
private static void playGame(List<String> names)
{
System.out.println();
System.out.println("List size: " + names.size());
Random rand = new Random();
int guessInt = rand.nextInt(names.size());
String ComputersGuess = names.get(guessInt);
boolean isWrong = false;
int trial = 0;
Scanner input = new Scanner(System.in);
System.out.println("A random color was chosen choose which one: ");
while (isWrong)
{
trial++;
String humanGuess = input.next();
if (humanGuess.trim().toLowerCase().equals(ComputersGuess.toLowerCase()))
{
isWrong = false;
System.out.println("Yeey! you got it");
names.remove(guessInt);
}else{
System.out.println("Try again, dude!");
}
if (trial == 1){
String firstletter = ComputersGuess.substring(0,1);
System.out.println("First Letter is: " + firstletter);
}else if(trial == 2){
System.out.println("Number of letters: " + ComputersGuess);
}else if(trial == 3){
isWrong = false;
}
}
public static main(String[] args)
{
List<String> names = new ArrayList<>();
names.add("Yellow");
names.add("Red");
names.add("Blue");
names.add("Maroon");
names.add("Pink");
names.add("Purple");
names.add("Black");
names.add("Orange");
names.add("White");
names.add("Green");
System.out.format("%d\t\t%s","Index", "Color name");
System.out.println();
int count = 0;
for(String name: names)
{
System.out.format("%d\t\t%s",count++,name);
}
playGame(names);
boolean isInterested = continueGame();
while(isInterested)
{
playGame(names);
isInterested = continueGame();
}
}
答案 0 :(得分:0)
您收到错误是因为您尝试将字符串格式化为double。你的问题就在这里
System.out.format(“%d \ t \ t%s”,“索引”,“颜色名称”);
删除或修复此行,并修复其他人提及的其他问题。