你在这里看到的是我的两个应用程序之一,一个用于创建类和变量,第二个用于获取信息并显示它。
package ch7;
import java.util.Calendar;
public class Dealership {
// public static final class variables
public static final int YEAR_STARTED = 1995;
public static final String COMPANY_NAME = "The Rusty Lemon";
public static final String COMPANY_URL = "www.TheRustyLemon.com";
public static final String COMPANY_ADDRESS =
"123 Rustbelt Road, Somewhere, SomeState, 12345";
public static final String COMPANY_SLOGAN =
"Many parts of our cars run great!";
public static final double STANDARD_MARKUP = 0.50;
public static final String COMPANY_EMAIL = "contact@therustylemon.com";
// public static methods
public static int getYearsInBusiness () {
int yearsOperating = 0;
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
yearsOperating = currentYear - YEAR_STARTED;
return yearsOperating;
}
public static String getCompanyBanner () {
return COMPANY_NAME + "\n(Selling rusty lemons since " +
YEAR_STARTED + ")\n" + COMPANY_ADDRESS + "\n" +
COMPANY_URL + "\n" + COMPANY_SLOGAN + "\n";
}
public static double getRetailPrice (double dealerCost,
double cleaningCost) {
double markup = dealerCost * STANDARD_MARKUP;
return dealerCost + cleaningCost + markup;
}
static String COMPANY_EMAIL() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
这是第二位代码。现在,当我运行它时,它将成功构建并运行但我在输出方面没有得到任何东西,即使我做了一些简单的事情
System.out.println("hi");
没什么,老实说,我无法弄清楚为什么在天堂会发生这种情况。
public class Rusty1
{
public static void main(String[] args)
{
String message1 = Dealership.getCompanyBanner();
int message2 = Dealership.getYearsInBusiness();
String message3 = Dealership.COMPANY_EMAIL();
System.out.print(message1);
System.out.print("We've been operating now for " + message2 + " years! To talk to one of our sales people, send an email to" + message3);
}
}
答案 0 :(得分:0)
在主方法中调用Dealership.COMPANY_EMAIL()
时会出现异常。
static String COMPANY_EMAIL() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
netbeans中的异常跟踪:
运行: 线程" main"中的例外情况java.lang.UnsupportedOperationException:尚不支持。 在javaapplication1.Dealership.COMPANY_EMAIL(经销商.java:46) 在javaapplication1.Rusty1.main(Rusty1.java:18) Java结果:1 建立成功(总时间:3秒)
答案 1 :(得分:0)
我编辑了你的方法:
static String COMPANY_EMAIL() {
if(COMPANY_EMAIL==null)
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
else
return COMPANY_EMAIL;
}
因此,如果电子邮件不存在或不受支持,则抛出异常。
此异常也会给您的程序带来问题,我建议您使用这样的方法:
static String COMPANY_EMAIL() {
if(COMPANY_EMAIL==null || COMPANY_EMAIL.equals(""))
return ("Email Function Not supported yet."); //return not supported
else
return COMPANY_EMAIL; //return original email
}