사용자가 지정한 숫자까지 pi의 숫자를 인쇄하는 프로그램을 만들고 있습니다. 사용자로부터 입력을 읽을 수는 있지만 텍스트 파일을 읽을 수는 있지만 숫자를 인쇄하면 잘못된 번호가 인쇄됩니다.읽기 정수를 출력 할 수 없습니다 : java
"Pi.txt"에는 "3.14159"가 포함됩니다. 여기 내 코드입니다 :이 출력됩니다
package pireturner;
import java.io.*;
import java.util.Scanner;
class PiReturner {
static File file = new File("Pi.txt");
static int count = 0;
public PiReturner() {
}
public static void readFile() {
try {
System.out.print("Enter number of digits you wish to print: ");
Scanner scanner = new Scanner(System.in);
BufferedReader reader = new BufferedReader(new FileReader(file));
int numdigits = Integer.parseInt(scanner.nextLine());
int i;
while((i = reader.read()) != -1) {
while(count != numdigits) {
System.out.print(i);
count++;
}
}
} catch (FileNotFoundException f) {
System.err.print(f);
} catch (IOException e) {
System.err.print(e);
}
}
public static void main(String[] args) {
PiReturner.readFile();
}
}
"515151"만약 그들이 인쇄 할 자리의 수와 사용자 입력 3. 왜 이런 일을하는지 모르겠다. 오류가없고 내가 읽는 방법을 테스트 했으므로 제대로 작동하지 않기 때문에 내가 뭘 잘못하고 있는지 확신 할 수 없다. 어떤 도움이라도 기꺼이 감사 할 것입니다. 미리 감사드립니다.
덧붙여서 char에 정수 'i'를 캐스팅하면 333이 출력됩니다 (입력이 3이라고 가정).
... 출력 numdigit 시간 대신
3 전에 왼쪽되지 않습니다. – Mesop