gpt4 book ai didi

java - Java格式错误的url异常,用于简单的url

转载 作者:行者123 更新时间:2023-12-02 11:13:10 25 4
gpt4 key购买 nike

令人沮丧的格式错误的url异常。作为测试,我添加了“ URL url = new URL(” http://www.google.com“); ”只是为了查看一个非常简单的URL是否会引发错误,并且确实如此。我不知道是什么原因造成的。

import javax.swing.*;
import java.net.*;

public class Train {

public static void main(String[] args) {
URL url = new URL("http://www.google.com");
ImageIcon icon = new ImageIcon(new URL("https://upload.wikimedia.org/wikipedia/commons/d/d4/Wikipedesketch1.png"));**
Integer miles = Integer.parseInt(JOptionPane.showInputDialog("Enter the miles"));
Double gallons = Double.parseDouble(JOptionPane.showInputDialog("Enter the gallons"));
Double mpg = miles / gallons;
JOptionPane.showMessageDialog(null, "Miles per gallon is " + mpg, "Gass Mileage", JOptionPane.PLAIN_MESSAGE, icon);
}

}

线程“主”中的异常java.lang.Error: Unresolved 编译问题:
未处理的异常类型MalformedURLException
未处理的异常类型MalformedURLException
at Train.main(Train.java:13)

最佳答案

您有编译错误,没有try/catch for抛出异常
新的ImageIcon()

尝试这个:

import javax.swing.*;
import java.net.*;
public class Train {
public static void main(String[] args) {
try {
URL url = new URL("http://www.google.com");
ImageIcon icon = new ImageIcon(new URL("https://upload.wikimedia.org/wikipedia/commons/d/d4/Wikipedesketch1.png"));
Integer miles = Integer.parseInt(JOptionPane.showInputDialog("Enter the miles"));
Double gallons = Double.parseDouble(JOptionPane.showInputDialog("Enter the gallons"));
Double mpg = miles / gallons;
JOptionPane.showMessageDialog(null, "Miles per gallon is " + mpg, "Gass Mileage", JOptionPane.PLAIN_MESSAGE, icon);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}

关于java - Java格式错误的url异常,用于简单的url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47383369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com