2013-10-05 6 views
1

Я закодировал способ получения метеорологических данных из веб-службы WSDL. Мой код работает правильно без каких-либо ошибок, но есть ли способ использовать цикл for/while, чтобы получить от пользователя три города и ZipCodes?Использование цикла с погодой Java Web Service

Программа должна запрашивать ту же информацию на втором, третьем и более городов путем использования «для/время цикла.»

Как я могу это сделать?

Вот файл WSDL:

`http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL

Вот мой код:

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); 
    }   
    } 
+0

ли, что' getCityWeatherByZIP' вызов на все дорого ? Потому что, если это так, вы должны называть его только один раз за почтовый индекс, а не дважды, и хранить результат в переменной. –

ответ

0

Довольно просто:

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()); 
} 
+0

Спасибо! Теперь как распечатать 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()); С новыми переменными? – TheEvanElement

+0

Да. См. Мой обновленный ответ для подсказки. Возможно, вам будет интересно экспортировать 'port.getCityWeatherByZIP (nextZip)' в качестве переменной и повторно использовать его. –

+0

О, ладно, спасибо. Как я могу распечатать все 3 города с их информацией после сбора данных. Прямо сейчас, когда я использую этот код, он делает это Пожалуйста, введите ваш Город № 1: shelby Пожалуйста, введите свой Почтовый индекс No.1: ------------------ ------------------------------------ ПРОГНОЗ ПОГОДЫ EVAN ДЛЯ: ВОСКРЕСЕНЬЕ, ОКТЯБРЬ 07, 2013 - -------------------------------------------------- --- Город Почтовый индекс Температура Относительная влажность SHELBY 48317 64 53 Пожалуйста, введите ваш Город No.2: – TheEvanElement