在类/方法之间交换数值

huangapple go评论118阅读模式
英文:

Exchange values between classes/Methods

问题

  1. public class MainM {
  2. public static void main(String[] args) {
  3. Loop Q = new Loop();
  4. Q.doOperation();
  5. }
  6. }
  7. //------------------------------------------------------
  8. public class Loop {
  9. public double b;
  10. Sum R = new Sum(); // Java 出现问题的地方:在 Sum.<init>(Sum.java:3)
  11. public void doOperation() {
  12. for (int i = 0; i < 10; i++) {
  13. b = b + 2;
  14. if (b <= 10) {
  15. R.operationFor1();
  16. }
  17. }
  18. }
  19. }
  20. //--------------------------------------------------
  21. public class Sum {
  22. Loop Q = new Loop();
  23. public void operationFor1() {
  24. System.out.println("b " + Q.b);
  25. }
  26. }

请注意,我只对代码进行了翻译,不包括代码中的问题或错误。如果您需要进一步的帮助,可以详细描述问题,我将尽力回答。

英文:

i recently learned the use of public, private and double in my different classes. But for some reason i cant understand why this is not working. My intention was to use three different classes as an exercise: I want Do() to make numbers from 0 to 20 and show only the numbers 0 till 10 on my console using the method for1() in a different class. Can someone please fix this issue? I dont need a shorter code or a code in just 1 class since i need it to educate myself using many classes. I would thank anyone if you could fix this issue using this kind of setup. Thanks in advance.

  1. public class MainM {
  2. public static void main(String[] args) {
  3. loop Q = new loop();
  4. Q.Do();
  5. }
  6. }
  7. //------------------------------------------------------
  8. public class loop {
  9. public double b;
  10. Sum R = new Sum(); // Java shows the problem is here : at Sum.&lt;init&gt;(Sum.java:3)
  11. public void Do() {
  12. for (int i = 0; i &lt; 10; i++) {
  13. b = b + 2;
  14. if (b &lt;= 10) {
  15. R.for1();
  16. }
  17. }
  18. }
  19. }
  20. //--------------------------------------------------
  21. public class Sum {
  22. loop Q = new loop();
  23. public void for1() {
  24. System.out.println(&quot;b &quot; + Q.b);
  25. }
  26. }
  27. </details>
  28. # 答案1
  29. **得分**: 0
  30. 你可以只保留 `print` 语句的 `Sum` 类,并且 `for1()` 方法应该有一个参数。以下是我的建议:
  31. ```java
  32. public class Sum {
  33. public void for1(double b) {
  34. System.out.println("b " + b);
  35. }
  36. }

而你的循环类将会是:

  1. public class Loop {
  2. public double b;
  3. Sum R = new Sum();
  4. public void Do() {
  5. for (int i = 0; i < 10; i++) {
  6. b = b + 2;
  7. if (b <= 10) {
  8. R.for1(b);
  9. }
  10. }
  11. }
  12. }
英文:

You can have your Sum class only with the print statement and the method for1() should have one parameter. Bellow is my suggestion

  1. public class Sum {
  2. public void for1(double b) {
  3. System.out.println(&quot;b &quot; + b);
  4. }
  5. }

And your loop class will be

  1. public class loop {
  2. public double b;
  3. Sum R = new Sum();
  4. public void Do() {
  5. for (int i = 0; i &lt; 10; i++) {
  6. b = b + 2;
  7. if (b &lt;= 10) {
  8. R.for1(b);
  9. }
  10. }
  11. }
  12. }

huangapple
  • 本文由 发表于 2020年10月18日 23:30:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64415064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定