将For循环与weatherjavaweb服务结合使用
我编写了一种从WSDL Web服务获取天气详细信息的方法。我的代码工作正常,没有任何错误,但是有没有办法使用For循环/while从用户那里获取三个City和ZipCodes 程序应使用“for/while循环”提示第二、第三和更多城市的相同信息 我该怎么做 以下是WSDL文件: ` 这是我的密码:将For循环与weatherjavaweb服务结合使用,java,web-services,weather,Java,Web Services,Weather,我编写了一种从WSDL Web服务获取天气详细信息的方法。我的代码工作正常,没有任何错误,但是有没有办法使用For循环/while从用户那里获取三个City和ZipCodes 程序应使用“for/while循环”提示第二、第三和更多城市的相同信息 我该怎么做 以下是WSDL文件: ` 这是我的密码: package weather; import com.cdyne.ws.weatherws.ArrayOfWeatherDescription; import com.cdyne.ws.weat
package weather;
import com.cdyne.ws.weatherws.ArrayOfWeatherDescription;
import com.cdyne.ws.weatherws.WeatherReturn;
import java.util.Scanner;
/**
*
* @author elacy
*/
public class Weather {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
String zipCode1;
String zipCode2;
String zipCode3;
Scanner keyboard = new Scanner(System.in);
String City1;
String City2;
String City3;
com.cdyne.ws.weatherws.Weather service = new com.cdyne.ws.weatherws.Weather();
com.cdyne.ws.weatherws.WeatherSoap port = service.getWeatherSoap();
\\makes this part a For loop
System.out.println ("Please enter your First city");
City1 = keyboard.nextLine().toUpperCase();
System.out.println ("Please enter your First zipcode");
zipCode1 = keyboard.nextLine();
System.out.println ("Please enter your Second city");
City2 = keyboard.nextLine().toUpperCase();
System.out.println ("Please enter your Second zipcode");
zipCode2 = keyboard.nextLine();
System.out.println ("Please enter your Third city");
City3 = keyboard.nextLine().toUpperCase();
System.out.println ("Please enter your Third zipcode");
zipCode3 = keyboard.nextLine();
System.out.println("------------------------------------------------------");
System.out.println("EVAN'S WEATHER FORECAST FOR: SUNDAY, OCTOBER 07, 2013");
System.out.println("------------------------------------------------------");
System.out.println("City Zip Code Temp Relative Humidity");
System.out.println(City1 +" "+ zipCode1 +
port.getCityWeatherByZIP(zipCode1).getTemperature() +
port.getCityWeatherByZIP(zipCode1).getRelativeHumidity());
System.out.println(City2 +" "+ zipCode2 +
port.getCityWeatherByZIP(zipCode2).getTemperature() +
port.getCityWeatherByZIP(zipCode2).getRelativeHumidity());
System.out.println(City3 +" "+ zipCode1 +
port.getCityWeatherByZIP(zipCode3).getTemperature() +
port.getCityWeatherByZIP(zipCode3).getRelativeHumidity());
} catch (Exception ex) {
}
}
private static WeatherReturn getCityWeatherByZIP(java.lang.String zip) {
com.cdyne.ws.weatherws.Weather service = new com.cdyne.ws.weatherws.Weather();
com.cdyne.ws.weatherws.WeatherSoap port = service.getWeatherSoap();
return port.getCityWeatherByZIP(zip);
}
}
非常简单:
String[] cities = new String[3];
String[] zipCodes = new String[3];
for (int i = 0; i < 3; i++) {
System.out.println ("Please enter your City No." + (i + 1) + ":");
cities[i] = keyboard.nextLine().toUpperCase();
System.out.println ("Please enter your Zipcode No." + (i + 1) + ":");
zipCodes[i] = keyboard.nextLine();
}
System.out.println("------------------------------------------------------");
System.out.println("EVAN'S WEATHER FORECAST FOR: SUNDAY, OCTOBER 07, 2013");
System.out.println("------------------------------------------------------");
System.out.println("City Zip Code Temp Relative Humidity");
for (int i = 0; i < 3; i++) {
System.out.println(cities[i] +" " + zipCodes[i] +
port.getCityWeatherByZIP(zipCodes[i]).getTemperature() +
port.getCityWeatherByZIP(zipCodes[i]).getRelativeHumidity());
}
String[]cities=新字符串[3];
String[]zipCodes=新字符串[3];
对于(int i=0;i<3;i++){
System.out.println(“请输入您的城市编号”+(i+1)+“:”;
cities[i]=keyboard.nextLine().toUpperCase();
System.out.println(“请输入您的Zipcode编号”+(i+1)+“:”;
zipCodes[i]=keyboard.nextLine();
}
System.out.println(“--------------------------------------------------------------”);
System.out.println(“埃文天气预报:2013年10月7日星期日”);
System.out.println(“--------------------------------------------------------------”);
System.out.println(“城市邮政编码温度相对湿度”);
对于(int i=0;i<3;i++){
System.out.println(城市[i]+“”+zipCodes[i]+
port.getCityWeatherByZIP(zipCodes[i]).getTemperature()+
port.getCityWeatherByZIP(zipCodes[i]).getRelativeHumidity();
}
这是getCityWeatherByZIP
电话费贵吗?因为如果是的话,你应该每个邮政编码只调用它一次,而不是两次,并将结果存储在一个变量中。谢谢!现在如何打印System.out.println(City1+“”+zipCode1+port.getCityWeatherByZIP(zipCode1.getTemperature()+port.getCityWeatherByZIP(zipCode1.getRelativeHumidity());System.out.println(City2+“”+zipCode2+port.getCityWeatherByZIP(zipCode2.getTemperature()+port.getCityWeatherByZIP(zipCode2.getRelativeHumidity());System.out.println(City3+“”+zipCode1+port.getCityWeatherByZIP(zipCode3.getTemperature()+port.getCityWeatherByZIP(zipCode3.getRelativeHumidity());有新的变量吗?是的。请参阅我的更新答案以获取线索。您可能有兴趣将port.getCityWeatherByZIP(nextZip)
导出为变量并重用它。哦,好的,谢谢。我如何让它在收集输入后打印所有3个城市的信息。现在,当我使用该代码时,它会这样做。请输入您的城市编号1:shelby请输入您的Zipcode编号1:48317-----------------------------------------------埃文的天气预报:2007年10月7日,星期日,2013年------------------------------------------城市邮政编码Temp相对湿度SHELBY 48317 64 53请输入您的城市编号2:可能的方法:将读取的值存储在数组中,然后再次迭代并依次调用webservice。如何将它们存储在数组中?(nextCity,nextZip):