Ave_L
October 24th, 2010, 07:58 PM
Short Story: I am a student who was assigned a simple problem ( using what we've learned thus far) to write some Java code for a program to receive a user inputted number and then output how many cycles of square roots it takes to get the number below 1.01. That part was simple enough and the program I wrote performs the task.
Here is my problem: When I was checking for runtime errors I encountered a very strange behavior of the program. During certain situations it will display a message twice back to back. Myself and my professor can not understand why it does this as the code seems very straight forward. Hopefully some one here can run the code and/or explain why the code runs this way.
The error happens when you first enter a character like 'A' , then you enter the digit 1, then enter a character 'A' again . This will produce the System.out.print msg twice. Praying that some one will understand why it does this. Here is the code I wrote:
// Chap 4 handout sheet Question4 Square roots
import java.util.*;
import static java.lang.Math.*;
public class Q4squares
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
double num1, num2 = 0;
boolean result;
char letter;
System.out.println("\nWelcome to the square root cycle Calculator!");
System.out.println("This program will prompt you to enter a number and then \ncalculate how many"
+ " cycles of square roots it takes until \nthe number is less then 1.01!"
+ "\nSeems weird right? Lets begin:");
do
{
do
{
/* This section aquires a user inputed number; tests to see if its
a double and tests to see if its less then 10*/
System.out.print("\nPlease enter a number (must be higher then 10): ");
result = scan.hasNextDouble();
while (!(result))
{
scan.nextLine();
System.out.print("Input must be a number. Please re-enter: ");
result = scan.hasNextDouble();
}
num1 = scan.nextDouble();
} while ( num1 < 11);
num2 = num1; // retain number for final display
/* This section performs the square calculation and output
of how many cycles it takes to get below 1.01 */
int count = 0;
for ( ;num1 > 1.01; count++)
{
num1 = Math.sqrt(num1);
}
System.out.println("The number " + num2 + " took " + count + " square root "
+ "cycles to be less then 1.01");
/* This section is to give the user a choice if they would like to enter another
number to calculate and tests to make sure the char thats inputed is a Y or N */
System.out.print("\nWould you like to enter another number?: Y(Yes) or N(No): ");
letter = scan.next().charAt(0);
while (!(letter == 'y' || letter =='Y' || letter == 'n' || letter == 'N'))
{
System.out.print("Please re-enter: Y(Yes) or N(No) ");
letter = scan.next().charAt(0);
}
} while (letter == 'Y' || letter == 'y');
System.out.println("Ok. Goodbye!");
}}
Here is my problem: When I was checking for runtime errors I encountered a very strange behavior of the program. During certain situations it will display a message twice back to back. Myself and my professor can not understand why it does this as the code seems very straight forward. Hopefully some one here can run the code and/or explain why the code runs this way.
The error happens when you first enter a character like 'A' , then you enter the digit 1, then enter a character 'A' again . This will produce the System.out.print msg twice. Praying that some one will understand why it does this. Here is the code I wrote:
// Chap 4 handout sheet Question4 Square roots
import java.util.*;
import static java.lang.Math.*;
public class Q4squares
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
double num1, num2 = 0;
boolean result;
char letter;
System.out.println("\nWelcome to the square root cycle Calculator!");
System.out.println("This program will prompt you to enter a number and then \ncalculate how many"
+ " cycles of square roots it takes until \nthe number is less then 1.01!"
+ "\nSeems weird right? Lets begin:");
do
{
do
{
/* This section aquires a user inputed number; tests to see if its
a double and tests to see if its less then 10*/
System.out.print("\nPlease enter a number (must be higher then 10): ");
result = scan.hasNextDouble();
while (!(result))
{
scan.nextLine();
System.out.print("Input must be a number. Please re-enter: ");
result = scan.hasNextDouble();
}
num1 = scan.nextDouble();
} while ( num1 < 11);
num2 = num1; // retain number for final display
/* This section performs the square calculation and output
of how many cycles it takes to get below 1.01 */
int count = 0;
for ( ;num1 > 1.01; count++)
{
num1 = Math.sqrt(num1);
}
System.out.println("The number " + num2 + " took " + count + " square root "
+ "cycles to be less then 1.01");
/* This section is to give the user a choice if they would like to enter another
number to calculate and tests to make sure the char thats inputed is a Y or N */
System.out.print("\nWould you like to enter another number?: Y(Yes) or N(No): ");
letter = scan.next().charAt(0);
while (!(letter == 'y' || letter =='Y' || letter == 'n' || letter == 'N'))
{
System.out.print("Please re-enter: Y(Yes) or N(No) ");
letter = scan.next().charAt(0);
}
} while (letter == 'Y' || letter == 'y');
System.out.println("Ok. Goodbye!");
}}