题目如下:
编写一个换算GPA的APPLET程序。对于学生学习的每门课程,都输入两个整数:考试成绩和学分,考试成绩按如下规则换算。
85~100:4
75~84:3
60~74:2
45~59:1
44以下:0
GPA等于换算后每门课的成绩的学分加权平均值(∑(成绩X学分)/∑学分)。
程序设计思路:包括两个输入文本框和两个提示标签,定义两个全局变量分别表示学分的和及成绩与学分乘积的和,在输入文本框中输入考试成绩和学分,按回车键执行action方法,文本框清空,可继续输入,直到用户在两个文本框输入-1,表示输入完毕,计算出学分加权平均值并显示输出。我编写的代码:import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Ex3_2_3 extends Applet
{
Label l1= new Label("考试成绩:");
TextField in1 = new TextField(10);
Label l2= new Label("学分:");
TextField in2 = new TextField(10);
Label l3 = new Label("");
public void init()
{
add(l1);
add(in1);
add(l2);
add(in2);
add(l3);
}
public boolean action(Event e , Object o)
{
double sum=0,mul;
int xuefen=0, kecheng=0;
int n1, n2;
String Str1=in1.getText();
String Str2=in2.getText();
n1 = Integer.parseInt(Str1);
n2 = Integer.parseInt(Str2);
if(n1>100 || n1<-1)
{
in1.setText("成绩输入错误");
repaint( );
return false;
}
if(n2<-1 || n2>4)
{
in2.setText("学分输入错误");
repaint( );
return false;
}
else if(n1>=0 && n1<=100)
{
sum += n1*n2;
xuefen += n2;
in1.setText("");
in2.setText("");
} else if( n1==-1 && n2==-1 )
{
l3.setText("平均分是:"+(sum/xuefen));
}
return true; }
}
编写一个换算GPA的APPLET程序。对于学生学习的每门课程,都输入两个整数:考试成绩和学分,考试成绩按如下规则换算。
85~100:4
75~84:3
60~74:2
45~59:1
44以下:0
GPA等于换算后每门课的成绩的学分加权平均值(∑(成绩X学分)/∑学分)。
程序设计思路:包括两个输入文本框和两个提示标签,定义两个全局变量分别表示学分的和及成绩与学分乘积的和,在输入文本框中输入考试成绩和学分,按回车键执行action方法,文本框清空,可继续输入,直到用户在两个文本框输入-1,表示输入完毕,计算出学分加权平均值并显示输出。我编写的代码:import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Ex3_2_3 extends Applet
{
Label l1= new Label("考试成绩:");
TextField in1 = new TextField(10);
Label l2= new Label("学分:");
TextField in2 = new TextField(10);
Label l3 = new Label("");
public void init()
{
add(l1);
add(in1);
add(l2);
add(in2);
add(l3);
}
public boolean action(Event e , Object o)
{
double sum=0,mul;
int xuefen=0, kecheng=0;
int n1, n2;
String Str1=in1.getText();
String Str2=in2.getText();
n1 = Integer.parseInt(Str1);
n2 = Integer.parseInt(Str2);
if(n1>100 || n1<-1)
{
in1.setText("成绩输入错误");
repaint( );
return false;
}
if(n2<-1 || n2>4)
{
in2.setText("学分输入错误");
repaint( );
return false;
}
else if(n1>=0 && n1<=100)
{
sum += n1*n2;
xuefen += n2;
in1.setText("");
in2.setText("");
} else if( n1==-1 && n2==-1 )
{
l3.setText("平均分是:"+(sum/xuefen));
}
return true; }
}
kecheng=0; 这两个变量没有用
程序没有问题的!呵呵
慢慢调试!
不知道是什么问题?呵呵
把double sum=0,mul; //移动
int xuefen=0, kecheng=0;//移动
移到类的属性。代码如下:
package ch07;import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Ex3_2_3 extends Applet{
Label l1= new Label("考试成绩:");
TextField in1 = new TextField(10);
Label l2= new Label("学分:");
TextField in2 = new TextField(10);
Label l3 = new Label("");
double sum=0,mul; //移动
int xuefen=0, kecheng=0;//移动 public void init(){ add(l1);
add(in1);
add(l2);
add(in2);
add(l3);
}
public boolean action(Event e , Object o) {
int n1, n2;
String Str1=in1.getText();
String Str2=in2.getText();
n1 = Integer.parseInt(Str1);
n2 = Integer.parseInt(Str2);
if(n1>100 || n1 <-1) {
in1.setText("成绩输入错误");
repaint( );
return false;
}
if(n2 <-1 || n2>4){
in2.setText("学分输入错误");
repaint( );
return false;
}else if(n1>=0 && n1 <=100) {
sum += n1*n2;
xuefen += n2;
in1.setText("");
in2.setText("");
}else if( n1==-1 && n2==-1 ) {
l3.setText("平均分是:"+(sum/xuefen));
}
return true; }
}