My project is to:
Have user enter a "lower bound" and a "higher bound" for a range of integers that I'm making 0-10.
The code is supposed to automatically calculate and display the squares and the square roots for all numbers in the range.
If the lower bound is greater than the upper bound, the statement "No work is to be performed" should be displayed.
---------------------------------------------
I can make the input work and get the first part to display, but I can't figure out the LOOP part and I can't figure out the if/else to get it to work.
If anyone can help, I would appreciate it. Here is what I have:
------------------------------------------
import java.util.*;
public class P4
{
public static void main(String[] args)
{
int x;
int lowerBound, upperBound;
double square, squareRoot;
Scanner kb = new Scanner(System.in);
for (x = 1; x <= 10; x++);
{
System.out.print("Please enter a lower bound: ");
lowerBound = kb.nextInt();
System.out.print("Please enter an upper bound: ");
upperBound = kb.nextInt();
square = Math.pow(lowerBound,2);
squareRoot = Math.sqrt(lowerBound);
if (lowerBound <= upperBound)
System.out.printf("For the value of " + lowerBound + ", the square is " + "%.2f", +square);
System.out.printf(" and the square root is " + "%.2f%n", +squareRoot);
if (lowerBound > upperBound)
System.out.println("The lower bound is greater than the upper bound.");
System.out.println("No work will be performed.");
}
}}
Comments
If I understand well the requirements, the user types 1 lower bound and 1 upper bound, and then the code is calculating and displaying the squares and the square roots for all numbers in the range(all numbers between lower_bound and upper_bound). But lower bound must be >=1 and upper bound must be <=10.
`for (x = 1; x <= 10; x++);{
System.out.print("Please enter a lower bound: ");
lowerBound = kb.nextInt();
System.out.print("Please enter an upper bound: ");
upperBound = kb.nextInt();
if(lowerBound <= upperBound){...}
if(lowerBound > upperBound){...}
}`
You ask here the user to type the bounds 9 times.
I will move the the lines where you ask the user to provide input and read it before the LOOP so you will ask the user to type them only once.
I think it's easier and more logical to test from the start if the input is correct.
`
import java.util.*;
public class P4{
Scanner kb = new Scanner(System.in);
System.out.print("Please enter a lower bound: ");
lowerBound = kb.nextInt();
System.out.print("Please enter an upper bound: ");
upperBound = kb.nextInt();
//checking if the input is correct
if (lowerBound > upperBound){System.out.println("The lower bound is greater than the upper bound.");
wrong++;
}`
//checking if the input is correct
//checking if the input is correct
//if the input is correct
if(wrong == 0){
} `
}
}`