在Java中获取表达式错误
我已经尝试查看其他线程,并尝试自己修复此错误。你能帮我写下面的Java代码吗? 我收到以下错误在Java中获取表达式错误,java,Java,我已经尝试查看其他线程,并尝试自己修复此错误。你能帮我写下面的Java代码吗? 我收到以下错误错误:表达式开始非法 对不起,如果我没有正确地发布这个问题,我不知道在这个论坛上哪里可以找到行号功能 import java.util.*; public class PracFive { private static boolean continueGame() { System.out.println("Do you want to play again? (Y/N)
错误:表达式开始非法
对不起,如果我没有正确地发布这个问题,我不知道在这个论坛上哪里可以找到行号功能
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();
}
}
import java.util.*;
公共课实习{
私有静态布尔continueGame()
{
System.out.println(“您想再次播放吗?(Y/N)”);
字符串应答=新扫描仪(System.in).next();
返回answer.trim().toLowerCase().equals(“y”);
}
私人静态void游戏(列表名称)
{
System.out.println();
System.out.println(“列表大小:+names.size());
Random rand=新的Random();
int guessInt=rand.nextInt(names.size());
字符串ComputersGuess=names.get(gusticint);
布尔值是错误的=错误的;
int-trial=0;
扫描仪输入=新扫描仪(System.in);
println(“随机选择了一种颜色,选择哪一种:”);
虽然(我错了)
{
试用++;
字符串humanGuess=input.next();
if(humanGuess.trim().toLowerCase().equals(ComputersGuess.toLowerCase()))
{
错误=错误;
System.out.println(“耶!你明白了”);
名称。删除(猜测);
}否则{
System.out.println(“再试一次,伙计!”);
}
如果(试验==1){
字符串firstletter=ComputersGuess.substring(0,1);
System.out.println(“第一个字母是:“+firstletter”);
}否则如果(试验==2){
System.out.println(“字母数:+计算机组”);
}否则如果(试验==3){
错误=错误;
}
}
公共静态main(字符串[]args)
{
列表名称=新的ArrayList();
名称。添加(“黄色”);
名称。添加(“红色”);
名称。添加(“蓝色”);
名称。添加(“褐红色”);
名称。添加(“粉色”);
名称。添加(“紫色”);
名称。添加(“黑色”);
名称。添加(“橙色”);
名称。添加(“白色”);
名称。添加(“绿色”);
System.out.format(“%d\t\t%s”、“索引”、“颜色名称”);
System.out.println();
整数计数=0;
for(字符串名称:名称)
{
System.out.format(“%d\t\t%s”,count++,name);
}
游戏(名称);
布尔isInterested=continueGame();
while(我感兴趣)
{
游戏(名称);
isInterested=continueGame();
}
}
由于试图将字符串格式设置为双精度,所以出现错误。问题就在这里
System.out.format(“%d\t\t%s”、“索引”、“颜色名称”)
删除或修复这一行,并修复其他人提到的其他问题。我不知道在这个论坛上哪里可以找到行编号功能-您可以在有问题的行末尾添加注释,例如//几个大括号{
,}
不匹配,缺少。在使用main()
方法关闭playGame()之前,和main
的返回类型必须是void
您似乎缺少}
method。您是否使用IntelliJ或Eclipse之类的IDE?如果您粘贴了PracFive.java文件中的内容,那么您需要在
main`方法之前添加额外的
}(这将关闭playGame
),并且应该向main
方法添加返回类型,例如public static void main(String[]args)
请从控制台粘贴错误日志