求一个简单的JAVA小程序,急急,今天就要,在线等
求一个简单的JAVA小程序,要用到:loop,不同methods,不同class,array或者arraylist,不予要用GUI,类似管理学生成绩的就可以,简单就好,1...
求一个简单的JAVA小程序,要用到:loop,不同methods, 不同class,array或者arraylist,不予要用GUI,类似管理学生成绩的就可以,简单就好,100-200行就行!急急,今天就要,在线等
展开
2个回答
2010-12-18
展开全部
给你个简单的,你看看吧:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test1 {
private static int count = 0;
public static void allStuInfo(Student [] stu) {
System.out.println();
System.out.println("学生信息:");
System.out.println("姓名 课程 成绩");
for (int i = 0; i < stu.length; i++) {
if (stu[i] == null) {
break;
}
System.out.print(stu[i].getName() + " ");
System.out.print(stu[i].getScore().getCourse() + " ");
System.out.println(stu[i].getScore().getScore());
}
}
public static void addStuInfo(Student [] stu, BufferedReader in) throws IOException {
while (true) {
System.out.println("输入学生姓名:(输入0退出添加)");
String name = in.readLine();
if (name.equals("0")) {
break;
}
stu[count] = new Student();
stu[count].setName(name);
System.out.println("输入课程名:");
String course = in.readLine();
System.out.println("输入课程成绩:");
String str = in.readLine();
float score = str.equals("") ? 0 : Float.parseFloat(str);
Score s = new Score();
s.setCourse(course);
s.setScore(score);
stu[count].setScore(s);
count++;
}
}
public static void inqStuInfo(Student [] stu, BufferedReader in) throws IOException {
while(true) {
System.out.println("输入学生姓名:(输入0退出查询)");
String name = in.readLine();
if (name.equals("0")) {
break;
}
for (int i = 0; i < stu.length; i++) {
if (stu[i] == null) {
break;
}
if (stu[i].getName().equals(name)) {
System.out.print(stu[i].getName() + " ");
System.out.print(stu[i].getScore().getCourse() + " ");
System.out.println(stu[i].getScore().getScore());
}
}
}
}
public static void main(String[] args) {
Student [] stu = new Student[20];
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try {
int count = 0;
while (count <= 20) {
System.out.println("输入1添加学生信息");
System.out.println("输入2查询学生信息");
System.out.println("输入3列出学生信息");
System.out.println("输入4退出");
String cmd = in.readLine();
if (cmd.equals("1")) {
addStuInfo(stu, in);
} else if (cmd.equals("2")) {
inqStuInfo(stu, in);
} else if (cmd.equals("3")) {
allStuInfo(stu);
} else if (cmd.equals("4")) {
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Score {
private String course; // 课程
private float score; // 成绩
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
}
class Student {
private String name; // 姓名
private Score score; // 成绩
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Score getScore() {
return score;
}
public void setScore(Score score) {
this.score = score;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test1 {
private static int count = 0;
public static void allStuInfo(Student [] stu) {
System.out.println();
System.out.println("学生信息:");
System.out.println("姓名 课程 成绩");
for (int i = 0; i < stu.length; i++) {
if (stu[i] == null) {
break;
}
System.out.print(stu[i].getName() + " ");
System.out.print(stu[i].getScore().getCourse() + " ");
System.out.println(stu[i].getScore().getScore());
}
}
public static void addStuInfo(Student [] stu, BufferedReader in) throws IOException {
while (true) {
System.out.println("输入学生姓名:(输入0退出添加)");
String name = in.readLine();
if (name.equals("0")) {
break;
}
stu[count] = new Student();
stu[count].setName(name);
System.out.println("输入课程名:");
String course = in.readLine();
System.out.println("输入课程成绩:");
String str = in.readLine();
float score = str.equals("") ? 0 : Float.parseFloat(str);
Score s = new Score();
s.setCourse(course);
s.setScore(score);
stu[count].setScore(s);
count++;
}
}
public static void inqStuInfo(Student [] stu, BufferedReader in) throws IOException {
while(true) {
System.out.println("输入学生姓名:(输入0退出查询)");
String name = in.readLine();
if (name.equals("0")) {
break;
}
for (int i = 0; i < stu.length; i++) {
if (stu[i] == null) {
break;
}
if (stu[i].getName().equals(name)) {
System.out.print(stu[i].getName() + " ");
System.out.print(stu[i].getScore().getCourse() + " ");
System.out.println(stu[i].getScore().getScore());
}
}
}
}
public static void main(String[] args) {
Student [] stu = new Student[20];
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try {
int count = 0;
while (count <= 20) {
System.out.println("输入1添加学生信息");
System.out.println("输入2查询学生信息");
System.out.println("输入3列出学生信息");
System.out.println("输入4退出");
String cmd = in.readLine();
if (cmd.equals("1")) {
addStuInfo(stu, in);
} else if (cmd.equals("2")) {
inqStuInfo(stu, in);
} else if (cmd.equals("3")) {
allStuInfo(stu);
} else if (cmd.equals("4")) {
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Score {
private String course; // 课程
private float score; // 成绩
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
}
class Student {
private String name; // 姓名
private Score score; // 成绩
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Score getScore() {
return score;
}
public void setScore(Score score) {
this.score = score;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询