public class Solution { public static void main(String[] args) throws Exception { readText(); rectangle(); } public static void readText() throws IOException { BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); int m = Integer.parseInt(read.readLine()); int n = Integer.parseInt(read.readLine()); } public static void rectangle(){ for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { System.out.print(8); } System.out.println(); } } } 

rectangle () method does not see the values ​​of the variables "m" and "n", which are entered in the readText () method, how to fix it?

  • It's time to read about the scope of variables in Java - pavlofff
  • I understand that "m" and "n" are local variables, that they are visible within the readText () method. I ask how to make working code based on this? - Alexander
  • Obviously, putting them into the class field - pavlofff

1 answer 1

 public class Solution { int static n; int static m; public static void main(String[] args) throws Exception { readText(); rectangle(); } public static void readText() throws IOException { BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); m = Integer.parseInt(read.readLine()); n = Integer.parseInt(read.readLine()); } public static void rectangle(){ for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { System.out.print(8); } System.out.println(); } } } 

But it would be best to create an instance of the class of the Solution class and call methods on it so that the static is less

  • Can you just write this part of the code? Please "But it would be best to create an instance of the class Solution and call methods on it" How to write this? - Alexander
  • 3
    Do not ask such questions if you learn JAva. You have been given more clues than you need, even to a beginner. Understand. - iamthevoid