英文:
How to use if condition for location
问题
我的应用程序 API 地址在 variables.java 文件中为:
public static final String main_domain = "example.com/api";
我想要根据不同的国家(基于位置)使用不同的 URL。那么如何设置条件语句,类似于:
if (location.equals("india")) {
public static final String main_domain = "example1.com/api";
}
if (location.equals("us")) {
public static final String main_domain = "example.com/api";
}
英文:
My application api url is
example.com/api in variables.java file
public static final string main_domain ="example.com/api";
I want to use different url for different country (location based)
Then how to set if condition like
if(location=india){ public static final string main_domain ="example1.com/api";}
if(location=us){ public static final string main_domain ="example.com/api";}
答案1
得分: 0
正确的比较符号是(==)。
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
if(location == us){ //code }
<!-- end snippet -->
英文:
The correct comparisson sign is (==).
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
if(location == us){ //code }
<!-- end snippet -->
答案2
得分: 0
如果您有多个 API 链接(五个或更多),我建议使用一个映射(map)。
首先,您需要像这样导入它:
import java.util.HashMap; // 导入 HashMap 类
然后像这样创建:
HashMap<String, String> apiUrl = new HashMap<String, String>();
然后像这样添加键值对:
apiUrl.put("india", "https://example1.com/api");
apiUrl.put("us", "https://example.com/api");
// 以此类推
然后您可以像这样直接使用它:
public static final String main_domain = apiUrl.get(location); // 请注意是 `String`,而不是 `string`
您可以使用 HashMap 的 get() 方法来访问特定的键。如果您已经知道键(如果您想直接引用它),您可以像这样使用:
String url = apiUrl.get("us");
否则,如果您有一个包含国家名称的变量,您可以像这样使用该变量:
String url = apiUrl.get(location);
您可能会从用户那里以输入的方式获得 location 值,或者它可能存储在数据库中,您可以从中获取并使用。
英文:
If you have multiple api urls (five or more), I suggest using a map.
First you will have to import it like this:
import java.util.HashMap; // import the HashMap class
Then create it like this:
HashMap<String, String> apiUrl = new HashMap<String, String>();
Then add key value pair like this:
apiUrl.put('india', 'https://example1.com/api');
apiUrl.put('us', 'https://example.com/api');
//and so on
Then you can use it directly like this:
public static final String main_domain = apiUrl.get(location);
//note that it is String
, not string
.
You can use get() method of HashMap to access a particular key. If you already know the key (if you want to refer to it directly), you can use it like
String url = apiUrl.get('us');
Otherwise, if you have a variable which contains the country name, you can use the variable like
String url = apiUrl.get(location);
You might be getting location value from the user as input or it might be stored in database, from where you fetch it and use.
答案3
得分: 0
上面的初始示例包括两个国家:印度和美国。根据示例的上下文,很可能您想要根据不同情况包含更多的国家。使用 switch 语句会更好。请尝试以下代码:
String mainDomain;
// 假设 location 变量是一个字符串
switch (location) {
case "india":
mainDomain = "example1.com/api";
break;
case "us":
mainDomain = "example.com/api";
break;
// 在此添加更多 case
default:
mainDomain = "example.com/api";
}
// mainDomain 在此处作为非空字符串可用
英文:
The initial example above consists of two countries: india and us. It is likely that you want to include more countries based on the context of the example. Using switch is better. Try this:
String mainDomain;
// assuming the location variable is a String
switch (location) {
case "india":
mainDomain = "example1.com/api";
break;
case "us":
mainDomain = "example.com/api";
break;
// more cases here
default:
mainDomain = "example.com/api";
}
// mainDomain is available as a non-null String here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论