题目如下: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的创建,和属性的赋值,还请大家帮帮忙,谢谢(小白一枚,大家多多指教)
控制台输入来进行student a的基本属性值,通过set方法进行设置呀。
java.util.Scanner input = new java.util.Scanner(System.in);
DATA就是你输入的数据
望采纳========如下:
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("按回车enter,进入下一个:");
System.out.println("请输入第一个数:");
Integer i1 = s.nextInt();// 输入一个得到一个整数
System.out.println("请输入第二个数:");
Integer i2 = s.nextInt();// 输入一个得到一个整数
System.out.println("请输入符号:");
String fu = s.next();// 输入一个得到一个字符串
while (!fu.equals("end")) {
if (fu.equals("+")) {
System.out.println(i1 + "" + fu + "" + i2 + "=" + (i1 + i2));
fu = "end";
} else if (fu.equals("-")) {
System.out.println(i1 + "" + fu + "" + i2 + "=" + (i1 - i2));
fu = "end";
} else if (fu.equals("*")) {
System.out.println(i1 + "" + fu + "" + i2 + "=" + (i1 * i2));
fu = "end";
} else if (fu.equals("/")) {
System.out.println(i1 + "" + fu + "" + i2 + "="
+ (i1 * 1.0 / i2));
fu = "end";
} else {
System.out.println("+ - * /符号不对,请重新输入");
System.out.println("");
System.out.println("请输入符号:");
fu = s.next();// 输入一个得到一个字符串
}
}
}
Scanner s = new Scanner(System.in);
String name = s.nextLine();
int age = s.nextInt();
String school=s.nextLine();
Student stu=new Student(name,age,school);//
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
System.out.println("输入第一个值(true/false):");
if(sca.nextBoolean()){
System.out.println("输入:真的");
}else{
System.out.println("输入:假的");
}
System.out.println("输入第一个数字:");
System.out.println("输入数字:"+sca.nextInt());
System.out.println("输入一个字符串:");
System.out.println("输入字符串:"+sca.next());
System.out.println("输入一个布尔型:");
System.out.println("输入布尔型:"+sca.nextLong());
}
System.out.println("请输入你的姓名 年龄 学校,中间以空格分隔.");
String[] data=sc.next().split("\\s");
Student stu=new Student(data[0],Integer.valueOf(data[2]),data[2]);