简体   繁体   English

如何在另一种方法中调用一个方法的整数?

[英]How do i call an integer from one method in another method?

I'm fairly new to programming with Java... but this has really got me stumped, I've searched for a while and I could not find a clear answer to what I was looking for... but let's say I have two methods 我对使用Java进行编程相当新...但是这真让我感到难过,我已经搜索了一段时间而且找不到我想要的答案的明确答案...但是我要说我有两个方法

public static void program1 (String[] args) {
    Integer intMoney;
    intMoney = 500;
}

public static void program2 (String[] args) {
    String strYes;
    strYes = JOptionPane.showInputDialog("type yes to subtract 100");
    if((strYes.equals("Yes") || (strYes.equals("yes")))) {
    /*((This is where I call the intMoney from program1) */ - 100;
    }else{
        JOptionPane.showMessageDialog(null, "Thats not yes!");
    }
}

And here is where I get really stuck.. say I have another method like program1, But how can I call on the intMoney value in program1 in another method? 这里是我真正卡住的地方..说我有另一种方法,如program1,但是如何在另一种方法中调用program1中的intMoney值?

Let's say I have a program and I want intMoney to be declared in a separate method so that when the Method program2 gets repeated the intMoney value wont change and it will be the same when the method is called on again. 假设我有一个程序,我希望intMoney在一个单独的方法中声明,这样当方法程序2重复时, intMoney值不会改变,并且当再次调用该方法时它将是相同的。

First of all your program is totally out of rules and regulation having so many mistakes: 首先,你的计划完全出于规则和规则,有很多错误:

  1. intMoney has function scope.So, it cannot be called from outside the function program1() . intMoney具有函数作用域。因此,它不能从函数program1()外部调用。 You have to return value from this function to use in another function. 您必须从此函数返回值才能在另一个函数中使用。
  2. In your if you are checking 2 condition separated by || 如果你正在检查由||分隔的2个条件 but both conditions are same.Use one please. 但两个条件都相同。请使用一个。

     public static int program1 () { Integer intMoney; intMoney = 500; return intMoney; } public static void program2 () { String strYes; strYes = JOptionPane.showInputDialog("type yes to subtract 100"); if((strYes.equals("Yes") || (strYes.equals("yes")))); { program1() - 100 }else{ JOptionPane.showMessageDialog(null, "Thats not yes!"); } } 

You can' access access the variable in program1 since it't scoped to the method. 你可以“访问program1中的变量,因为它没有作用于方法。 You should do something like this: 你应该做这样的事情:

public class Foo {
    public static Integer intMoney;

    public static void program1(String[] args) {
        intMoney = 500;
    }

    public static void program2(String[] args) {
        String strYes;
        strYes = JOptionPane.showInputDialog("type yes to subtract 100");
        if ((strYes.equals("Yes") || (strYes.equals("yes"))))
        {
            Integer i = intMoney;
            Integer x = i - 100;

        }else{
            JOptionPane.showMessageDialog(null, "Thats not yes!");

        }
    }
}

Of course now you need to call program1 first in order for the variable to be set. 当然,现在需要先调用program1才能设置变量。 You could also just initiate it like this public static final Integer intMoney = 500 ; 你也可以像这个public static final Integer intMoney = 500一样启动它;

Also, what's with the String[] args parameters if you are not using them? 另外,如果您不使用String [] args参数,那么它们是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM