강사가 저에게 준 숙제 중 하나는 야구 통계 프로그램이었습니다. stats.dat라는 파일에서 읽습니다.이 파일에는 야구 선수 이름의 이름과 박쥐에있을 때 일어난 일의 목록이 들어 있습니다. 그것은 그들의 이름과 외출 (o), 히트 (h), 걷기 (w) 및 희생 플라이 (s)를 읽고 프린트합니다. 이 파일에 포함 된 내용입니다 :파일에서 통계 읽기
Willy Wonk,o,o,h,o,o,o,o,h,w,o,o,o,o,s,h,o,h
Shari Jones,h,o,o,s,s,h,o,o,o,h,o,o,o
Barry Bands,h,h,w,o,o,o,w,h,o,o,h,h,o,o,w,w,w,h,o,o
Sally Slugger,o,h,h,o,o,h,h,w
Missy Lots,o,o,s,o,o,w,o,o,o
Joe Jones,o,h,o,o,o,o,h,h,o,o,o,o,w,o,o,o,h,o,h,h
Larry Loop,w,s,o,o,o,h,o,o,h,s,o,o,o,h,h
Sarah Swift,o,o,o,o,h,h,w,o,o,o
Bill Bird,h,o,h,o,h,w,o,o,o,h,s,s,h,o,o,o,o,o,o
Don Daring,o,o,h,h,o,o,h,o,h,o,o,o,o,o,o,h
Jill Jet,o,s,s,h,o,o,h,h,o,o,o,h,o,h,w,o,o,h,h,o
지금까지 내가 꽤 각 행이 무엇을하는지 이해하지 않더라도, 아래 기본적인 생각을 가지고 (나는 내 수업이 책에 프로그램의 일부 코드를 수정 읽기는 텍스트 파일에있는 URL을 인쇄 한 다음 /로 분리 된 URL의 각 부분을 인쇄합니다. 나는 프로그램에서 플레이어의 이름을 출력 할 수 있도록 해 놓았지만, 히트, 아웃, 걷기, 그리고 그들이받는 희생의 양을 출력하는 방법에 대해 잘 모르겠습니다. 난 (
import java.util.Scanner;
import java.io.*;
public class BaseballStats
{
public static void main(String [] args) throws IOException
{
int hit = 0, walk = 0, sac = 0, out = 0, length = 0, wholeLength = 0;
Scanner fileScan, lineScan, statScan;
String fileName, playerName, line, stats, playerStats;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the name of the file: ");
fileName = scan.nextLine();
fileScan = new Scanner(new File(fileName));
while (fileScan.hasNext())
{
System.out.println();
line = ("Player: " + fileScan.nextLine());
wholeLength = line.length();
lineScan = new Scanner(line);
lineScan.useDelimiter(",");
stats = lineScan.next();
statScan = new Scanner(stats);
statScan.useDelimiter(",");
while (statScan.hasNext())
{
System.out.println(statScan.next());
length = stats.length() - 1;
for (int i = 0; i < length; i++)
{
if (stats.charAt(i) == 'h')
hit++;
else if (stats.charAt(i) == 'o')
out++;
else if (stats.charAt(i) == 'w')
walk++;
else if (stats.charAt(i) == 's')
sac++;
}
}
System.out.println("Hits: " + hit + "\nOuts: " + out + "\nWalks: " + walk + "\nSacrifice flies: " + sac);
}
}
}
: 지금까지이 라인 중 1 개 문자를 읽는 것, 그것은 다음 플레이어로 다운되면 다음 2, 3 출력한다, 등 여기에 지금까지 그것을 가지고있는 코드입니다 내 코드에서 마지막 문장의 마지막 부분을 편집자에게 올바르게 표시하는 데 어려움을 겪습니다. 조금 이상하게 보일 경우 미안합니다.) 무엇이 잘못되었는지 궁금해했으며 지금까지는 이해할 수 없습니다. 올바른 길로 가야 할 것이 있습니까?
for-loop 전에 통계를 인쇄 해보십시오. 힌트를 얻을 수 있습니다. – Rickard
필자는 개인적으로 'line.split (",")'을 사용하고 스캐너로 엉망이 아닌 결과 배열로 작업합니다. –