Я пытаюсь распечатать бейсболистов на базовый процент.Пока код работает отлично.Единственная проблема заключается в том, что, когда я распечатываю OBP для каждого года, я не могу подобрать правильный год, который соответствует году ввода пользователя.Каждый раз, когда он зацикливается в методе printOnBasePercentage (), он увеличивает год на единицу.Есть ли способ, которым я могу решить эту проблему?Благодарю.
Я пытался добавить +startYear++
, но это не помогло.Это приблизило меня.
public static void main(String[] args) {
int numYears;
double [] years;
String name;
int startYear;
double oBP;
int hits, walks, sacFlies, hitsByPitch, atBats;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter name of baseball player: ");
name = keyboard.nextLine();
System.out.print("Enter the number of years " + name +" has been playing: ");
numYears = keyboard.nextInt();
years = new double[numYears];
System.out.print("Enter " +name +" first year on the team: ");
startYear = keyboard.nextInt();
for (int index = 0; index < years.length; index++) {
System.out.print("For Year: "+ startYear++);
System.out.print("\nEnter how many hits the player has: ");
hits = keyboard.nextInt();
System.out.print("Enter the number of walks the player has: ");
walks = keyboard.nextInt();
System.out.print("Enter the number of sacrifice flies the player has: ");
sacFlies = keyboard.nextInt();
System.out.print("Enter the number of hits by pitch the player has: ");
hitsByPitch = keyboard.nextInt();
System.out.print("Enter the amount of at bats the player has: ");
atBats = keyboard.nextInt();
years[index] = ((hits + walks + hitsByPitch) / atBats+ walks+ hitsByPitch +sacFlies);
}
printOnBasePercentage(name, startYear, years);
}
public static void printOnBasePercentage(String name, int startYear, double []years){
for (int index = 0; index < years.length; index++){
System.out.println("\n" + name + "'s On Base Percentage");
System.out.printf("For Year: " +startYear + " " + "%.3f", years[index]);
}
}