十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
public class TestApplet extends java.applet.Applet {
我们提供的服务有:成都网站制作、成都做网站、微信公众号开发、网站优化、网站认证、莒县ssl等。为千余家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的莒县网站制作公司
/** Initializes the applet TestApplet */
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setLayout(new java.awt.BorderLayout());
jPanel1.setLayout(null);
jLabel1.setText("\u65701\uff1a");
jPanel1.add(jLabel1);
jLabel1.setBounds(70, 30, 30, 15);
jPanel1.add(jTextField1);
jTextField1.setBounds(110, 30, 90, 21);
jLabel2.setText("\u65702\uff1a");
jPanel1.add(jLabel2);
jLabel2.setBounds(70, 70, 30, 15);
jPanel1.add(jTextField2);
jTextField2.setBounds(110, 60, 90, 21);
jButton1.setText("\u5929\u6c14");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel1.add(jButton1);
jButton1.setBounds(120, 110, 57, 23);
add(jPanel1, java.awt.BorderLayout.CENTER);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(jTextField1.getText().equals(jTextField2.getText())) {
JOptionPane.showMessageDialog(null, "今天天气很好", "天气", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "今天天气很坏", "天气", JOptionPane.INFORMATION_MESSAGE);
}
}
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
//通过中国天气api调用
private String getWeatherInfo2(){
StringBuilder info = new StringBuilder();
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httget = new HttpGet("");
ResponseHandlerString responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httget, responseHandler);
System.out.println(responseBody);
JsonParser jp = new JsonParser();
JsonElement jse = jp.parse(responseBody);
JsonObject jso = jse.getAsJsonObject().get("weatherinfo").getAsJsonObject();
// String updTime = jso.get("fchh").getAsString();
// if(updTime != null){
// //温度
// String j = jso.get("temp1").getAsString();//今天
// String m = jso.get("temp2").getAsString();//明天
// //天气情况
// String j_weather = jso.get("weather1").getAsString();//今天
// String m_weather = jso.get("weather2").getAsString();//明天
// //风向风力
// String j_wind = jso.get("wind1").getAsString();//今天
// String m_wind = jso.get("wind2").getAsString();//明天
// info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
// info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
// }
String updTime = jso.get("fchh").getAsString();
if(updTime != null){
if(!updTime.trim().equals("18")){
//温度
String j = jso.get("temp1").getAsString();//今天
String m = jso.get("temp2").getAsString();//明天
//天气情况
String j_weather = jso.get("weather1").getAsString();//今天
String m_weather = jso.get("weather2").getAsString();//明天
//风向风力
String j_wind = jso.get("wind1").getAsString();//今天
String m_wind = jso.get("wind2").getAsString();//明天
info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
}else{
//18
//温度
String temp1 = jso.get("temp1").getAsString();//今天
String temp2 = jso.get("temp2").getAsString();//今天
String temp3 = jso.get("temp3").getAsString();//今天
String j = temp1.split("~")[1] + "~" + temp2.split("~")[0];
String m = temp2.split("~")[1] + "~" + temp3.split("~")[0];//明天
//天气情况
String weather1 = jso.get("weather1").getAsString();
String weather2 = jso.get("weather2").getAsString();
String weather3 = jso.get("weather3").getAsString();
String j_weather = "";
String j_weather_part1 = "";
String j_weather_part2 = "";
//判断是否有转
if(weather1.indexOf("转") 0){
//有
j_weather_part1 = weather1.split("转")[1];
}else{
j_weather_part1 = weather1;
}
if(weather2.indexOf("转") 0){
//有
j_weather_part2 = weather2.split("转")[0];
}else{
j_weather_part2 = weather2;
}
if(j_weather_part1.equalsIgnoreCase(j_weather_part2)){
j_weather = j_weather_part1;//今天
}else{
j_weather = j_weather_part1 + "转" + j_weather_part2;//今天
}
String m_weather = "";
String m_weather_part1 = "";
String m_weather_part2 = "";
//判断是否有转
if(weather2.indexOf("转") 0){
//有
m_weather_part1 = weather2.split("转")[1];
}else{
m_weather_part1 = weather2;
}
if(weather3.indexOf("转") 0){
//有
m_weather_part2 = weather3.split("转")[0];
}else{
m_weather_part2 = weather3;
}
if(m_weather_part1.equalsIgnoreCase(m_weather_part2)){
m_weather = m_weather_part1;//今天
}else{
m_weather = m_weather_part1 + "转" + m_weather_part2;//明天
}
//风向风力
String j_wind = jso.get("wind2").getAsString();//今天
String m_wind = jso.get("wind3").getAsString();//明天
info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
}
}
} catch (Exception e) {
}
return info.toString();
}
import java.util.Scanner;
public class Weather {
public static String getWeatherInChinese(char c) {
switch (c) {
case 'D':
return "干燥";
case 'M':
return "潮湿";
case 'H':
return "炎热";
case 'R':
return "下雨";
default:
return "输入错误,无法转换";
}
}
public static void main(String[] args) {
boolean tbd = true;
do {
System.out.print("请输入天气情况的第一个英文字母:");
Scanner scanner = new Scanner(System.in);
String input = scanner.next().trim();
if (input.length() != 1) {
System.out.println("输入错误,无法转换!");
} else {
System.out.println(getWeatherInChinese(input.charAt(0)));
}
System.out.print("你想继续吗?(y/n)");
String sta = scanner.next();
tbd = sta.equals("y") ? true : false;
} while (tbd);
System.out.println("退出系统!");
}
}
运行截图:
天气晴天:it's sunny today.
有风:it's windy outside.
多云是cloudy,下雨是rainy,也有sprinkle是小雨,storm 暴雨
hurricane飓风,tornado龙卷风等等多种.
import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String weather="";
String answer="";
do
{
System.out.println("请输入第一个字母(d,m,h,r)");
//接收输入的天气
weather=input.next();
//根据输入判断
if(weather.equalsIgnoreCase("d"))
{
System.out.println("干燥");
}
if(weather.equalsIgnoreCase("m"))
{
System.out.println("潮湿");
}
if(weather.equalsIgnoreCase("h"))
{
System.out.println("炎热");
}
if(weather.equalsIgnoreCase("r"))
{
System.out.println("下雨");
}
else
{
System.out.println("输入错误");
}
System.out.println("是否继续输入?");
//接收是否输入为n
answer=input.next();
}while(!answer.equalsIgnoreCase("n"));
}
}
此处equalsIgnoreCase是不区分大小写的意思
如果要区分,则把他改成equals即可
①取出地址中的返回值(getWeatherReader方法)
②解析json格式的字符串
③形成你要展示的天气预报效果
public static String getWeatherReader() {//取得接口字符串
String currentLine = "";
String strReturn = "";
URL url = null;
HttpURLConnection conn = null;
InputStream in = null;
BufferedReader buff = null;
try {
url = new URL("");
System.out.println(url.toURI());
//打开地址链接
conn = (HttpURLConnection)url.openConnection();
conn.connect();
//接收数据
in = conn.getInputStream();
//如有乱码注意编码方式,如:UTF-8
buff = new BufferedReader(new InputStreamReader(in, "gb2312"));
while((currentLine = buff.readLine()) != null) {
strReturn += currentLine;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
in.close();
buff.close();
} catch (IOException e) {
return "8EF0000";
}
}
return strReturn;
}