【问题标题】:Use one method in another method在另一种方法中使用一种方法
【发布时间】:2016-12-09 05:26:19
【问题描述】:

所以这可能以前被问过,但我无法真正理解我在网上阅读的所有信息。

public static void balanceSheet(){
  //Put in the Company u want to get the data from
  Scanner scan= new Scanner(System.in);
  System.out.println("Enter your Symbol: ");
  String symbol= scan.nextLine();

  //Diffrent Websites to visit and get the Data
  String url = ("http://www.morningstar.com/stocks/XNAS/"+symbol+"/quote.html");
  driver.switchTo().frame(driver.findElement(By.xpath("//*[(@id = 'quote_quicktake')]//iframe")));
  String currPrice = driver.findElement(By.id("last-price-value")).getText();
  driver.switchTo().defaultContent();

  System.out.println("Current Share Pirce: "+currPrice+"$");
}

如何在 calCulator 方法中获取 String currPrice 的值,以便我可以用其他数字计算它?

public void calCulator() {
  Scanner calCu = new Scanner(System.in);
  System.out.println("Do you want to Calculate the Value?: ");
  String calculator = calCu.next();

  if(calculator.equals("Yes")) {
    System.out.println("the Calculation is: ");
  }

  else {
    System.out.println("ok");
  }
}

【问题讨论】:

  • 将该值作为参数传递给calCulator() 方法??
  • 或将currPrice 存储为字段
  • 我可以举个例子吗?这是我的第一个程序,所以我对 java 很陌生
  • "所以这可能以前被问过" -- 是的,在过去的两周里大约有十几次...... StackOverflow 不是一个教程网站,请访问help center 并阅读 How to Ask
  • @BrudgeKisekka - 添加调用balanceSheetcalCulator 的完整代码。通过currPrice 无法真正理解您在这里要做什么

标签: java variables methods calculator


【解决方案1】:

好吧,经过 15 个小时的纯看教程,我得到了我想要的答案,这真的很容易,只是我的头脑很慢,但我所做的是

public class Webscraper {

    String currentPrice; <<<<<< ADDED THAT STRING TO MY CLASS

    public void balanceSheet(){
      //Put in the Company u want to get the data from
      Scanner scan= new Scanner(System.in);
      System.out.println("Enter your Symbol: ");
      String symbol= scan.nextLine();

      //Diffrent Websites to visit and get the Data
      String url = ("http://www.morningstar.com/stocks/XNAS/"+symbol+"/quote.html");
      driver.switchTo().frame(driver.findElement(By.xpath("//*[(@id = 'quote_quicktake')]//iframe")));
      String currPrice = driver.findElement(By.id("last-price-value")).getText();
      driver.switchTo().defaultContent();

      System.out.println("Current Share Pirce: "+currPrice+"$");
      currentPirce = currPrice; <<<<<< ADDED THAT SO THAT THE CLASS STRING WOULD GET ITS VALUE
    }

    public void calCulator() {
      Scanner calCu = new Scanner(System.in);
      System.out.println("Do you want to Calculate the Value?: ");
      String calculator = calCu.next();

      if(calculator.equals("Yes")) {
        System.out.println("the Calculation is: "currentPrice); <<<<<< NOW I CAN GET THE VALUE
      }

      else {
        System.out.println("ok");
      }
    }

无论如何,谢谢你们的帮助,你们为我指明了正确的方向,希望这能帮助其他有同样问题的人

【讨论】:

    【解决方案2】:

    currPrice作为类的成员变量,然后你可以在类的其他方法中使用它,如下所示。

    public class Culator {
        // Take "currPrice" as member variable of class
        public static String currPrice = "";
    
    
        public static void balanceSheet(){
    
            //Put in the Company u want to get the data from
            Scanner scan= new Scanner(System.in);
            System.out.println("Enter your Symbol: ");
            String symbol= scan.nextLine();
    
            //Diffrent Websites to visit and get the Data
            String url = ("http://www.morningstar.com/stocks/XNAS/"+symbol+"/quote.html");
    
            driver.switchTo().frame(driver.findElement(By.xpath("//*[(@id = 'quote_quicktake')]//iframe")));
            currPrice = driver.findElement(By.id("last-price-value")).getText();
    
            driver.switchTo().defaultContent();
    
            System.out.println("Current Share Pirce: "+currPrice+"$");
        }
    
    
         public void calCulator()
         {
             Scanner calCu = new Scanner(System.in);
             System.out.println("Do you want to Calculate the Value?: ");
             String calculator = calCu.next();
    
             if(calculator.equals("Yes")){
                 System.out.println("the Calculation is: ");
             } else {
                 System.out.println("ok");
             }
             // Now You can use "currPrice" in this method
         }
    
    }    
    

    【讨论】:

    • 但是当我尝试时,我总是得到返回 Null 的结果,而不是在 public static void balanceSheet() 中声明的 currPrice
    • balanceSheet 是一个静态方法,你不能在里面访问实例变量currPrice
    • 更新您的代码作为我的更新答案。您需要先调用 balanceSheet()。因此,首先计算currPrice,然后您可以在其他方法中使用该变量值。
    • 我确实更新了它,就像你说的结果是计算结果是:空结果
    猜你喜欢
    • 1970-01-01
    • 2017-10-30
    • 2013-02-10
    • 2020-08-02
    • 1970-01-01
    • 2011-02-21
    • 2012-03-10
    • 1970-01-01
    • 2011-07-05
    相关资源
    最近更新 更多