2017-02-28 101 views
0

如果我有一些IF語句,該如何返回1個值?如何在有多個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? 
    } 
} 
+0

在整個方法中可以有多個return語句。 – Compass

+0

一些替代方法:(1)在調用所選方法的位置返回字符串; (2)在方法的最外層範圍聲明String'test',並將這個* same *變量設置爲你的方法結果,無論你調用哪種方法。 –

回答

0

您可以使用方法範圍字符串變量並設置該值而不是兩個不同的字符串變量。事情是這樣的:

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? 
    } 
} 
0

您應該你的if語句或進行多次返回前申報test - 每一個裏面是否。

0

正如你已經意識到了,原因你不能訪問,你想使你的回報是因爲他們兩個都超出範圍時,該「測試」或「測試2」變量點。

您需要聲明一個字符串變量(在您的第一個if語句之前),其中變量的範圍將通過整個credentialsCollector()方法擴展,或使用多個返回值,將每個返回值放在與你的「測試」和「測試2」變量。

+0

這是一個合理的答案 - 但在描述代碼片段時顯示一段代碼片段。 – rbellamy

+0

感謝您的回答。我剛剛檢查了@ ritesh.garg提出的結果,它效果很好! 我沒有意識到我可以創建1個測試字符串,併爲每個IF語句使用測試字符串的返回值。我相信我應該是最好的選擇。 –

+0

沒問題!是的,這是兩種選擇中最好的。 – ClumsySyrup

相關問題