bubbleTg 2017-04-22 02:31 采纳率: 75%

# （由于没c币了，望见谅）java编程练习题,题目做出来了，但没有通过测试用例，望大神求解，谢谢

N行，每行是一个标识符加一个平均得分（向下取整的整数），用空格隔开。

2
2 3 4
2 3 4 5

Student 3
Teacher 3

`````` import java.util.Scanner;
class Person
{
double x,y,z;
Person(double x,double y,double z)
{
this.x=x;
this.y=y;
this.z=z;
}
}
class Triangle extends Person
{
double e;
Triangle(double x, double y,double z,double z2) {
super(x, y,z);
this.e=z2;

}
int show()
{

return ((int)(x+y+z+e)/4);
}

}
class Rectangle extends Person
{

Rectangle(double x, double y,double z) {
super(x, y, z);

}
int show()
{
return (int)((x+y+z)/3);
}
}
public class Main
{
public static void main(String[] args)
{
double n,x,y,z,e;
Scanner cin=new Scanner(System.in);
n=cin.nextInt();
int []na=new int[(int)n];
for(int i=1;i<=n;i++)
{
if(i%2!=0)
{
x=cin.nextInt();
y=cin.nextInt();
z=cin.nextInt();
Rectangle re=new Rectangle(x, y, z);
na[i-1]=re.show();

}
else
{
x=cin.nextInt();
y=cin.nextInt();
z=cin.nextInt();
e=cin.nextInt();
Triangle th=new Triangle(x, y, z, e);
na[i-1]=th.show();
}
}
for(int i=0;i<n;i++)
{
if(i%2==0)
System.out.println("Student "+na[i]);
else
System.out.println("Teacher "+na[i]);

}

}
}

``````
• 写回答

#### 4条回答默认 最新

• 晓·飞段 2017-04-22 05:07
关注

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;

class Person {

``````private int aveGrades = 0;
private String personType = null;
public Person(double x1,double x2,double x3){
personType = "Student";
}
public Person(double x1,double x2,double x3,double x4){
personType = "Teacher";
}
}
}
public String getPersonType() {
return personType;
}
public void setPersonType(String personType) {
this.personType = personType;
}
public String toString(){
}
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Person> list = new ArrayList<Person>();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
while(n>0){

int[] x = new int[4];
String str = in.nextLine();
int count=0;//pan duan xia han you duosao shuju.
StringTokenizer st = new StringTokenizer(str," ");
while(st.hasMoreTokens()){

x[count++] = Integer.parseInt(st.nextToken());

}
Person p;
if(count==4){
p = new Person(x[0],x[1],x[2],x[3]);
}else{
p = new Person(x[0],x[1],x[2]);
}
n--;
}
Iterator<Person> it = list.iterator();
while(it.hasNext()){
System.out.println(it.next().toString());
}

}
``````

}

本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

#### 悬赏问题

• ¥85 3559A sensor时间同步
• ¥15 IGH主站与elmo驱动器连接时，pdo映射后无法输入输出
• ¥100 Android应用如何动态绑定邀请码
• ¥15 grbl用"!"命令停止移动后，怎么重置？
• ¥15 自己配的nginx 正向代理一直失败（相关搜索：谷歌浏览器|拒绝连接）