【问题标题】:The operator < is undefined for the argument type(s) String, int ? in java [closed]运算符 < 未定义参数类型 String, int ?在java中[关闭]
【发布时间】:2021-08-10 02:24:46
【问题描述】:

我正在尝试在方法服装中设置一个条件,即如果温度

对于参数类型 String, int,未定义运算符

那么在下面的穿衣方法中,如何使用大于&gt;小于&lt;的这个呢?

import java.util.Random;

import net.aksingh.owmjapis.api.APIException;
import net.aksingh.owmjapis.core.OWM;
import net.aksingh.owmjapis.model.CurrentWeather;

public class Weather {
    
    Weather Tweather = new Weather();
    // method to test the API key
    public static String owmKey() {
        return "api-key"; // api key
    }

    static CurrentWeather cwd;

    String[] highTemp = { "Shirt", "T-shirt", "Jacket", "Raincoat" };
    String[] midTemp = { "sweater", "thermal-shirt" };
    String[] lowTemp = { "Trousers", "Jeans", "Shorts" };

    public static Boolean getCity(String city) {
        /*
         * Description: This method returns the name of the city.
         * 
         * @param city: value of to be validated.
         *
         * @return : return the back the name of the city if valid.
         */

        // declaring object of "OWM" class
        OWM owm = new OWM(owmKey());

        // getting current weather data for the selected city

        try {
            cwd = owm.currentWeatherByCityName(city);
            if (cwd.hasCityId()) {
                // we then return the city name
                return true;

            }

        } catch (APIException e) {
            // returns if city doesn't exist on the OWM API
            return false;
        }
        return null; // else return null
    }

    public String temp() {
        return "The temperature of are: Max-" + (cwd.getMainData().getTempMax() - 273.15) + " / Min - "
                + (cwd.getMainData().getTempMin() - 273.15) + "\'Celsius";
    }

    public String clothing() {
        Random clothings = new Random();
        int high = clothings.nextInt(highTemp.length - 1);
        int mid = clothings.nextInt(midTemp.length - 1);
        int low = clothings.nextInt(lowTemp.length - 1);
        
        if( Tweather.temp() < 10 ) {
            return "Adviesd Clothings By CTZ: " + highTemp[high]; 
        }

        return "Adviesd Clothings By CTZ: " + highTemp[high] + " and " + midTemp[mid] + " and " + lowTemp[low];
        // return "Adviesd Clothings By CTZ: "+ highTemp[high] +" and " + midTemp[mid] +
        // " and " + lowTemp[low];
    }
}

【问题讨论】:

  • .... temp() 不会给你温度。它给你一个字符串说“温度是:......”。如果您想要一种可以让您获得温度的方法,请编写一个并使用它。
  • 补充@khelwood 所说的内容,这也有助于为您的方法使用更好的名称以免混淆。名为clothing() 的方法有什么作用?从名称上肯定不清楚,temp() 也不是。

标签: java weather-api


【解决方案1】:

if( Tweather.temp() &lt; 10 ) 行中,Tweather.temp() 返回一个String。我将添加另一种方法,将实际最低温度返回为double。也许是这样的:

public double getMinTemp() {
    return cwd.getMainData().getTempMin() - 273.15;
}

然后使用该方法进行比较。

【讨论】:

  • 是的,谢谢,差别很大
猜你喜欢
  • 2017-06-04
  • 1970-01-01
  • 2022-12-13
  • 1970-01-01
  • 1970-01-01
  • 2018-06-30
  • 2018-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多