J
jinmen
Unregistered / Unconfirmed
GUEST, unregistred user!
1, 用 int num 可以declare 一个 variable. 用 integer num
= new integer() 也能declare一个reference variable. 按书上
说reference variable 和 variable 的不同,在于前者有
address sort 数据,后者则直接sort 数据。 那我不太明白为什
么要有个address来 sort数据,而不能直接sort呢? 我的意思是
,为什么要有个reference variable, 而不能全部都用variable
? 同样的,在用法上,variable 的 num 和 reference
variable 的 num 有什么不同? 是否reference variable 是一
个object,而variable是一个数值?他们之间能相互转换吗?
2, static final 用于declare 固定数据, 那 static final
int =1;
和 final int =1;
这两个指令有什么不同呢?我的意思
是,有stantic和没有stantic表达的意思有什么不同?
3,我的java文件在附件中。这程序是用tokenizer读取一个txt文
件中的很多组数据。
数据的次序是:性别(m or f), course1 分数,course2 分数,
GPA值。
然后输出 M F 两种性别分别读不同COURSE的不同人数,和每个的
平均分,平均GPA。
还有找出GPA的RANGE。
我compile后没有语法错误,但我用具体文件test却不行。你能帮
我看看是什么问题吗?
附源程序studentGpa.java
import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
public class studentGpa
{
public static void main (String[] args) throws IOException,FileNotFoundException
int maleCourse1 = 0;
int maleCourse2 = 0;
int maleCourse1Sum = 0;
int maleCourse2Sum = 0;
int maleCourse1Num = 0;
int maleCourse2Num = 0;
do
uble maleCourse1Mean = 0;
do
uble maleCourse2Mean = 0;
do
uble maleGpa = 0;
do
uble maleGpaMean = 0;
do
uble maleMaxGpa = 0;
do
uble maleMinGpa = 10;
do
uble totalMaleGpa = 0;
int femaleCourse1 = 0;
int femaleCourse2 = 0;
int femaleCourse1Sum = 0;
int femaleCourse2Sum = 0;
int femaleCourse1Num = 0;
int femaleCourse2Num = 0;
do
uble femaleCourse1Mean = 0;
do
uble femaleCourse2Mean = 0;
do
uble femaleGpa = 0;
do
uble femaleGpaMean =0;
do
uble femaleMaxGpa = 0;
do
uble femaleMinGpa = 10;
do
uble totalFemaleGpa = 0;
int totalStudentsCourse1;
int totalStudentsCourse2;
do
uble course1Mean;
do
uble course2Mean;
do
uble gpaMean;
do
uble maxGpa = 0;
do
uble minGpa = 0;
do
uble totalGpa = 0;
do
uble meanGpa = 0;
StringTokenizer tokenizer;
BufferedReader inFile = new BufferedReader (new FileReader("StudentData.txt"));
tokenizer = new StringTokenizer(inFile.readLine());
DecimalFormat twoDecimal = new DecimalFormat("0.00");
String inputLine = inFile.readLine();
while( inputLine != null)
{
char gender = tokenizer.nextToken().charAt(0);
switch (gender)
{
case 'M':
case 'm': maleCourse1 = Integer.parseInt(tokenizer.nextToken());
maleCourse2 = Integer.parseInt(tokenizer.nextToken());
maleGpa =do
uble.parseDouble(tokenizer.nextToken());
if (maleCourse1 <0 )
{System.out.println("Invalid grade, the line number in the file that was skipped.");
break;}
if (maleCourse2 <0 )
{System.out.println("Invalid grade, the line number in the file that was skipped.");
break;}
if (maleGpa <0 )
{System.out.println("Invalid GPA, the line number in the file that was skipped.");
break;}
if (maleCourse1 > 0)
{
maleCourse1Num++;
maleCourse1Sum = maleCourse1Sum + maleCourse1;}
if (maleCourse2 >0)
{
maleCourse2Num++;
maleCourse2Sum = maleCourse2Sum + maleCourse2;
}
totalMaleGpa = totalMaleGpa + maleGpa;
if ( maleGpa >= maleMaxGpa)
maleMaxGpa = maleGpa;
if ( maleGpa <= maleMinGpa)
maleMinGpa = maleGpa;
break;
case 'F':
case 'f': femaleCourse1 = Integer.parseInt(tokenizer.nextToken());
femaleCourse2 = Integer.parseInt(tokenizer.nextToken());
femaleGpa =do
uble.parseDouble(tokenizer.nextToken());
if (femaleCourse1 <0 )
{System.out.println("Invalid grade, the line number in the file that was skipped.");
break;}
if (femaleCourse2 <0 )
{System.out.println("Invalid grade, the line number in the file that was skipped.");
break;}
if (femaleGpa <0 )
{System.out.println("Invalid GPA, the line number in the file that was skipped.");
break;}
if (femaleCourse1 > 0)
{
femaleCourse1Num++;
femaleCourse1Sum = femaleCourse1Sum + femaleCourse1;}
if (femaleCourse2 > 0)
{
femaleCourse2Num++;
femaleCourse2Sum = femaleCourse2Sum + femaleCourse2;
}
totalFemaleGpa = totalFemaleGpa + femaleGpa;
if ( femaleGpa >= femaleMaxGpa)
femaleMaxGpa = femaleGpa;
if ( femaleGpa <= femaleMinGpa)
femaleMinGpa = femaleGpa;
break;
default: System.out.println("Invalid gender, the line number in the file that was skipped.");
}
}
totalStudentsCourse1 = maleCourse1Num + femaleCourse1Num;
totalStudentsCourse2 = maleCourse2Num + femaleCourse2Num;
course1Mean = (maleCourse1Sum + femaleCourse1Sum) / totalStudentsCourse1;
course2Mean = (maleCourse2Sum + femaleCourse2Sum) / totalStudentsCourse2;
maleCourse1Mean = maleCourse1Sum / maleCourse1Num;
maleCourse2Mean = maleCourse2Sum / maleCourse2Num;
femaleCourse1Mean = femaleCourse1Sum / femaleCourse1Num;
femaleCourse2Mean = femaleCourse2Sum / femaleCourse2Num;
if ( maleMaxGpa > femaleMaxGpa)
maxGpa = maleMaxGpa;
else
maxGpa = femaleMaxGpa;
if ( maleMinGpa < femaleMinGpa)
minGpa = maleMinGpa;
else
minGpa = femaleMinGpa;
totalGpa = totalMaleGpa + totalFemaleGpa;
meanGpa = 2 * totalGpa / (totalStudentsCourse1 + totalStudentsCourse2);
maleGpaMean = 2 * totalMaleGpa / (maleCourse1Num + maleCourse2Num);
femaleGpaMean = 2 * totalFemaleGpa / (femaleCourse1Num + femaleCourse2Num);
System.out.println(" " + "Course#1 " + "Course#2 " + "GPA");
System.out.println("Total Number of Students " + totalStudentsCourse1 + " " + totalStudentsCourse2 + " " + (int)(totalGpa));
System.out.println("Mean Grades " + course1Mean + " " + course2Mean + " " + twoDecimal.format(meanGpa));
System.out.println("GPA range " + " " + minGpa + " to " + maxGpa);
System.out.println("Number of female Students " + femaleCourse1Num + " " + femaleCourse2Num + " " + (int)(totalFemaleGpa));
System.out.println("Mean Grades(female) " + femaleCourse1Mean + " " + femaleCourse2Mean + " " + twoDecimal.format(femaleGpaMean));
System.out.println("GPA range(female) " + " " + femaleMinGpa + " to " + femaleMaxGpa);
System.out.println("Number of male Students " + maleCourse1Num + " " + maleCourse2Num + " " + (int)(totalMaleGpa));
System.out.println("Mean Grades(male) " + maleCourse1Mean + " " + maleCourse2Mean + " " + twoDecimal.format(maleGpaMean));
System.out.println("GPA range(male) " + " " + maleMinGpa + " to " + maleMaxGpa);
}
}
= new integer() 也能declare一个reference variable. 按书上
说reference variable 和 variable 的不同,在于前者有
address sort 数据,后者则直接sort 数据。 那我不太明白为什
么要有个address来 sort数据,而不能直接sort呢? 我的意思是
,为什么要有个reference variable, 而不能全部都用variable
? 同样的,在用法上,variable 的 num 和 reference
variable 的 num 有什么不同? 是否reference variable 是一
个object,而variable是一个数值?他们之间能相互转换吗?
2, static final 用于declare 固定数据, 那 static final
int =1;
和 final int =1;
这两个指令有什么不同呢?我的意思
是,有stantic和没有stantic表达的意思有什么不同?
3,我的java文件在附件中。这程序是用tokenizer读取一个txt文
件中的很多组数据。
数据的次序是:性别(m or f), course1 分数,course2 分数,
GPA值。
然后输出 M F 两种性别分别读不同COURSE的不同人数,和每个的
平均分,平均GPA。
还有找出GPA的RANGE。
我compile后没有语法错误,但我用具体文件test却不行。你能帮
我看看是什么问题吗?
附源程序studentGpa.java
import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
public class studentGpa
{
public static void main (String[] args) throws IOException,FileNotFoundException
int maleCourse1 = 0;
int maleCourse2 = 0;
int maleCourse1Sum = 0;
int maleCourse2Sum = 0;
int maleCourse1Num = 0;
int maleCourse2Num = 0;
do
uble maleCourse1Mean = 0;
do
uble maleCourse2Mean = 0;
do
uble maleGpa = 0;
do
uble maleGpaMean = 0;
do
uble maleMaxGpa = 0;
do
uble maleMinGpa = 10;
do
uble totalMaleGpa = 0;
int femaleCourse1 = 0;
int femaleCourse2 = 0;
int femaleCourse1Sum = 0;
int femaleCourse2Sum = 0;
int femaleCourse1Num = 0;
int femaleCourse2Num = 0;
do
uble femaleCourse1Mean = 0;
do
uble femaleCourse2Mean = 0;
do
uble femaleGpa = 0;
do
uble femaleGpaMean =0;
do
uble femaleMaxGpa = 0;
do
uble femaleMinGpa = 10;
do
uble totalFemaleGpa = 0;
int totalStudentsCourse1;
int totalStudentsCourse2;
do
uble course1Mean;
do
uble course2Mean;
do
uble gpaMean;
do
uble maxGpa = 0;
do
uble minGpa = 0;
do
uble totalGpa = 0;
do
uble meanGpa = 0;
StringTokenizer tokenizer;
BufferedReader inFile = new BufferedReader (new FileReader("StudentData.txt"));
tokenizer = new StringTokenizer(inFile.readLine());
DecimalFormat twoDecimal = new DecimalFormat("0.00");
String inputLine = inFile.readLine();
while( inputLine != null)
{
char gender = tokenizer.nextToken().charAt(0);
switch (gender)
{
case 'M':
case 'm': maleCourse1 = Integer.parseInt(tokenizer.nextToken());
maleCourse2 = Integer.parseInt(tokenizer.nextToken());
maleGpa =do
uble.parseDouble(tokenizer.nextToken());
if (maleCourse1 <0 )
{System.out.println("Invalid grade, the line number in the file that was skipped.");
break;}
if (maleCourse2 <0 )
{System.out.println("Invalid grade, the line number in the file that was skipped.");
break;}
if (maleGpa <0 )
{System.out.println("Invalid GPA, the line number in the file that was skipped.");
break;}
if (maleCourse1 > 0)
{
maleCourse1Num++;
maleCourse1Sum = maleCourse1Sum + maleCourse1;}
if (maleCourse2 >0)
{
maleCourse2Num++;
maleCourse2Sum = maleCourse2Sum + maleCourse2;
}
totalMaleGpa = totalMaleGpa + maleGpa;
if ( maleGpa >= maleMaxGpa)
maleMaxGpa = maleGpa;
if ( maleGpa <= maleMinGpa)
maleMinGpa = maleGpa;
break;
case 'F':
case 'f': femaleCourse1 = Integer.parseInt(tokenizer.nextToken());
femaleCourse2 = Integer.parseInt(tokenizer.nextToken());
femaleGpa =do
uble.parseDouble(tokenizer.nextToken());
if (femaleCourse1 <0 )
{System.out.println("Invalid grade, the line number in the file that was skipped.");
break;}
if (femaleCourse2 <0 )
{System.out.println("Invalid grade, the line number in the file that was skipped.");
break;}
if (femaleGpa <0 )
{System.out.println("Invalid GPA, the line number in the file that was skipped.");
break;}
if (femaleCourse1 > 0)
{
femaleCourse1Num++;
femaleCourse1Sum = femaleCourse1Sum + femaleCourse1;}
if (femaleCourse2 > 0)
{
femaleCourse2Num++;
femaleCourse2Sum = femaleCourse2Sum + femaleCourse2;
}
totalFemaleGpa = totalFemaleGpa + femaleGpa;
if ( femaleGpa >= femaleMaxGpa)
femaleMaxGpa = femaleGpa;
if ( femaleGpa <= femaleMinGpa)
femaleMinGpa = femaleGpa;
break;
default: System.out.println("Invalid gender, the line number in the file that was skipped.");
}
}
totalStudentsCourse1 = maleCourse1Num + femaleCourse1Num;
totalStudentsCourse2 = maleCourse2Num + femaleCourse2Num;
course1Mean = (maleCourse1Sum + femaleCourse1Sum) / totalStudentsCourse1;
course2Mean = (maleCourse2Sum + femaleCourse2Sum) / totalStudentsCourse2;
maleCourse1Mean = maleCourse1Sum / maleCourse1Num;
maleCourse2Mean = maleCourse2Sum / maleCourse2Num;
femaleCourse1Mean = femaleCourse1Sum / femaleCourse1Num;
femaleCourse2Mean = femaleCourse2Sum / femaleCourse2Num;
if ( maleMaxGpa > femaleMaxGpa)
maxGpa = maleMaxGpa;
else
maxGpa = femaleMaxGpa;
if ( maleMinGpa < femaleMinGpa)
minGpa = maleMinGpa;
else
minGpa = femaleMinGpa;
totalGpa = totalMaleGpa + totalFemaleGpa;
meanGpa = 2 * totalGpa / (totalStudentsCourse1 + totalStudentsCourse2);
maleGpaMean = 2 * totalMaleGpa / (maleCourse1Num + maleCourse2Num);
femaleGpaMean = 2 * totalFemaleGpa / (femaleCourse1Num + femaleCourse2Num);
System.out.println(" " + "Course#1 " + "Course#2 " + "GPA");
System.out.println("Total Number of Students " + totalStudentsCourse1 + " " + totalStudentsCourse2 + " " + (int)(totalGpa));
System.out.println("Mean Grades " + course1Mean + " " + course2Mean + " " + twoDecimal.format(meanGpa));
System.out.println("GPA range " + " " + minGpa + " to " + maxGpa);
System.out.println("Number of female Students " + femaleCourse1Num + " " + femaleCourse2Num + " " + (int)(totalFemaleGpa));
System.out.println("Mean Grades(female) " + femaleCourse1Mean + " " + femaleCourse2Mean + " " + twoDecimal.format(femaleGpaMean));
System.out.println("GPA range(female) " + " " + femaleMinGpa + " to " + femaleMaxGpa);
System.out.println("Number of male Students " + maleCourse1Num + " " + maleCourse2Num + " " + (int)(totalMaleGpa));
System.out.println("Mean Grades(male) " + maleCourse1Mean + " " + maleCourse2Mean + " " + twoDecimal.format(maleGpaMean));
System.out.println("GPA range(male) " + " " + maleMinGpa + " to " + maleMaxGpa);
}
}