我有一个带有数字的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("*");
}
}
}
答案 0 :(得分:2)
请尝试更具体一点。什么是输入,什么是所需的输出?如果以下情况对您没有帮助,我希望我能帮助您。
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();
答案 1 :(得分:0)
预期输出为*取决于每个数字。