【发布时间】:2014-04-20 06:08:35
【问题描述】:
我对编程很陌生。我目前正在大学一年级学习 java。我得到了一个小组 java 项目。我的项目部分给了我 96 个不同的错误,但实际上给出的错误都没有错. 该项目接受 1 到 7 个命令行参数。然后它根据输入的内容做不同的事情。它整体分析代表乐透的文件的不同部分。我用命令行编译我的程序。如果需要,我可以发布所有错误。首先是我的整个代码,但我必须取出一个方法,以便它适合一个帖子。我取出的方法没有错误,因为我已经检查过了。它是 arguments 方法。
import javax.swing.JOptionPane;
import java.util.*;
import java.io.*;
import java.text.*;
import java.lang.*;
public class Text1
{
public static final String filename = "SampleLottoData.txt";
public static void main(String [] args) throws IOException, ParseException, AssertionError
{
File Numbers = new File(filename);
Scanner in = new Scanner(Numbers);
if(Numers.exists())
{
ArgumentCheck(args);
if(args.length <= 5)
int choice = Integer.parseInt(args[0]);
if (args.length >= 2 && args.length <= 5)
String drawtype = args[1].toUpperCase();
if(args.length >= 3 %% args.length <= 5)
int day = Integer.parseInt(args[2]);
if(args.length == 4)
int year = Integer.parseInt(args[3]);
if(args.length == 1)
JackpotOrBonus(args,in,choice);
else if(args.length == 2)
TypeOfDraw(args,in,choice,drawtype);
else if(args.length == 3)
DrawOnADay(args,in,choice,drawtype,day);
else if(args.length == 4)
DrawForAYear(args,in,choice,drawtype,day,year);
else if(args.length == 5)
BetweenTwoDates(args,in,choice,drawtype,day);
}
else JOptionPane.showMessageDialog(null,"The file " + filename + " doesn't exist");
}
public static void JackpotOrBonus(String [] args,Scanner in,int choice) throws IOException
{
int [] jackpotnumbers = new int[45], int [] bonusnumbers = new int[45], int [] jackpot = new int[6], int bonus = 0;
String data = "";
String [] temp = new String[9];
if(choice == 1)
{
while(in.hasNext())
{
data = in.nextLine();
temp = data.split(",");
for(int counter = 0;counter < jackpot.length;counter++)
{
jackpot[counter] = Integer.parseInt(temp[counter + 1]);
jackpotnumbers[jackpot[counter] - 1]++;
}
}
in.close();
}
else
{
while(in.hasNext())
{
data = in.nextLine();
temp = data.split(",");
bonus = Integer.parseInt(temp[7]);
bonusnumbers[bonus - 1]++;
}
in.close();
}
Printing(args,jackpotnumbers,bonusnumbers);
}
public static void TypeOfDraw(String [] args,Scanner in,int choice,String drawtype) throws IOException
{
int [] jackpotnumbers = new int[45], int [] bonusnumbers = new int[45], int [] jackpot = new int[6], int bonus = 0;
String data = "";
String [] temp = new String[9];
if(choice == 1 || choice == 2)
{
while(in.hasNext())
{
data = in.nextLine();
temp = data.split(",");
if(temp[8].contentEquals(drawtype) || drawtype.contentEquals("A"))
for(int count = 0;count<jackpot.length;count++)
{
jackpot[count] = Integer.parseInt(temp[count + 1]);
jackpotnumbers[jackpot[count] - 1]++;
}
}
in.close();
}
if(choice == 2)
{
while(in.hasNext())
{
data = in.nextLine();
temp = data.split(",");
bonus = Integer.parseInt(temp[7]);
if(temp[8].contentEquals(drawtype) || drawtype.contentEquals("A"))
bonusnumbers[bonus - 1]++;
}
in.close();
}
Printing(args,jackpotnumbers,bonusnumbers);
}
public static void DrawOnADay(String [] args,Scanner in,int choice,String drawtype,int day) throws IOException
{
int [] jackpotnumbers = new int[45], int [] bonusnumbers = new int[45], int [] jackpot = new int[6], int bonus = 0;
String data = "",weekday;
String [] temp = new String[9];
if(choice == 1 || choice == 2)
{
while(in.hasNext())
{
data = in.nextLine();
temp = data.split(",");
weekday = convertDateToWeekDay(temp);
if(weekday == day || day == 0);
{
if(temp[8].contentEquals(drawtype) || drawtype.contentEquals("A"))
for(int count = 0;count<jackpot.length;count++)
{
jackpot[count] = Integer.parseInt(temp[count + 1]);
jackpotnumbers[jackpot[count] - 1]++;
}
}
in.close;();
}
if(choice == 2)
{
while(in.hasNext())
{
data = in.nextLine();
temp = data.split(",");
weekday = convertDateToWeekDay(temp);
if(weekday == day || day == 0);
{
bonus = Integer.parseInt(temp[7]);
if(temp[8].contentEquals(drawtype) || drawtype.contentEquals("A"))
bonusnumbers[bonus - 1]++;
}
}
in.close();
}
}
Printing(args,jackpotnumbers,bonusnumbers);
}
public static void DrawsForAYear(String [] args,Scanner in,int choice,String drawtype,int day,int year) throws IOException
{
int [] jackpotnumbers = new int[45], int [] bonusnumbers = new int[45], int [] jackpot = new int[6], int bonus = 0;
String data = "",weekday;
String [] temp = new String[9];
String temporary = temp[0];
String [] temps = temporary.split("/");
int years = Integer.parseInt(temps[2]);
weekday = convertDateToWeekDay(temp);
while(in.hasNext())
{
data = in.nextLine();
temp = data.split(",");
String temporary = temp[0];
String [] temps = temporary.split("/");
int years = Integer.parseInt(temps[2]);
weekday = convertDateToWeekDay(temp);
if((year == years) && (temp[8].contentEquals(drawtype) || drawtype.contentEquals("A")) && (weekday == day || day == 0))
{
if (choice == 1 || choice == 2)
{
for(int count = 0;count<jackpot.length;count++)
{
jackpot[count] = Integer.parseInt(temp[count + 1]);
jackpotnumbers[jackpot[count] - 1]++;
}
}
if(choice == 2)
{
bonus = Integer.parseInt(temp[7]);
if(temp[8].contentEquals(drawtype) || drawtype.contentEquals("A"))
bonusnumbers[bonus - 1]++;
}
}
}
in.close();
EvenOrOdd(args,choice,in);
Printing(args,jackpotnumbers,bonusnumbers,year);
}
public static void EvenOrOdd(String [] args,int choice,Scanner in,int year)
{
int length, number,odd = 0,even = 0,lowexceedshighr,lowexceedshighs,lowexceedshighlp1,lowexceedshighlp2;
String data = "",weekday;
String [] temp = new String[9];
if(choice == 1)
length = 6;
else
length = 7;
while(in.hasNext())
{
int oddcounter = 0, evencounter = 0;
data = in.nextLine();
temp = data.split(",");
String temporary = temp[0];
String [] temps = temporary.split("/");
for(int counter = 0;counter<length;counter++)
{
number = Integer.parseInt(temp[counter + 1]);
if(number % 2 == 0)
evencounter++;
else
oddcounter++;
}
if(evencounter == length)
even++;
else if(oddcounter == length)
odd++;
int years = Integer.parseInt(temps[2]);
if (year == years)
{
int high = 36;
String 39s = "22/08/1992", 42s = "24/09/1994", 45s = "4/11/2006";
String datayear = temp[0];
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date datas = df.parse(datayear);
Date 39ss = df.parse(39s);
Date 42ss = df.parse(42s);
Date 45ss = df.parse(45s);
if(datas.equals(39ss) || (datas.after(39ss) && datas.before(42ss)))
high = 39;
else if(datas.equals(42ss) || (datas.after(42ss) && datas.before(45ss)))
high 42;
else if(datas.equals(45ss) || (datas.after(45ss)))
high = 45;
int limit = (high - 10);
for(int count = 0;count<jackpot.length;count++)
jackpot[count] = Integer.parseInt(temp[count + 1]);
int lowwinning = 0,highwinning = 0,
for(int counter = 0;counter<jackpot.length;counter++)
{
if(jackpot[counter] <= 10)
lowwinning++;
else if(jackpot[counter] >= limit)
highwinning++;
}
if (lowwining > highwinning)
{
if(temp[8].contentEquals("R"))
lowexceedshighr++;
else if(temp[8].contentEquals("S"))
lowexceedshighs++;
else if(temp[8].contentEquals("LP1"))
lowexceedshighlp1++;
else if(temp[8].contentEquals("LP2"))
lowexceedshighlp2++;
}
}
in.close();
String results = "";
if(choice == 1)
{
results = "All Jackpot numbers were even " + even + " times.\n";
results += "All Jackpot numbers were odd " + odd + " times.\n";
}
else
{
results = "All Jackpot and Bonus numbers were even " + even + " times.\n";
results += "All Jackpot and Bonus numbers were odd " + odd + " times.\n";
}
results += "In " + args[3] + " low winning numbers exceeded high winning numbers\n";
results += lowexceedshighr + " in regular draws.\n";
results += lowexceedshighs + " in special draws.\n";
results += lowexceedshighlp1 + " in Lotto Plus 1 draws.\n";
results += lowexceedshighlp2 + " in Lotto Plus 2 draws.";
JOptionPane.showMessageDialog(null,results,"Analysing the Lotto",1);
}
public static void BetweenTwoDates(String [] args,Scanner in,int choice,String drawtype,int day) throws IOException, ParseException, AssertionError
{
int [] jackpotnumbers = new int[45], int [] bonusnumbers = new int[45], int [] jackpot = new int[6], int bonus = 0;
String data = "",weekday;
String [] temp = new String[9];
while(in.hasNext())
{
data = in.nextLine();
temp = data.split(",");
String firstyear = args[3];
String secondyear = args[4];
String datayear = temp[0];
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date first = df.parse(firstyear);
Date second = df.parse(secondyear);
Date datas = df.parse(datayear);
weekday = convertDateToWeekDay(temp);
if(weekday == day || day == 0);
{
if(temp[8].contentEquals(drawtype) || drawtype.contentEquals("A"))
{
if((datas.equals(first)) || (datas.equals(second)) || ((datas.after(first)) && (datas.before(second))))
{
if(choice == 1 || choice == 2)
{
for(int count = 0;count<jackpot.length;count++)
{
jackpot[count] = Integer.parseInt(temp[count + 1]);
jackpotnumbers[jackpot[count] - 1]++;
}
}
if(choice == 2)
{
bonus = Integer.parseInt(temp[7]);
if(temp[8].contentEquals(drawtype) || drawtype.contentEquals("A"))
bonusnumbers[bonus - 1]++;
}
}
}
}
}
in.close();
Printing(args,jackpotnumbers,bonusnumbers);
}
public static int convertDateToWeekDay(String [] temp)
{
int result = 0;
int a, b, d, m, y, dayOfWeek;
String date = temp[0];
String [] dates = date.split("/");
d = Integer.parseInt(dates[0]);
m = Integer.parseInt(dates[1]);
y = Integer.parseInt(dates[2]);
if (m == 1 || m == 2)
{
m += 12; y -= 1;
}
a = y % 100; b = y / 100;
dayOfWeek = ((d + (((m + 1)*26)/10)+ a + (a/4) + (b/4)) + (5*b)) % 7;
switch(dayOfWeek)
{
case 0: result = 2; break;
case 4: result = 1; break;
}
return result;
}
public static void Printing(String [] args,int [] jackpotnumbers,int [] bonusnumbers)
{
String results = "";
if(args.length == 1)
{
if(choice == 1)
{
results = "An Analysis of Jackpot Numbers for the entire file.\n\n";
for(int counter = 0;counter<45;counter++)
{
int num = counter + 1;
results+= num + " was a jackpot number " + jackpotnumbers[counter] + "times.\n";
}
}
else
{
results = "An Analsis of Bonus Numbers for the entire file.\n\n";
for(int counter = 0;counter<45;counter++)
{
int num = counter + 1;
results+= num + " was a bonus number " + bonusnumbers[counter] + "times.\n";
}
}
}
if(args.length == 2)
{
if(choice == 1)
{
if((args[1].toUpperCase()).contentEquals("A"))
results = "An Analysis of Jackpot Numbers for the entire file.\n\n";
else if((args[1].toUpperCase()).contentEquals("R"))
results = "An Analysis of Jackpot Numbers for all regular draws.\n\n";
else if((args[1].toUpperCase()).contentEquals("S"))
results = "An Analysis of Jackpot Numbers for all special draws.\n\n";
else if((args[1].toUpperCase()).contentEquals("LP1"))
results = "An Analysis of Jackpot Numbers for all Lotto Plus 1 draws.\n\n";
else
results = "An Analysis of Jackpot Numbers for all Lotto Plus 2 draws.\n\n";
for(int counter = 0;counter<45;counter++)
{
int num = counter + 1;
results+= num + " was a jackpot number " + jackpotnumbers[counter] + "times.\n";
}
}
else
{
if((args[1].toUpperCase()).contentEquals("A"))
results = "An Analsis of Jackpot and Bonus Numbers for the entire file.\n\n";
else if((args[1].toUpperCase()).contentEquals("R"))
results = "An Analysis of Jackpot and Bonus Numbers for all regular draws.\n\n";
else if((args[1].toUpperCase()).contentEquals("S"))
results = "An Analysis of Jackpot and Bonus Numbers for all special draws.\n\n";
else if((args[1].toUpperCase()).contentEquals("LP1"))
results = "An Analysis of Jackpot and Bonus Numbers for all Lotto Plus 1 draws.\n\n";
else
results = "An Analysis of Jackpot and Bonus Numbers for all Lotto Plus 2 draws.\n\n";
for(int counter = 0;counter<45;counter++)
{
int num = counter + 1;
results+= num + " was a bonus number " + bonusnumbers[counter] + "times and a jackpot number " + jackpotnumbers[counter] + "times.\n";
}
if(args.length == 3)
{
int numb = Integer.parseInt(args[2]);
if(choice == 1)
{
if((args[1].toUpperCase()).contentEquals("A"))
results = "An Analysis of Jackpot Numbers for the entire file";
else if((args[1].toUpperCase()).contentEquals("R"))
results = "An Analysis of Jackpot Numbers for all regular draws";
else if((args[1].toUpperCase()).contentEquals("S"))
results = "An Analysis of Jackpot Numbers for all special draws";
else if((args[1].toUpperCase()).contentEquals("LP1"))
results = "An Analysis of Jackpot Numbers for all Lotto Plus 1 draws";
else
results = "An Analysis of Jackpot Numbers for all Lotto Plus 2 draws";
if(numb == 2)
results += " on Wednesday draws.\n\n\";
else if(numb == 1)
results += " on Saturday draws.\n\n\";
else
results += " for all draws.\n\n\";
for(int counter = 0;counter<45;counter++)
{
int num = counter + 1;
results+= num + " was a jackpot number " + jackpotnumbers[counter] + "times.\n";
}
}
else
{
if((args[1].toUpperCase()).contentEquals("A"))
results = "An Analsis of Jackpot and Bonus Numbers for the entire file";
else if((args[1].toUpperCase()).contentEquals("R"))
results = "An Analysis of Jackpot and Bonus Numbers for all regular draws";
else if((args[1].toUpperCase()).contentEquals("S"))
results = "An Analysis of Jackpot and Bonus Numbers for all special draws";
else if((args[1].toUpperCase()).contentEquals("LP1"))
results = "An Analysis of Jackpot and Bonus Numbers for all Lotto Plus 1 draws";
else
results = "An Analysis of Jackpot and Bonus Numbers for all Lotto Plus 2 draws";
if(numb == 2)
results += " on Wednesday draws.\n\n";
else if(numb == 1)
results += " on Saturday draws.\n\n";
else
results += " for all draws.\n\n";
for(int counter = 0;counter<45;counter++)
{
int num = counter + 1;
results+= num + " was a bonus number " + bonusnumbers[counter] + "times and a jackpot number " + jackpotnumbers[counter] + "times.\n";
}
}
}
if(args.length == 5)
{
int numb = Integer.parseInt(args[2]);
if(choice == 1)
{
if((args[1].toUpperCase()).contentEquals("A"))
results = "An Analysis of Jackpot Numbers for the entire file";
else if((args[1].toUpperCase()).contentEquals("R"))
results = "An Analysis of Jackpot Numbers for all regular draws";
else if((args[1].toUpperCase()).contentEquals("S"))
results = "An Analysis of Jackpot Numbers for all special draws";
else if((args[1].toUpperCase()).contentEquals("LP1"))
results = "An Analysis of Jackpot Numbers for all Lotto Plus 1 draws";
else
results = "An Analysis of Jackpot Numbers for all Lotto Plus 2 draws";
if(numb == 2)
results += " on Wednesday draws\n\n";
else if(numb == 1)
results += " on Saturday draws\n\n";
else
results += " for all draws\n\n";
results += "for " + args[3] + ".\n\n";
for(int counter = 0;counter<45;counter++)
{
int num = counter + 1;
results+= num + " was a jackpot number " + jackpotnumbers[counter] + "times.\n";
}
}
else
{
if((args[1].toUpperCase()).contentEquals("A"))
results = "An Analsis of Jackpot and Bonus Numbers for the entire file";
else if((args[1].toUpperCase()).contentEquals("R"))
results = "An Analysis of Jackpot and Bonus Numbers for all regular draws";
else if((args[1].toUpperCase()).contentEquals("S"))
results = "An Analysis of Jackpot and Bonus Numbers for all special draws";
else if((args[1].toUpperCase()).contentEquals("LP1"))
results = "An Analysis of Jackpot and Bonus Numbers for all Lotto Plus 1 draws";
else
results = "An Analysis of Jackpot and Bonus Numbers for all Lotto Plus 2 draws";
if(numb == 2)
results += " on Wednesday draws";
else if(numb == 1)
results += " on Saturday draws";
else
results += " for all draws";
results += "for " + args[3] + ".\n\n";
for(int counter = 0;counter<45;counter++)
{
int num = counter + 1;
results+= num + " was a bonus number " + bonusnumbers[counter] + "times and a jackpot number " + jackpotnumbers[counter] + "times.\n";
}
}
}
if(args.length == 5)
{
int numb = Integer.parseInt(args[2]);
if(choice == 1)
{
if((args[1].toUpperCase()).contentEquals("A"))
results = "An Analysis of Jackpot Numbers for the entire file";
else if((args[1].toUpperCase()).contentEquals("R"))
results = "An Analysis of Jackpot Numbers for all regular draws";
else if((args[1].toUpperCase()).contentEquals("S"))
results = "An Analysis of Jackpot Numbers for all special draws";
else if((args[1].toUpperCase()).contentEquals("LP1"))
results = "An Analysis of Jackpot Numbers for all Lotto Plus 1 draws";
else
results = "An Analysis of Jackpot Numbers for all Lotto Plus 2 draws";
if(numb == 2)
results += " on Wednesday draws\n\n";
else if(numb == 1)
results += " on Saturday draws\n\n";
else
results += " for all draws\n\n";
results += "between " + args[3] + " and " + args[4] + ".\n\n";
for(int counter = 0;counter<45;counter++)
{
int num = counter + 1;
results+= num + " was a jackpot number " + jackpotnumbers[counter] + "times.\n";
}
}
else
{
if((args[1].toUpperCase()).contentEquals("A"))
results = "An Analsis of Jackpot and Bonus Numbers for the entire file";
else if((args[1].toUpperCase()).contentEquals("R"))
results = "An Analysis of Jackpot and Bonus Numbers for all regular draws";
else if((args[1].toUpperCase()).contentEquals("S"))
results = "An Analysis of Jackpot and Bonus Numbers for all special draws";
else if((args[1].toUpperCase()).contentEquals("LP1"))
results = "An Analysis of Jackpot and Bonus Numbers for all Lotto Plus 1 draws";
else
results = "An Analysis of Jackpot and Bonus Numbers for all Lotto Plus 2 draws";
if(numb == 2)
results += " on Wednesday draws";
else if(numb == 1)
results += " on Saturday draws";
else
results += " for all draws";
results += "between " + args[3] + " and " + args[4] + ".\n\n";
for(int counter = 0;counter<45;counter++)
{
int num = counter + 1;
results+= num + " was a bonus number " + bonusnumbers[counter] + "times and a jackpot number " + jackpotnumbers[counter] + "times.\n";
}
}
}
}
}
JOptionPane.showMessageDialog(null,results,"Analysing the Lotto",1);
}
}
很抱歉代码很长,但我不知道实际问题出在哪里。我也很抱歉它没有评论,最终版本将是。由于字符问题,我无法在此处发布所有错误限制。我怎样才能告诉你错误是什么。有 96 个错误,但它们似乎都没有。错误是以下不同版本:
Text1.java:541: error: unclosed string literal
results += " for all draws.\n\n\";
Text1.java:391: error: <identifier> expected
int [] jackpotnumbers = new int[45], int [] bonusnumbers = new int[45], int [
] jackpot = new int[6], int bonus = 0;
Text1.java:391: error: ';' expected
int [] jackpotnumbers = new int[45], int [] bonusnumbers = new int[45], int [
] jackpot = new int[6], int bonus = 0;
Text1.java:391: error: illegal start of expression
int [] jackpotnumbers = new int[45], int [] bonusnumbers = new int[45], int [
] jackpot = new int[6], int bonus = 0;
Text1.java:349: error: not a statement
for(int counter = 0;counter<jackpot.length;counter++)
Text1.java:23: error: '.class' expected
int year = Integer.parseInt(args[3]);
如果您需要更多信息,请尽管询问。任何帮助将不胜感激,因为我现在不知道该怎么做,而且由于项目将于周二到期,因此我无法再次编写所有代码。
【问题讨论】:
-
删除
results += " for all draws.\n\n\";中的最后一个反斜杠它转义了",结果是字符串没有关闭 -
我这样做了,现在我有 100 个错误
-
好吧,你已经遇到了 3 次这个问题,然后还有很多其他问题 - 例如
String 39s = "22/08/1992", 42s = "24/09/1994", 45s = "4/11/2006";你不能以数字开头的变量。这只是挑选一个的错误...... -
基本上,我建议您重新开始,并经常编译。一旦你有一个错误,修复它。这样你就不会遇到这么大的问题了。
-
更好的是,使用 Eclipse 之类的 IDE,以便您在出错时立即知道。
标签: java error-handling compiler-errors