如何在代码中使用条件语句来处理位置。

huangapple go评论69阅读模式
英文:

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&lt;String, String&gt; apiUrl = new HashMap&lt;String, String&gt;();

Then add key value pair like this:

apiUrl.put(&#39;india&#39;, &#39;https://example1.com/api&#39;);

apiUrl.put(&#39;us&#39;, &#39;https://example.com/api&#39;);
//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(&#39;us&#39;);

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 &quot;india&quot;:
        mainDomain = &quot;example1.com/api&quot;;
        break;
    
    case &quot;us&quot;:
        mainDomain = &quot;example.com/api&quot;;
        break;

    // more cases here

    default:
        mainDomain = &quot;example.com/api&quot;;
}

// mainDomain is available as a non-null String here

huangapple
  • 本文由 发表于 2020年8月23日 12:28:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63543385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定