将字符串中的单词拆分为每个字母
所以我基本上是在尝试用java制作wordle(如果你不知道它是什么,请查找它是一个有趣的文字游戏),并且我有一个包含一堆单词的文本文件。我使用缓冲阅读器随机选择其中一个单词,这将是您必须猜测的单词,我不知道您在猜测单词后如何检查每个字母。我认为我需要将字符串拆分为单独的字母,因此我尝试将 line.toCharArray()
行作为字符串名称,但这不起作用。这是我读到该行的代码
int min = 1;
int max = 101;
Random r = new Random();
int value = r.nextInt(max-min) + min;
int lines = 0;
File text = new File("text.txt");
try (BufferedReader br = new BufferedReader(new FileReader(text)))
{
String line = null;
while ((line = br.readLine()) != null) {
lines++;
if(lines == value)
line.toCharArray()
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
So i am basically trying to make wordle in java(if you don't know what it is look it up its a fun word game), and I have a text file of a bunch of words. I randomly pick 1 of the words using a bufferd reader which would be the word you have to guess, I don't know how you would be able to check each letter after you guess a word. I think I need to split the string up into individual letters so I tried doing this line.toCharArray()
line being the string name, but that didn't work. here is the code from where i read the line
int min = 1;
int max = 101;
Random r = new Random();
int value = r.nextInt(max-min) + min;
int lines = 0;
File text = new File("text.txt");
try (BufferedReader br = new BufferedReader(new FileReader(text)))
{
String line = null;
while ((line = br.readLine()) != null) {
lines++;
if(lines == value)
line.toCharArray()
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
String.toCharArray()
方法返回char[]
并且不会修改原始字符串参考:Java 文档
String.toCharArray()
method returnschar[]
and won't modify the original stringReference: Java doc
tl;dr
避免
char
作为 16 位值,
char
类型在物理上无法表示大多数字符。自 Java 2 以来,char
类型已基本上被破坏,并且自 Java 5 以来已被遗留。代码点
而是使用 代码点整数。代码点是 Unicode 联盟永久分配给每个字符的数字。
下面的代码用于获取某些文本的代码点作为数组。此代码生成一个
IntStream
来访问每个字符的代码点。然后我们将该流的元素收集到一个int
原始数字数组中。现在您可以循环这些代码点编号。
您可以通过多种方式从代码点数字生成文本。
这是一个使用
Character.toString
的方法。另一个是调用
StringBuilder#appendCodePoint
。顺便说一句,
ThreadLocalRandom
类比Random
类具有更方便的方法。这:
……变成这样,不需要任何数学。
提示:努力为变量命名,而不是“值”。
tl;dr
Avoid
char
As a 16-bit value,
char
type is physically incapable of representing most characters. Thechar
type has been essentially broken since Java 2, and legacy since Java 5.Code points
Instead use code point integer numbers. A code point is the number permanently assigned to each character by the Unicode Consortium.
Here is code to get the code points of some text as an array. This code generates an
IntStream
to access each character’s code point. Then we collect the elements of that stream into an array ofint
primitive numbers.Now you can loop those code point numbers.
You can generate text from the code point numbers in various ways.
Here’s one, using
Character.toString
.And another is calling
StringBuilder#appendCodePoint
.By the way, the
ThreadLocalRandom
class has more convenient methods thanRandom
.This:
… becomes this, without any math needed.
Tip: Work on better naming of your variables than “value”.