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方法进行设置呀。

    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动