题目如下: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的创建,和属性的赋值,还请大家帮帮忙,谢谢(小白一枚,大家多多指教)