【问题标题】:Calling a method, but won't return the values. Java调用方法,但不会返回值。爪哇
【发布时间】:2014-11-27 02:59:09
【问题描述】:

所以我有一个任务,我必须开一个银行账户。长话短说,我有一个类在方法中包含您的操作(存款、取款等),还有一个驱动程序可以填补空白(即客户名称、余额等),它还会调用其他类的方法。

现在我的问题。例如,当我存款时,我的变量很好地发送到该方法并正确地进行数学运算并具有正确的余额,但返回余额的新值是一个问题。它仍然与之前在驱动程序中的值相同。

这是我司机的存款代码

 String deposit1 = JOptionPane.showInputDialog ("How much would you like to deposit?");
 int deposit = Integer.parseInt (deposit1);
 myBank.getDeposit(deposit, balance);

这是我的存款方式代码

 public void getDeposit(int deposit, int balance)
{
    if (deposit<0)
    {
        System.out.println ("Invalid Number");
        deposit=0;
    }
    balance =balance + deposit;
}

任何帮助将不胜感激,似乎找不到任何解决方案,我已经被困了一段时间

谢谢

P.S 这是我第一次上这些论坛,如果我做错了什么,请见谅

P.P.S 我的java知识没那么高,还是个初学者

编辑: 我们已经学习了实例变量以及一些关于 return 语句的知识,我们还没有学到任何关于 passby 的知识。通过返回,我的意思是给驱动程序正确的值,如果这让你失望了,对不起

Edit2:非常感谢user1710742,终于明白了。我现在知道了。有道理

【问题讨论】:

  • 您需要向您的讲师询问有关“返回值”的问题。 (可能是“实例字段”,也可能是“按值传递”,但很难猜出你在这里真正想要做什么。)
  • 该方法被命名为get-Something 但返回void 表明有问题。

标签: java variables object methods


【解决方案1】:

问题是您试图更改变量的副本,而不是您为方法提供的确切副本。当您退出该方法时,副本被销毁,您只剩下未更改的原始值。

除非您使用return 语句告知该方法,否则该方法无法返回值。

public int getDeposit(int deposit, int balance)
{
    if (deposit<0)
    {
        System.out.println ("Invalid Number");
        deposit=0;
    }
    return balance + deposit;
}

致电: int total = myBank.getDeposit(deposit, balance);

【讨论】:

  • 虽然是一个答案,但我认为 OP 实际上最好在这里使用实例变量。最好问问老师我觉得作业是什么。
  • OP 确实说“返回”是问题所在。但是,OP 可能错误地使用了该术语。
【解决方案2】:

您可以使用 int 数据类型添加一个新的变量调用 dep

int dep = myBank.getDeposit(deposit, balance);

之后将您的方法类型从 void 更改为 int 并添加您想要的变量的 return 语句:

public int getDeposit(int deposit, int balance)
{
  if (deposit<0)
  {
    System.out.println ("Invalid Number");
    deposit=0;
  }
  balance =balance + deposit;
  return balance;
}

【讨论】:

  • 至少添加一些类的解释。
【解决方案3】:

从语言的基础开始。然而,请记住,了解问题是获得正确解决方案的唯一真正途径。不要让语言妨碍逻辑。您将足够快地掌握语法。你有一个好的开始。很容易陷入只寻找 A 解决方案,而不是找到 THE 解决方案。解决问题的方法比有答案更重要。试试下面的链接,了解有关返回值的基础知识。

http://www.homeandlearn.co.uk/java/java_methods.html

【讨论】:

    【解决方案4】:
    1. 你不应该将你的方法命名为getDeposit(),因为返回类型是void。相反,只需将其命名为 deposit(),因为您实际上执行了一项操作。

    2. 在您的 Bank 类中使用名为 balance 的私有实例变量。

    3. 为您的balance 字段使用吸气剂。

    这个 sn-p 应该可以工作:

    public class BankAccount {
    
        private int balance;
    
        public static void main(String[] args) {
            BankAccount account = new BankAccount();
    
            String deposit = JOptionPane.showInputDialog ("How much would you like to deposit?");
            account.deposit(Integer.parseInt (deposit));
    
            deposit = JOptionPane.showInputDialog ("How much would you like to deposit?");
            account.deposit(Integer.parseInt (deposit));
    
            System.out.println(account.getBalance());
    
        }
    
         public void deposit(int deposit){
             if (deposit < 0){
                 System.out.println ("Invalid Number");
             }else{
                 balance += deposit;
             }   
         }
    
         public int getBalance(){
             return balance;
         }
    }
    

    【讨论】:

      【解决方案5】:

      我不确定你是否真的在做你想做的事.. 对我来说,您似乎想创建一个名为 Bank 的类并在那里有一个私有变量,称为 ballance。然后是操作该值的方法..

      public static class Bank{
          private int balance = 0;
      
          // this method won't return any value. it will only change
          // the banks internal state, therefor we write "void"!
          public void deposit(int deposit){
              if(deposit<0){
                  // using System.err instead of System.out will be handled differently
                  // by java. It is meant to be used for error messages. some consoles make
                  // these texts pop out more (like displyaing it in read)
                  System.err.println ("Invalid Number");
              }else{
                  // balance += deposit is a short way of writing balance = balance + deposit;
                  balance += deposit;
              }
          }
      
          // this method will return an int value to who ever called it.
          // you can tell, because it says int after public
          public int getBalance(){
              return balance;
          }
      }
      

      然后是处理银行的主要方法:

      Bank bank = new Bank();
      bank.deposit(5);
      bank.deposit(8);
      
      int balance = bank.getBalance(); // will be 13
      
      System.out.println("balance is "+balance);
      

      有关 System.err 的更多信息,请参阅oracle docs about System

      希望我能帮上忙

      【讨论】:

      • 你如何在不打印出方法内部内容的情况下获得方法的返回值?因为我试图在我的方法中返回一个对象值,但是当我将方法分配给一个变量时,比如Object x = call_method();,发生的事情是再次调用整个方法。有什么办法可以避免吗?
      猜你喜欢
      • 2015-05-23
      • 2020-08-08
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多