gpt4 book ai didi

java - 随机数无法解析为类型 - Java 本身的拼写错误

转载 作者:行者123 更新时间:2023-12-02 03:14:29 26 4
gpt4 key购买 nike

我已重新输入 Java fortune telling program. 的 LinkedIn 学习 Material

显然,我在随机关键字中没有拼写错误,但 Java 显示了有关 Radnom 的运行时错误。有人可以告诉我什么会导致这个错误吗?

错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:

Radnom cannot be resolved to a type

Type mismatch: cannot convert from String to ArrayList

Syntax error, insert "Finally" to complete TryStatement at customPrograms.FortuneTeller.main(FortuneTeller.java:15)

图像证明:

enter image description here

我的代码:

package customPrograms;
import java.util.Random;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.*;

public class FortuneTeller {

public static void main(String[] args) {
File inputFile = new File("answers.txt");
ArrayList<String> answers = new ArrayList<String>();
String answer, response = "y";
Random rand = new Random();
System.out.println(rand);
try {
Scanner input = new Scanner(inputFile);
while(input.hasNextLine())
{
answer = input.nextLine();
answers.add(answer);
}
} catch(Exception e) {
System.out.println("The input file \"answers.txt\" was not found." );
System.out.println(e.toString());
}

Scanner in = new Scanner(System.in);
while (response.equalsIgnoreCase("y"))
{
System.out.println("The fortune teller is ready for you \n"
+ "Please think about question in your mind \n and hit enter for your reply");
}
in.nextLine();
System.out.println("The fortune teller says: \n" + answers.get(rand.nextInt(answers.size())) +
"\"\n");
System.out.println("Do you have another question? (y/n)");
response = in.nextLine();
}
}

最佳答案

只要第15行是:

Random rand = new Random();

我想我已经弄清楚你的项目中发生了什么:你没有 .class文件与 .java 同步来源。

<小时/>

解决方案

项目菜单中:

选择构建项目选项。

标记自动构建后以保持文件同步。

enter image description here

关于java - 随机数无法解析为类型 - Java 本身的拼写错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40505487/

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