Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

arrays - Golf Score Tally Program Java - Stuck

So for my intro to Java course, I am supposed to create a simply golf-scoring program using arrays that looks like this:

(I'm stuck on the bottom portion when it comes to scoring all of my golf scores and tallying the results of the arrays...)

How many holes did you play? 9

Hole Pars

What is par for hole 1? 5

What is par for hole 2? 4

What is par for hole 3? 4

etc.

Your Scores

What did you shoot on hole 1? 4

What did you shoot on hole 2? 5

What did you shoot on hole 3? 3

etc.


Your scoring summary includes:

Total score: Par

Total holes-in-one: 0

Total number of double eagles (albatross): 0

Total number of eagles: 0

Total number of birdies: 5

Total number of pars: 1

Total number of bogies: 1

Total number of double bogies: 2

Total number of holes at 3 or more over par: 0

Here's the code I have to far,

/* Lab 4: Part 2 - Golf Score */

import java.io.*;
import java.text.*;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class golf_score2
{
 public static void main(String[] args) throws IOException
 {

  Scanner m= new Scanner(System.in);
  Scanner q= new Scanner(System.in);

  BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

  System.out.print("How many holes did you play?");

  int h = m.nextInt();
  int hArray[] = new int[h];
  int pArray[] = new int[h];

  // Asks the user to enter par for i number of holes

  System.out.print("
Hole Pars");

  for(int i=0;i <h;i++)
   {
   System.out.print("

What is par for hole " + (i+1) + "? ");
   hArray[i]=m.nextInt();
   }

  // Asks the user to enter score for i holes

  System.out.print("

Your Scores");

  for(int i=0; i <h;i++)
   {
   System.out.print("

What did you shoot on hole " + (i+1) + "? ");
   pArray[i]=m.nextInt();
   }

  System.out.print("

---------------------------");
  System.out.print("

Your scoring summary includes:");

  score_result = pArray[i] - hArray[i];

  System.out.print(score_result);

 }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like you need to use another for loop to make sure you're tallying each score:

for(int i = 0; i < h; i++) {
    score_result = pArray[i] - hArray[i];
    System.out.print(score_result);
}

If you just want the final score it would look something like this:

int final_score = 0;
for(int i = 0; i < h; i++) {
    score_result = pArray[i] - hArray[i];
    final_score += score_result;
}
System.out.println(final_score);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...