Java测试题

2024-11-30 02:12:25
推荐回答(3个)
回答1:

/**
* Student类
* @author hsf
* 没有实质的改动,把有关结果判断的方法放到Course类中,
* 在Student类中new Course的对象调用它的方法,功能是一样的。
*/
public class Student {

public static void main(String[] args) {
System.out.println("Welcome to the student course admin");

Course sc = new Course();
System.out.println("Collecting information for subject 0");
sc.evaluatue();

System.out.println("Collecting information for subject 1");
sc.evaluatue();

System.out.println("Collecting information for subject 2");
sc.evaluatue();
}

}

import java.util.Scanner;

/**
* Course类
*
* @author hsf
*/
public class Course {
int num1, num2;

public void evaluatue() {
Scanner sn = new Scanner(System.in);
Course st = new Course();

System.out.println("Enter mark out of 50 for Assessment task 0:");
num1 = sn.nextInt();
st.check(num1);

System.out.println("Enter mark out of 50 for Assessment task 1:");
num2 = sn.nextInt();
st.check(num2);

int sum = num1 + num2;
if (sum < 100 && sum >= 80) {
System.out.println("Subject Mark:" + sum + "-HD");
} else if (sum >= 70 && sum < 79) {
System.out.println("Subject Mark:" + sum + "-DI");
} else if (sum >= 60 && sum < 69) {
System.out.println("Subject Mark:" + sum + "-CR");
} else if (sum >= 50 && sum < 59) {
System.out.println("Subject Mark:" + sum + "-PA");
} else {
System.out.println("Less than 50 NN");
}
}

public void check(int num) {
boolean flag = true;
while (flag) {
if (num > 50 || num < 0) {
System.out.println("Invalid mark, Re-enter mark!");
Scanner sn = new Scanner(System.in);
if (num == num1) {
System.out
.println("Enter mark out of 50 for Assessment task 0:");
} else {
System.out
.println("Enter mark out of 50 for Assessment task 1:");
}

num = sn.nextInt();
} else {
flag = false;
}
}
}
}

回答2:

import java.util.Scanner;
public class Student
{
int sub0;
int sub1;
int sub2;

public Student()
{
sub0=0;
sub1=0;
sub2=0;
}
public void add(int a,int b)
{
if (a==0) sub0=sub0+b;
if (a==1) sub1=sub1+b;
if (a==2) sub2=sub2+b;

}
public int get(int a)
{
if (a==0) return sub0;
if (a==1) return sub1;
if (a==2) return sub2;
return -1;

}
}

class course
{
public static void main(String[] args)
{
int next=0;
boolean flag=true;
Student s1= new Student();
Scanner read = new Scanner(System.in);
System.out.println("Welcome to the student course admin");
for(int i=0;i<3;i++)
{
System.out.println("Collecting information for subject "+i);
for(int j=0;j<2;j++)
{
System.out.println("Enter mark out of 50 for Assessment task "+j);
while (flag==true)
{
next = read.nextInt();
flag=false;
if ((next<0)||(next>50))
{
System.out.println("Invalid mark, Re-enter mark!");
flag=true;
}
}
s1.add(i,next);
flag=true;
}

String myresult="";
int result=s1.get(i);
if (result<50) myresult=""+result+"-NN";
else if (result<60) myresult=""+result+"-PA";
else if (result<70) myresult=""+result+"-CR";
else if (result<80) myresult=""+result+"-DI";
else if (result<=100) myresult=""+result+"-HD";
System.out.println("Subject Mark "+myresult);
System.out.println();
System.out.println();
System.out.println();

}
}

}

回答3:

又一个计算成绩的
我这里有个计算GPA的
参考一下

http://zhidao.baidu.com/question/122300682.html