MineralTea 2017-04-17 03:45 采纳率: 0%
浏览 989
已结题

一个java的题目问题,请大家帮忙指导一下,谢谢!

题目如下:1.定义类People
属性:姓名,年龄,
方法:
设置姓名:通过参数设置姓名
获取姓名:返回姓名
设置年龄:通过参数设置年龄
获取年龄:返回年龄

定义People的子类Student
私有成员:学校
方法:
设置学校:通过参数设置学校
获取学校:返回学校
进行两个数的算术运算:输入两个数,再输入操作符+或-或*或/,根据操作符返回两个数的运算结果。(如果操作符不是这四个中的一个要求用户重新输入直到输入正确为止)
在类Test中定义main方法,创建Student类的对象,设置年龄,设置姓名,计算两个数的算术运算。

我的代码是:package work3;
import java.util.*;
public class People {

String name;
 int age;
static Scanner input=new Scanner(System.in);
People(String name,int age){
    this.name=name;
    this.age=age;
}
 void getName(){
    System.out.print("Your name:");
    String name=input.nextLine();
}
 void getAge(){
    System.out.print("Your age:");
    int age=input.nextInt();
}

void run(){
    System.out.println("Please input you to compute the two numbers:");
    int one=input.nextInt();
    int two=input.nextInt();
    System.out.println("Which operation way you want '+' '-' '*' '/'");
    char c=(input.next().charAt(0));
    if(c!='+'&&c!='-'&&c!='*'&&c!='/'){}
    else{
        switch(c){
        case'+':System.out.print(one+two);
        case'-':System.out.print(one-two);
        case'*':System.out.print(one*two);
        case'/':System.out.print(one/two);
        }
    }
  }

}

package work3;

public class Student extends People{

private static String school;
Student(String name, int age,String school) {
    super(name, age);
    this.school=school;
    // TODO Auto-generated constructor stub
}
void setSchool(){
    System.out.println("Your school:");
    String school=input.nextLine();
}
static String getSchool(){
    return school;
}
void search(){
    System.out.println("Your name:"+name+"Your age:"+age+"Your school:"+school);
}

public static void main(String[] args){
Student a=new Student("",10,"");
a.getName();
a.getAge();
a.getSchool();
a.search();
a.run();

}
}

我的问题主要是在第二个class中,我只会一开始就建立好一个student a并且是在代码中给他属性进行的赋值,而我不懂得如何进行编码才能使得student a的属性是通过在控制台输入来进行student a的创建,和属性的赋值,还请大家帮帮忙,谢谢(小白一枚,大家多多指教)

  • 写回答

6条回答

  • 夕阳雨晴 2017-04-17 03:54
    关注

    控制台输入来进行student a的基本属性值,通过set方法进行设置呀。

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮