Results 1 to 7 of 7
Thread: Help needed for Diving Program
- 10-20-2012, 08:25 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 20
- Rep Power
- 0
Help needed for Diving Program
Hey guys,
I need some help on this assignment Question 4/5:
http://www.site.uottawa.ca/~mseha092...2_Fall2012.pdf
So basically this is what I have done:
For some reason, the person never jumps. Basically since each step forward is two steps and each step backward is one step, after the difference between the two the guy needs to have 10 steps forward in order to jump off but that never happens. If anyone knows what I am doing wrong, any help would be appreciated.Java Code:// This is a template to be used for all single-class ITI 1120 programs. /** * Replace this comment with a description of your program. */ import java.util.Scanner; class A2Q5 // Replace 'Template' with your own algorithm name. { public static void main (String args[ ]) { // DECLARE VARIABLES/DATA DICTIONARY Scanner keyboard = new Scanner (System.in); int tMax = 0; String diveAgain = "y"; while (true) { if (!diveAgain.equals("y")) { break; } // READ IN GIVENS use our ITI1120 special class for keyboard input System.out.print ("Please enter the maximum time in seconds to make a dive: "); tMax = keyboard.nextInt(); dive (tMax); System.out.println (); System.out.println (); System.out.print ("Do you wish to make another dive attempt?(y/n): "); diveAgain = keyboard.nextLine(); // BODY OF ALGORITHM - Call to the method to solve problem // PRINT OUT RESULTS AND MODIFIEDS } } // Problem Solving Method // Description: Short description of what the // method does, // Parameters (GIVENS): // A description of variables in the parameter list public static void dive(int tMax) { // DECLARE VARIABLES/DATA DICTIONARY // INTERMEDIATES8 double probability = 0; double distance = 0; double randomNum = 0; // RESULT char[] moves = new char[tMax]; // BODY OF ALGORITHM for (int i = 0; i<(moves.length-1); i++) { randomNum = 0 + Math.random(); System.out.println (randomNum); probability = 1 - (distance/10); System.out.println (probability); if (randomNum < probability) { moves[i] = ('F'); distance+=2; } else { moves[i] = ('B'); distance--; } } if (distance >= 10) { moves[moves.length-1] = ('D'); System.out.print ("The man jumped with the moves "); for (int i = 0; i <= moves.length-1; i++) { System.out.print (moves[i]); } System.out.print (" in " + (moves.length-1) + " seconds"); } else { System.out.print ("The man lost his nerve after the moves "); for (int i = 0; i <= moves.length-1; i++) { System.out.print (moves[i]); } } } } // Don't remove this brace bracket!
Thanks!
- 10-20-2012, 09:21 PM #2
Member
- Join Date
- Sep 2012
- Posts
- 34
- Rep Power
- 0
Re: Help needed for Diving Program
when i ran your code, the man did jump on some occasions, i guess it's just the random numbers,
- 10-20-2012, 09:33 PM #3
Member
- Join Date
- Sep 2012
- Posts
- 20
- Rep Power
- 0
Re: Help needed for Diving Program
Still not getting the man to jump. I think I know what the problem is though. I need to make the for loop exit when distance equals 10 because even when distance equals 10 it still continues and so the man moves backwards. So my question was, how can I make a for loop exit:
Java Code:if (randomNum < probability) { moves[i] = ('F'); distance+=2; if (distance == 10) { What would I put here to exit for loop? } } else { moves[i] = ('B'); distance--; }
- 10-20-2012, 09:42 PM #4
Member
- Join Date
- Sep 2012
- Posts
- 20
- Rep Power
- 0
Re: Help needed for Diving Program
Nvm, I added a break and it works. But now I am getting an output that looks like this:
The man jumped with the moves FFFFBFBF(16 blank spaces)D in 24 seconds.
How am I supposed to get rid of the blank spaces in the array since the arrays size is the size of the maximum time.
- 10-21-2012, 01:10 PM #5
Member
- Join Date
- Sep 2012
- Posts
- 34
- Rep Power
- 0
Re: Help needed for Diving Program
you can add 'D' at the moment he jumps and ignore the rest of the array
- 10-21-2012, 05:19 PM #6
Member
- Join Date
- Sep 2012
- Posts
- 20
- Rep Power
- 0
Re: Help needed for Diving Program
okay, but how do I ignore the rest of the array?
- 10-21-2012, 05:49 PM #7
Similar Threads
-
diving distance program
By maximumammo in forum New To JavaReplies: 4Last Post: 04-05-2012, 08:09 PM -
HELP NEEDED with J2ME program
By just_in_deed in forum CLDC and MIDPReplies: 11Last Post: 03-06-2012, 08:53 PM -
Diving into Java Web Services from .NET world
By mhalabi in forum New To JavaReplies: 0Last Post: 02-12-2012, 07:59 PM -
need some advice before diving into struts 2
By kulangotski in forum Web FrameworksReplies: 8Last Post: 04-26-2011, 04:52 AM -
Calender Program Help Needed!!
By DavidEvans in forum New To JavaReplies: 1Last Post: 03-17-2010, 06:37 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks