2 Replies - 1274 Views - Last Post: 04 March 2012 - 05:00 PM Rate Topic: -----

#1 JustTryin 757  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 24-February 12

Overlapping logic problem-nested While loop in try-catch block...

Posted 04 March 2012 - 04:32 PM

Greetings all-I said I would be back, and here I am. I have a while loop nested inside a try-catch block. Everything is working after the first loop around. I get the "Enter PAR for next hole: " text string fine, but on the next loop through I get a return of "Enter PAR for next hole " text string and "You are out of holes played" text string running back to back (or head to foot to be totally accurate). This continues until the "played" equals 0 is met and the game ends as it should, so the countdown is working, with additions. I know that this is some sort of flawed logic argument but am not seeing it. What I am seeing is that if variable "played" is greater than 0 (>0) to keep running the "Enter PAR for next hole" text string and when "played" is equal to 0 (==0) to run the "You are out of holes played" text string. But this is obviously not what is happening. As I said, the count down works, but after the first loop it adds the last condition and prints the "You are out of holes played" text string. Please disregard the commented out stuff as that is what comes after I get this part to run. What this is is a golf scorecard-user enters first the par for each hole played and then what they shot on each hole played. I will need to create an array to store the scores, but this step is first. Any help is deeply appreciated...thanks.

import java.io.*;


   public class Golf  {









       public static void main (String[] args)  {


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


        int hole = 0;
        int par = 0;
        int score = 0;
        int played = 0;
        int [] par_counter = new int[2];


System.out.println("How many holes did you play?"); //Golfer enters # holes played
        try {
        played = Integer.parseInt(scoring.readLine());
        }
        catch(IOException E) {
        System.out.println (E);
        }


System.out.println ("What is par for hole? ");
        try {
        par = Integer.parseInt(scoring.readLine());
        }
        catch(IOException E){
        System.out.println (E);
        }





        try {
        while (played > 0){
           System.out.println("Enter PAR for next  hole: ");
           played--;

           par = Integer.parseInt(scoring.readLine());


         if (played == 0);
            System.out.print("You are out of holes played");

        }

        }

        catch(IOException E) {
        System.out.println(E);
        }




          /*
          results [par_counter] = 0;
          hole++;
          */


          /*
          for (int print_counter =0; print_counter <3; print_counter++) {
           System.out.println("PAR for hole number " + (print_counter+1) + " is " + hole[print_counter]);
        }

          */


    }


}



This post has been edited by Atli: 04 March 2012 - 04:35 PM
Reason for edit:: Fixed the [code] tags. (The end tag should be [/code])


Is This A Good Question/Topic? 0
  • +

Replies To: Overlapping logic problem-nested While loop in try-catch block...

#2 btanne6  Icon User is offline

  • New D.I.C Head

Reputation: 13
  • View blog
  • Posts: 46
  • Joined: 24-November 11

Re: Overlapping logic problem-nested While loop in try-catch block...

Posted 04 March 2012 - 04:49 PM

remove this semicolon
         if (played == 0);

Was This Post Helpful? 1
  • +
  • -

#3 JustTryin 757  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 24-February 12

Re: Overlapping logic problem-nested While loop in try-catch block...

Posted 04 March 2012 - 05:00 PM

Boy I just hate it when I am stupid! :stupid:

Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1