- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在大学里第一次接触 Java。我正在努力打印工作。我无法打印,或者根本无法让程序打印任何内容。我认为问题出在显示方法上,但我无法解决它。因此,在下面的所有评论之后,我已经进行了更改,但是我现在发现它只打印每个方法中的标题,并在我尝试向数组添加附加事件时给出以下错误。
Exception in thread "main" java.lang.NullPointerException
at JavaAssignment1.TestEvent.addEvents(TestEvent.java:207)
at JavaAssignment1.TestEvent.main(TestEvent.java:28)
package JavaAssignment1;
import java.util.Scanner;
import firstPrograms.My;
import firstPrograms.MyDate;
import firstPrograms.MyTime;
public class TestEvent
{
public static Scanner key = new Scanner(System.in);
public static void main(String[] args)
{
//int noOfEvents = 25;
int option = 0;
Event [] allEvents = new Event [25];
do
{
option = mainMenu(option);
switch (option)
{
case 1:
addEvents(allEvents);
break;
case 2:
displayEvents(allEvents);
break;
case 3:
//System.out.println("\nEnter Search criteria here");
do
{
option = searchMenu(option);
switch (option)
{
case 1:
//addEventsByEventNo(allEvents);
System.out.println("\nEnter Search criteria here");
break;
case 2:
//displayEventsByEventDate(allEvents);
System.out.println("\nEnter Search criteria here");
break;
case 3:
//displayEventsByEventCategory(allEvents);
System.out.println("\nEnter Search criteria here");
break;
case 4:
//displayIncomes(allEvents);
break;
//case 5:
// displayCategoryIncomes(allEvents);
//break;
//case 6:
//System.out.println("\nGoodbye");
}
}while (option != 4);
break;
case 4:
displayIncomes(allEvents);
break;
case 5:
displayCategoryIncomes(allEvents);
break;
case 6:
System.out.println("\nGoodbye");
}
}while (option != 6);
}
public int createArray(Event [] allEvents)
{
allEvents[0] = new Event (1000, "West Ham United v Arsenal", "Premiership", new MyDate (9, 03, 2016), new MyDate (9, 04, 2016), new MyTime (12, 45, 00), "Upton Park", 3500, 3498, 65.00, 0.025);
allEvents[1] = new Event (1001, "Sunderland v Arsenal", "Premiership", new MyDate (24, 03, 2016), new MyDate (24, 04, 2016), new MyTime (14, 05, 00), "Stadium of Light", 500, 500, 75.00, 0.05);
allEvents[2] = new Event (1002, "Arsenal v Crystal Palace", "Premiership", new MyDate (17, 03, 2016), new MyDate (17, 04, 2016), new MyTime (16, 05, 00), "Emirates Stadium", 100, 98, 125.00, 0.025);
allEvents[3] = new Event (1003,"Arsenal v West Bromwich Albion", "Premiership", new MyDate (21, 03, 2016), new MyDate (21, 04, 2016), new MyTime (19, 45, 00), "Emirates Stadium", 125, 72, 95.00, 0.025);
allEvents[4] = new Event (1004, "Arsenal v Norwich City", "Premiership", new MyDate (30, 03, 2016), new MyDate (30, 04, 2016), new MyTime (17, 30, 00), "Emirates Stadium", 1250, 1137, 110, 0.025);
allEvents[5] = new Event (1005, "Manchester City v Arsenal", "Premiership", new MyDate (8, 04, 2016), new MyDate (8, 05, 2016), new MyTime (16, 05, 00), "Etihad Stadium", 5000, 4796, 75.00, 0.05);
allEvents[6] = new Event (1006, "Arsenal v Aston Villa", "Premiership", new MyDate (15, 04, 2016), new MyDate (15, 05, 2016), new MyTime (19, 30, 00), "Emirates Stadium", 1250, 872, 16.00, 0.025);
allEvents[7] = new Event (1007, "MLS All-Stars v Arsenal", "Friendly", new MyDate (28, 01, 2016), new MyDate (28, 07, 2016), new MyTime (17, 00, 00), "Avaya Stadium", 500, 391, 75.00, 0.05);
allEvents[8] = new Event (1008, "Chivas de Guadalajara v Arsenal", "Friendly", new MyDate (31, 01, 2016), new MyDate (31, 07, 2016), new MyTime (19, 30, 00), "StubHub Center", 1250, 872, 16.00, 0.025);
allEvents[9] = new Event (1009, "West Ham United v Manchester United", "Cup", new MyDate (13, 03, 2016), new MyDate (13, 04, 2016), new MyTime (19, 00, 00), "Upton Park", 750, 615, 85.00, 0.075);
allEvents[10] = new Event (1010, "Everton v TBC (West Ham or Man United)", "Cup", new MyDate (14, 04, 2016), new MyDate (23, 04, 2016), new MyTime (17, 15, 00), "Wembley Stadium", 10000, 5000, 150.00, 0.015);
allEvents[11] = new Event (1011, "Crystal Palace v Watford", "Cup", new MyDate (24, 03, 2016), new MyDate (24, 04, 2016), new MyTime (16, 00, 00), "Wembley Stadium", 10000, 10000, 150.00, 0.015);
allEvents[12] = new Event (1012, "FA Cup Final - TBC", "Cup", new MyDate (25, 04, 2016), new MyDate (21, 05, 2016), new MyTime (15, 00, 00), "Wembley Stadium", 20000, 0, 150.00, 0.015);
allEvents[13] = new Event (1013, "UEFA Champions League Final - TBC", "Europe", new MyDate (5, 5, 2016), new MyDate (28, 05, 2016), new MyTime (19, 45, 00), "Stadio Giuseppe Meazza", 25000, 0, 350.00, 0.01);
allEvents[14] = new Event (1014, "UEFA Europa League Final - TBC", "Europe", new MyDate (6, 05, 2016), new MyDate (18, 05, 2016), new MyTime (19, 45, 00), "St. Jakob-Park", 20000, 0, 250.00, 0.015);
return 16;
}
public static int mainMenu(int option)
{
int tempOption = 0;
System.out.println("Event Menu");
System.out.println("----------------------------");
System.out.println("1 - Add Event");
System.out.println("2 - Display All Events");
System.out.println("3 - Search Events");
System.out.println("4 - Display Incomes for Events");
System.out.println("5 - Display Incomes by Category");
System.out.println("6 - Exit");
System.out.println("Enter Option (1 - 6): ");
tempOption = key.nextInt();
key.nextLine();
return tempOption;
}
public static int searchMenu(int option)
{
int tempOption = 0;
System.out.println("Event Menu");
System.out.println("----------------------------");
System.out.println("1 - Search By Event No");
System.out.println("2 - Search By Date");
System.out.println("3 - Search By Category");
//System.out.println("4 - Display Incomes for Events");
//System.out.println("5 - Display Incomes by Category");
System.out.println("4 - Previous Menu");
System.out.println("Enter Option (1 - 4): ");
tempOption = key.nextInt();
key.nextLine();
return tempOption;
}
public int option(int min, int max)
{
int input;
boolean valid;
do
{
valid = false;
input = key.nextInt();
if (input < min || input > max)
{
System.out.printf("\n\n%s%d%s%d%s", "Invalid input! Please re-enter a value between ", min, " and ", max, ": ");
}
else
{
valid = true;
//System.out.printf("\n\n%s%d%s%d%s%d", "Number entered ", input, " is between ", min, " & ", max);
}
} while (!valid);
return input;
}
private static void displayEvents(Event [] allEvents)
{
System.out.println("\nEvents:");
for (int i = 0; i < allEvents.length; i++)
{
if(allEvents[i] != null)
System.out.printf("\n\n\t" + allEvents.toString());
}
}
private static void addEvents (Event[] allEvents)
{
System.out.println("\nAdd Events:");
int day = 0, month = 0, year = 0;
MyDate tempSaleDate;
MyDate tempEventDate;
int hour = 0, mins = 0, secs = 0;
MyTime tempTime;
boolean answer = false;
String keyedAnswer = "";
for (int i = 0; i < allEvents.length; i++)
{
Event[] newEvent = null;
newEvent[i] = new Event();
allEvents[i].setEventNo((allEvents[allEvents.length - 1]).getEventNo() + 1);
System.out.println("\n\tPlease enter event name for event: ");
allEvents[i].setEventName(key.nextLine());
System.out.println("\tPlease enter the event's category: ");
allEvents[i].setCategory(key.nextLine());
System.out.println("\tPlease enter event location: ");
allEvents[i].setVenue(key.nextLine());
System.out.println("\tPlease enter the event's tickets date of sale (ie 1 - 31): ");
day = key.nextInt();
System.out.println("\tPlease enter the event's tickets month of sale (ie 1 - 12): ");
month = key.nextInt();
System.out.println("\tPlease enter the event's tickets year of sale (ie 2016): ");
year = key.nextInt();
tempSaleDate = new MyDate(day, month, year);
allEvents[i].setDateOnSale(tempSaleDate);
key.nextLine();
System.out.println("\tPlease enter the event's date of event (ie 1 - 31): ");
day = key.nextInt();
System.out.println("\tPlease enter the event's month of event (ie 1 - 12): ");
month = key.nextInt();
System.out.println("\tPlease enter the event's year of event (ie 2016): ");
year = key.nextInt();
tempEventDate = new MyDate(day, month, year);
allEvents[i].setEventDate(tempEventDate);
key.nextLine();
System.out.println("\tPlease enter the event's start hour (ie 1am = 1, 10am = 10, 10pm = 22, etc): ");
hour = key.nextInt();
key.nextLine();
System.out.println("\tPlease enter the event's start minutes (ie = 00, 15, 30, 45, etc): ");
mins = key.nextInt();
key.nextLine();
tempTime = new MyTime(hour, mins, secs);
allEvents[i].setEventTime(tempTime);
System.out.println("\tPlease enter the event's ticket allocation: ");
allEvents[i].setTicketsReceived(key.nextInt());
key.nextLine();
System.out.println("\tPlease enter the amount of tickets sold for the event: ");
allEvents[i].setTicketsSold(key.nextInt());
key.nextLine();
System.out.println("\tPlease enter the event's ticket cost: £");
allEvents[i].setTicketCost(key.nextDouble());
key.nextLine();
System.out.println("\tPlease enter the event's ticket commission rate: ");
allEvents[i].setCommissionRate(key.nextDouble());
key.nextLine();
System.out.println("\n\t\tWould you like to add another event (Y / N): ");
keyedAnswer = key.nextLine();
//System.out.println("\n\tKeyed Answer = " + keyedAnswer);
if (answer = keyedAnswer.equalsIgnoreCase("N"))
{
answer = false;
break;
}
else
{
answer = true;
}
}
}
public static void displayIncomes(Event[] allEvents)
{
System.out.println("\n\nIncome per event");
System.out.println("\n\n\tEvent No" + "\t\tEvent" + "\t\t\t\tActual Income" + "\t\t\t\tPotential Income");
for (int i = 0; i < allEvents.length; i++)
{
if(allEvents[i] != null)
System.out.println("\n\n\t" + allEvents[i].getEventNo() + "\t\t" + allEvents[i].getEventName() + "\t\t\t£" + allEvents[i].getActualIncome() + "\t\t\t£" + allEvents[i].getPotentialIncome());
}
}
public static void displayCategoryIncomes(Event[] allEvents)
{
double tempActualIncome = 0.00, totalTemp = 0.00;
double percentageAchieved = 0.00;
String tempCategory;
System.out.println("\n\nIncome Per Category:");
System.out.println("\n\n\tCategory" + "\t\t\t\tActual Income" + "\t\t\tPecentage of Income Achieved");
for (int i = 0; i < allEvents.length; i++)
{
//if(allEvents[i] != null)
//{
//if (allEvents[i].getCategory() == "Premiership")
//if ("Premiership".equals(allEvents[i].getCategory()))
if (allEvents[i] != null && "Premiership".equals(allEvents[i].getCategory()))
{
//double tempActualIncome = 0.00;
//double percentageAchieved = 0.00;
//String tempCategory;
tempActualIncome = allEvents[i].getActualIncome();
totalTemp += tempActualIncome;
percentageAchieved = (tempActualIncome / allEvents[i].getPotentialIncome()) * 100;
tempCategory = "Premiership";
System.out.printf("%s%s%s%.2f%s%.2f", "\n\t", tempCategory, "\t\t\t\t£", totalTemp, "\t\t\t\t", percentageAchieved);
}
//else if (allEvents [i].getCategory() == "Friendly")
else if ("Friendly".equals(allEvents[i].getCategory()))
{
//double tempActualIncome = 0.00;
//double percentageAchieved = 0.00;
//String tempCategory;
tempActualIncome += allEvents[i].getActualIncome();
percentageAchieved = (tempActualIncome / allEvents[i].getPotentialIncome()) * 100;
tempCategory = "Friendly";
//System.out.println("\n\n\t" + tempCategory + "\t\t\t£" + tempActualIncome);
System.out.printf("%s%s%s%.2f%s%.2f", "\n\t", tempCategory, "\t\t\t\t£", tempActualIncome, "\t\t\t\t", percentageAchieved);
}
//else if (allEvents [i].getCategory() == "Cup")
else if ("Cup".equals(allEvents[i].getCategory()))
{
//double tempActualIncome = 0.00;
//double percentageAchieved = 0.00;
//String tempCategory;
tempActualIncome += allEvents[i].getActualIncome();
percentageAchieved = (tempActualIncome / allEvents[i].getPotentialIncome()) * 100;
tempCategory = "Cup";
System.out.println("\n\n\t" + tempCategory + "\t\t\t£" + tempActualIncome);
}
//else if (allEvents [i].getCategory() == "Europe")
else if ("Europe".equals(allEvents[i].getCategory()))
{
//double tempActualIncome = 0.00;
//double percentageAchieved = 0.00;
//String tempCategory;
tempActualIncome += allEvents[i].getActualIncome();
percentageAchieved = (tempActualIncome / allEvents[i].getPotentialIncome()) * 100;
tempCategory = "Europe";
System.out.println("\n\n\t" + tempCategory + "\t\t\t£" + tempActualIncome);
}
}
//}
//for (int i = 0; i < allEvents.length; i++)
//{
//System.out.println("\n\n\t" + allEvents[i].getCategory()+ "\t\t\t£" + allEvents[i].getActualIncome() + "\t\t\t£" + allEvents[i].getPotentialIncome());
//}
}
}
最佳答案
使用equals
方法比较字符串
<del>allEvents[i].getCategory() == "Premiership"</del>
"Premiership".equals(allEvents[i].getCategory())
您使用了许多运行应用程序不需要的静态
方法。在典型的控制台应用程序中,只有 main
方法应该是静态的。您应该创建对象,并在同一个类中使用它的方法和属性。
==
表示对象相等,具有相同的引用。只有使用像
public static final String PERMSHIP = "Premiership";
并在代码中的任何地方使用它,但这是非常糟糕的做法。
关于java - Java 中的打印是如何工作的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36619424/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!