|
|
Or, use your gamerDNA username: (more...)
| ||||||
| |
![]() |
| | LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
| | #76 (permalink) | |
| Registered User Join Date: Feb 2006
Posts: 1,657
+5 Internets | Quote:
| |
| | |
| | #77 (permalink) |
| Registered User Join Date: Aug 2006 Location: Raleigh, NC
Posts: 2,173
| Help...I'm stuck /because/ of my computeMax method. It's not generating the max of converges. /* * Created: *** put the date here *** * * Author: *** put your name here *** * * Program to compute Hailstone series. */ import java.util.Scanner; public class Lab5 { public static void main(String[] args) { Scanner in = new Scanner (System.in); int length, start, max; System.out.print("Would you like to compute a hailstorm series?"); String compute = in.nextLine(); if(compute.charAt(0) != 'Y' || compute.charAt(0) != 'y') { while((compute.charAt(0) == 'y' || compute.charAt(0) == 'Y')) { start = getStartingValue(in); length = computeLength(start); max = computeMax(start); outputResults(start, length, max); in.nextLine(); System.out.println(); System.out.println("Would you like to compute another hailstorm series?"); compute = in.nextLine(); } } System.out.println("Goodbye."); System.exit(0); } // Given a Scanner in, this method (repeatedly, if necessary) // prompts the user to enter a positive integer, inputs the // integer, and if it is positive returns it. Otherwise, is // keeps asking the user for a positve integer. private static int getStartingValue(Scanner in) { System.out.print("Enter a positive integer."); int x = in.nextInt(); if(x <= 0) { System.out.print("Enter a positive integer."); x = in.nextInt(); } return x; } // Do not declare the Scanner variable in this method. // You must use the value this method receives in the // argument (in). // Given a positive starting value x, this method generates the // corresponding Hailstone series and returns the length of the // series (i.e., how many steps it takes before the series hits 1). private static int computeLength(int x) { int length = 1; if (x == 1) { length++; } else { while(x > 1) { if(x%2 == 0) { x = x/2; length++; } else { x = (3 * x) + 1; length++; } } } return length; } // Given a positive starting value x, this method generates the // corresponding Hailstone series and returns the maximum value // in the series (i.e., the largest value it generates before // the series hits 1). private static int computeMax(int x) { int length = 1; int between; between = x; if(x == 1) { length = x; } else { while(x > 1) { if(x%2 == 0){ x = x/2; length++; } else { x = (3 * x) + 1; length++; if(between > x) { between = x; } } } } return between; } // Given the starting value, the length, and the max value in a // Hailstone series, this method outputs them with appropriate // messages. private static void outputResults(int start, int length, int max) { System.out.println("The generated series for starting value " + start); System.out.println("Series converged in " + length + " steps."); System.out.println("Largest value generated is " + max); } } |
| | |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
| |