如果我有一些IF语句,我怎么能返回1个值?
例如,当我选择Shopify时,它调用一个新方法,允许我输入一些需要凭据的控制台,然后将所有典型数据附加到1个String中并将该String(test)返回给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 :(得分:0)
你应该在你的if语句之前声明test
或者多次返回 - 每个都在一个内部。
答案 1 :(得分:0)
您可以使用方法范围String变量并设置其值而不是两个不同的String变量。像这样:
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 :(得分:0)
正如您已经意识到的那样,您无法访问"测试"或" test2"您想要返回的变量是因为它们都在此时超出了范围。
您需要在顶部(在第一个if语句之前)声明一个String变量,其中变量的范围将扩展到整个credentialsCollector()方法,或者使用多个返回,将每个返回放在与您的相同的范围内"测试"和" test2"变量