gpt4 book ai didi

java - 根据txt文件夹打印星星

转载 作者:行者123 更新时间:2023-11-30 03:07:14 24 4
gpt4 key购买 nike

我有一个包含数字的 txt 文件,我必须读取该文件并根据给定的数字打印星星。我该怎么做?

public class Testing
{
public static void main(String [] args)

throws IOException
{

FileInputStream fileInputStream = new FileInputStream("numbers.txt");
Scanner scanner = new Scanner(fileInputStream);


double doubleNum=(scanner.nextDouble());
int intNumb=(int) doubleNum;
for (int i=0;i<intNumb;i++)
while (scanner.hasNextInt())


{
System.out.println("*");
}
}
}

最佳答案

请尝试更具体。输入是什么以及期望的输出是什么?如果以下内容对您没有帮助,我希望能够帮助您。

FileInputStream fileInputStream = new FileInputStream("numbers.txt");
Scanner scanner = new Scanner(fileInputStream);


while (scanner.hasNextDouble()) {
int num = Double.valueOf(scanner.nextDouble()).intValue();
for (int i = 0; i < num; i++) {
System.out.print("*");
}
System.out.println();
}

scanner.close();

如果您需要输出类似于 **.*** 例如40.001

FileInputStream fileInputStream = new FileInputStream("numbers.txt");
Scanner scanner = new Scanner(fileInputStream);

while (scanner.hasNext()) {
// scanner.next() gets the next token
// replaceAll() replaces every digits with * (\d is equal to [0-9] and it needs to be escaped with \ giving \\d)
System.out.println(scanner.next().replaceAll("\\d", "*"));
}

scanner.close();
fileInputStream.close();

关于java - 根据txt文件夹打印星星,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34453072/

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