Two while loop with getter and setter method | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Two while loop with getter and setter method

I want to set a random number and print continuously. `fig` class is for set values and `sestk` class is for get values. I used while loop on both `fig` class and `sestk` class . My codes fig.java import java.util.Random; public class fig { public static void main(String[] args) { gsc fi = new gsc(); Random random = new Random(); while(true) { int a = random.nextInt(50); int b= random.nextInt(50); System.out.println("("+a+" , "+b+")"); fi.setdb( a,b); sestk dp = new sestk(); dp.a(); } } } gsc.java public class gsc { private static int a; private static int b; public void setdb(int c, int d){ this.a = c; this.b = d; } public int getdba(){ return a; } public int getdbb(){ return b; } } sestk.java public class mousestk { gsc a= new gsc(); public void a() { while (true){ System.out.println(a.getdba()+" x , y "+a.getdbb()); } } } OUTPUT (26 , 44) 26 x , y 44 26 x , y 44 26 x , y 44 26 x , y 44 26 x , y 44 The output printing ` 26 x , y 44` continuously . only one time the random value was generated and set in `gsc` class. I want to set random value contineously and get different outputs. `fig` class is for set values and `sestk` class is for get values. I used while loop on both `fig` class and `sestk` class . but , only one time `fig` class prints the random number. `sestk` class prints the get value from `gsc` class continuously . How Can I do this ? Is there any other methods to do this ? I asked this question , because I want to apply this concept in my graph project.

9th May 2021, 10:38 AM
🐕Pradeep Simba 🐶
🐕Pradeep Simba 🐶 - avatar
3 Answers
0
Its not the answer or a suggestion ajanant.
10th May 2021, 6:19 AM
🐕Pradeep Simba 🐶
🐕Pradeep Simba 🐶 - avatar
- 2
🐕Pradeep Simba 🐶 You have done lots of mistakes in your code. 1 - You didn't follow Java rules, class name should start with Capital letter 2 - method name should be understandable 3 - in class variable should not be static with private keyword. 4 - you didn't make proper setter getter methods 5 - you have used while (true) and there is no break statement to break infinite loop so your program may be crash.
9th May 2021, 2:31 PM
A͢J
A͢J - avatar
- 2
🐕Pradeep Simba 🐶 Problem is that you are using one while loop inside another loop so it is looking like this: while (true) { int x = 5; int y = 5; while (true) { sop(x + " " + y); //Since here is no break so every time same number will be print } } So you should not use another while loop just print values: https://code.sololearn.com/cURlI4pkT9w6/?ref=app
9th May 2021, 2:54 PM
A͢J
A͢J - avatar