Good day. Such a question - inside the while loop , the value of the variable n is determined, but outside the loop limits it is not available. How can I get him out of there? Is it possible to somehow modify the scope for a variable?

import java.util.Scanner; public class Main { public static void main(String[] args) { boolean j = false; int n; int[] arr1 = new int[n]; while (!j){ System.out.println("Введите четное положительное число"); Scanner inc = new Scanner(System.in); n = inc.nextInt(); if (n > 0 && n%2==0) j=true; } for (int i=0; i <=n; i++) {arr1[i] = (int) (Math.random() * 11 - 5);} }} 
  • 3
    за пределам цикла оно не доступно - how did you get this? - post_zeew
  • 2
    it is available outside the loop. I wonder why write new int [n] when n is undefined? - Isaev

3 answers 3

 public static void main(String[] args) { boolean j = false; int n = 0; while (!j){ System.out.println("Введите четное положительное число"); Scanner inc = new Scanner(System.in); n = inc.nextInt(); if (n > 0 && n%2==0) j=true; } int[] arr1 = new int[n]; for (int i=0; i < n; i++) {arr1[i] = (int) (Math.random() * 11 - 5);} } 

This program should work. The error was that with the string int[] arr1 = new int[n]; used the variable n , which was defined on the previous line, but not initialized. It is initialized only in a while loop in the string n = inc.nextInt(); . It is impossible to use what is not

  • "Should work", but does not work: an error in the string int [] arr1 = new int [n] and for (int i = 0; i <= n; i ++) because the variable n is not initialized, and the variable n = inc.nextInt () is available only during a while loop - Sergey
  • “It’s impossible to use something that’s not” Of course, it’s possible, without initialization, it’s even likely to be 0, or some kind of garbage, which is even worse. But if it is 0, then we create a null array, and then in a loop you go around it to n elements ... of course it will not work - Isaev
  • @Sergey yes, didn’t pay attention to the fact that in the for loop you are iterating from i = 0 to i = n. An array is created of size n, its indices are from zero to n-1 inclusive, and the cycle refers to the elements of the array from 0 to n inclusive. Correct for (int i=0; i <=n; i++) to for (int i=0; i <n; i++) and change int n; int n = 0; (here the compiler does not like that the variable from his point of view is not always initialized, he thinks that there is a chance not to get into the while loop) - Nikita Gordeev
  • 2
    @Isaev, Without initialization, the example will not compile. - post_zeew
  • @Isaev yes, at the end of the previous comment already wrote this - Nikita Gordeev

Apparently, the length of the array should be entered from the keyboard and be a positive even number, then the array should be created inside the loop and it might look like this:

  public static void main(String[] args) { boolean j = false; int n; while (!j) { System.out.println("Введите четное положительное число"); Scanner inc = new Scanner(System.in); n = inc.nextInt(); if (n > 0 && n % 2 == 0) { j = true; int[] arr1 = new int[n]; for (int i = 0; i < n; i++) { arr1[i] = (int) (Math.random() * 11 - 5); } } else System.out.println("Неверное число"); } } 

    Make the variable static, no?

    public class Main {

     public static int n; public static void main(String[] args) { boolean j = false; while (!j){ n=0; System.out.println("Введите четное положительное число"); Scanner inc = new Scanner(System.in); n = inc.nextInt(); if (n > 0 && n%2==0) j=true; } int[] arr1 = new int[n]; for (int i=0; i <n; i++) {arr1[i] = (int) (Math.random() * 11 - 5);} } 

    }