【问题标题】:How to return 1 String in case there are multiple IF statements如果有多个 IF 语句,如何返回 1 个字符串
【发布时间】:2017-02-28 21:56:47
【问题描述】:

如果我有一些 IF 语句,我如何返回 1 个值?

例如,当我选择 Shopify 时,它会调用一个新方法,该方法允许我在控制台中输入一些需要的凭据,然后将所有输入的数据附加到 1 个字符串中,并将该字符串(测试)返回给 Main 类。 如果我选择 Bigcommerce,它会调用几乎相同的方法,并将 Biggcomerce 所需的所有凭据也附加到 1 String(test2) 中。但在这种情况下, public String credentailsCollector() 应该返回 test2。

我该怎么做?谢谢。

public class CartCredentialsCollector {
//it should return something like this: 
//store_url=https://test.myshopify.com&apiKey=myapikey&apiPassword=myapipassword

public String credentailsCollector() throws IOException {

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Do you want to connect a new shopping cart ('yes'/'no')");
    String newConnection = bufferedReader.readLine();
    if (newConnection.equals("no")) {
        bufferedReader.close();
    } else {
        System.out.println("Specify your shopping cart. It works with Shopify and Bigcommerce only");
        String newShoppingCart = bufferedReader.readLine();
        if (newShoppingCart.equals("Shopify")) {
            ShopifyCredentialsHandler sch = new ShopifyCredentialsHandler();
            String test = sch.shopifyCredentialsHandler();
        }
        else if (newShoppingCart.equals("Bigcommerce")) {
        BigcommerceCredentialsHandler bch = new BigcommerceCredentialsHandler();
        String test2 = bch.bigcommerceCredentialsHandler()
        }

        else {
            System.out.println("This method works with Shopify and Bigcommerce. Try to connect a Shopify or Bigcommerce store.");
            bufferedReader.close();
        }
    }
    // Here I need to return test (in case I choose "Shopify") or test2 (in case I choose "Bigcommerce"). How can I do that?
    }
}

【问题讨论】:

  • 您的方法中可以有多个返回语句。
  • 一些替代方案:(1)在您调用所选方法的位置返回字符串; (2) 在方法的最外层声明 String test,并将 same 变量设置为您的方法结果,无论您调用的是哪个方法。

标签: java string if-statement return-value


【解决方案1】:

您可以使用方法范围的字符串变量并设置它的值,而不是两个不同的字符串变量。像这样的:

public class CartCredentialsCollector {
//it should return something like this: 
//store_url=https://test.myshopify.com&apiKey=myapikey&apiPassword=myapipassword

public String credentailsCollector() throws IOException {
    String test = null;
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Do you want to connect a new shopping cart ('yes'/'no')");
    String newConnection = bufferedReader.readLine();
    if (newConnection.equals("no")) {
        bufferedReader.close();
    } else {
        System.out.println("Specify your shopping cart. It works with Shopify and Bigcommerce only");
        String newShoppingCart = bufferedReader.readLine();
        if (newShoppingCart.equals("Shopify")) {
            ShopifyCredentialsHandler sch = new ShopifyCredentialsHandler();
            test = sch.shopifyCredentialsHandler();
        }
        else if (newShoppingCart.equals("Bigcommerce")) {
        BigcommerceCredentialsHandler bch = new BigcommerceCredentialsHandler();
        test = bch.bigcommerceCredentialsHandler()
        }

        else {
            System.out.println("This method works with Shopify. Try to connect a Shopify store.");
            bufferedReader.close();
        }
    }
    return test;
    // Here I need to return test (in case I choose "Shopify") or test2 (in case I choose "Bigcommerce"). How can I do that?
    }
}

【讨论】:

    【解决方案2】:

    您应该在 if 语句之前声明 test 或进行多次返回 - 每个 if 中返回一个。

    【讨论】:

      【解决方案3】:

      正如您所意识到的,您无法访问要返回的“test”或“test2”变量的原因是因为它们当时都超出了范围。

      您需要在顶部(在您的第一个 if 语句之前)声明一个字符串变量,其中变量的范围将扩展到整个 credentialsCollector() 方法,或者使用多个返回,将每个返回与您的“test”和“test2”变量。

      【讨论】:

      • 这是一个合理的答案 - 但显示一个代码 sn-p 而不是描述代码 sn-p。
      • 感谢您的回答。我刚刚检查了@ritesh.garg 提出的结果,效果很好!我没有意识到我可以为每个 IF 语句创建 1 个测试字符串并使用测试字符串的返回值。我相信我应该是最好的选择。
      • 没问题!是的,这是两个选项中更好的一个。
      猜你喜欢
      • 1970-01-01
      • 2015-02-05
      • 1970-01-01
      • 2019-04-25
      • 2017-10-11
      • 1970-01-01
      • 1970-01-01
      • 2023-02-26
      • 1970-01-01
      相关资源
      最近更新 更多