-2

I want to make a program that gets a number input and finds the closest perfect square to determine the square length. Thus, the closest perfect square has to be less than the input. For example, if the input is 8, the largest side length of the square is 2. The problem is that the program will ask me to input a number but does not output anything after that. It also says I have a duplicate local variable a1.

import java.util.Scanner;  
public class J1 {

    public static void main(String[] args) {

        int a;
        int a1;

        Scanner number = new Scanner(System.in);
        System.out.println("Number: ");
        a = number.nextInt(); 
        int n = (int) Math.sqrt(a1); 

        /* remove int from a1 */
        for ( int a1=a; a1<a; a1--) {

            if (Math.floor(a1)==0)
            System.out.println("The largest square has side length" + a1); 

        }
    }
}
ManLaw
  • 115
  • 1
  • 10
  • You've told us what you want to do, but not what problem you're having with your code attempt. You mention line 13 in the question title, but don't label which line this is in your code. Please edit your question to make it clearer what the problem is so that you have a better chance of people being able to help you. – Bobulous Jul 13 '19 at 18:56
  • 3
    You should look **very *carefully*** at the loop condition in `for (int a1=a; a1 – Elliott Frisch Jul 13 '19 at 18:56
  • I have clarified my question –  Jul 13 '19 at 19:01
  • Can you please clarify what you mean by that @ElliottFrisch –  Jul 13 '19 at 19:04
  • You assign something to the variable n, but never use it. Was that what you meant to do? – Mark Plotnick Jul 13 '19 at 19:09
  • I wanted a to be the answer to the square root of the input @MarkPlotnick –  Jul 13 '19 at 19:10

3 Answers3

0

As mentioned by Elliott in the comment, the loop is never entered. Reason: a1 is assigned the value of a and then you are checking if a1. Which is not true and hence the for-loop is terminated even before its 1st iteration. For what you are trying to achive, use:

for (a1=a; a1 >= 0; a1--) {
    if (Math.floor(a1)==0)
    System.out.println("The largest square has side length" + a1); 
}
  • How would I solve this problem, I made a equal to a1 and start decreasing by to determine the closest perfect square to the input which is represented by a. –  Jul 13 '19 at 19:08
  • for(int a1 = a; a1 > 0; a1--) – Rahul Singh Jul 13 '19 at 19:10
0

If I understand your problem correctly, I think you want to make a1 decrease in value until it reaches 0. Maybe you need

for ( a1=a; a1 >= 0; a1--) {
        if (Math.floor(a1)==0)
        System.out.println("The largest square has side length" + a1); 
 }

UPDATE EDIT: Removed declaration int from a1 inside the loop because it was likely the source of the other bug ( double declaration of a1).

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
ManLaw
  • 115
  • 1
  • 10
0

There are various problems with the code that others have pointed out and I'm sure you'll take them on board. To solve your problem though, I'd do something like this. The while loop lets you keep trying new values until you enter -1.

public static void main(String[] args) {
    System.out.println("Enter value:");
    Scanner sc = new Scanner(System.in);
    while (sc.hasNext()) {
        boolean hasPerfectSquare = false;
        int input = sc.nextInt();
        if (input == -1) {
            break;
        }
        for (int i = input; i > 0; i--) {
            if (Math.floor(Math.sqrt(i)) == Math.sqrt(i)) {
                hasPerfectSquare = true;
                System.out.println((int) Math.sqrt(i));
                break;
            }
        }
        if (!hasPerfectSquare) {
            System.out.println("No perfect square");
        }
        System.out.println("Enter value:");
    }
}
Robert Bain
  • 9,113
  • 8
  • 44
  • 63