I am using the following code for a hailstorm program, and cant quite figure out
ID: 3621168 • Letter: I
Question
I am using the following code for a hailstorm program, and cant quite figure out how to find the highest integer value reached in the series. Any help on this would be great! Thankyoupublic static void main(String[] args)
{
Scanner in = new Scanner (System.in);
int start;
int steps;
int max;
System.out.print("Would you like to compute a hailstone series?");
String answer = in.nextLine();
if(answer.charAt(0)!= 'Y' || answer.charAt(0) != 'y')
{
while((answer.charAt(0) == 'y' || answer.charAt(0) == 'Y'))
{
start = getStartingValue(in);
steps = computeLength(start);
max = computeMax(start);
outputResults(start, steps, max);
in.nextLine();
System.out.println();
System.out.println("Would you like to compute another series?: ");
answer = in.nextLine();
}
}
System.out.println("Goodbye.");
}
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;
}
private static int computeLength(int x)
{
int length = 1;
if (x == 1)
{
length= length + 1;
}
else
{
while(x > 1)
{
if(x%2 == 0)
{
x = x / 2;
length= length + 1;
}
else
{
x = (3 * x) + 1;
length= length + 1;
}
}
}
return length;
}
(this is the part I cant get!!!) private static int computeMax(int x)
{
int num1 = 1;
int num2= x;
if(x == 1)
{
num1 = x;
}
else
{
while(x > 1)
{
if(x%2 == 0)
{
x = x/2;
num1= num1 + 1;
}
else
{
x = (3 * x) + 1;
num1= num1 + 1;
}
if(num2 > x)
{
num2 = x;
}
}
}
return num2;
}
private static void outputResults(int start, int steps, int max)
{
System.out.println();
System.out.println("Generated series for starting value " + start);
System.out.println("Series converged in " + steps + " steps");
System.out.println("Largest value generated is " + max);
}
}
Explanation / Answer
please rate - thanks
you had it in the wrong place
import java.util.*;
public class main
{
public static void main(String[] args)
{
Scanner in = new Scanner (System.in);
int start;
int steps;
int max;
System.out.print("Would you like to compute a hailstone series?");
String answer = in.nextLine();
if(answer.charAt(0)!= 'Y' || answer.charAt(0) != 'y')
{
while((answer.charAt(0) == 'y' || answer.charAt(0) == 'Y'))
{
start = getStartingValue(in);
steps = computeLength(start);
max = computeMax(start);
outputResults(start, steps, max);
in.nextLine();
System.out.println();
System.out.println("Would you like to compute another series?: ");
answer = in.nextLine();
}
}
System.out.println("Goodbye.");
}
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;
}
private static int computeLength(int x)
{
int length = 1;
if (x == 1)
{
length= length + 1;
}
else
{
while(x > 1)
{
if(x%2 == 0)
{
x = x / 2;
length= length + 1;
}
else
{
x = (3 * x) + 1;
length= length + 1;
}
}
}
return length;
}
//(this is the part I cant get!!!)
private static int computeMax(int x)
{
int num1 = 1;
int num2= x;
if(x == 1)
{
num1 = x;
}
else
{
while(x > 1)
{
if(x>num2)
num2=x;
if(x%2 == 0)
{
x = x/2;
num1= num1 + 1;
}
else
{
x = (3 * x) + 1;
num1= num1 + 1;
}
}
}
return num2;
}
private static void outputResults(int start, int steps, int max)
{
System.out.println();
System.out.println("Generated series for starting value " + start);
System.out.println("Series converged in " + steps + " steps");
System.out.println("Largest value generated is " + max);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.