请问如何用键盘向二维数组内的元素赋值啊?

Pginat 2017-12-10 01:05:20

请问如何用键盘向二维数组内的元素赋值啊?
...全文
418 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ooo-ooo 2017-12-11
  • 打赏
  • 举报
回复
感觉用不到二维数组啊
ooo-ooo 2017-12-11
  • 打赏
  • 举报
回复
无聊写了一下,不过跟你的题目思路不太一致,我是根据关系数据库的思想来做的。 1、学生类:

package classTest;

public class Student {
	public static Long indexId = (long) 0;
	
	private String name;
	private int age;
	private String sex;
	private Long id;
	
	public Student() {
		super();
	}
	
	public Student(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.id = indexId;
		indexId ++;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		if (age > 0) {
			this.age = age;
		} else {
			this.age = (-1) * age;
		}
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
}
2、课程类:

package classTest;

public class Course {
	public static Long indexId = (long) 0;
	private String name;
	private Long id;
	
	public Course() {
		super();
	}

	public Course(String name) {
		super();
		this.name = name;
		this.id = indexId;
		indexId ++;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
}
3、学生~课程类:

package classTest;

/**
 * 
 * @author Oliver
 * Grade: { student, course }
 * 存储学生~课程,包括分数信息
 */
public class Score {
	public static Long indexId = (long) 0;
	private Student student;
	private Course course;
	private int grade;
	private Long id;
	
	public Score() {
		super();
	}

	public Score(Student student, Course course, int grade) {
		super();
		this.student = student;
		this.course = course;
		this.grade = grade;
		this.id = indexId;
		indexId ++;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public Course getCourse() {
		return course;
	}

	public void setCourse(Course course) {
		this.course = course;
	}

	public int getScore() {
		return grade;
	}

	public void setScore(int grade) {
		this.grade = grade;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
}
4、工具类:

package classTest;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class StudentUtil {
	//储存的是学生~课程关联对象,即score
	private List<Score> scores;
	
	public StudentUtil() {
		super();
	}

	public List<Score> getScores() {
		return scores;
	}

	public void setScores(List<Score> scores) {
		this.scores = scores;
	}
	//打印所有学生~课程对象
	public void printAll() {
		Iterator<Score> iterator = scores.iterator();
		while (iterator.hasNext()) {
			Score score = iterator.next();
			System.out.println("姓名:" + score.getStudent().getName() + ", 性别:" + score.getStudent().getSex()
					+ ", 年龄:" + score.getStudent().getAge() + ", 学号:" + score.getStudent().getId()
					+ ", 分数:" + score.getScore());
		}
	}
	//打印指定学生~课程对象
	public void print(Student student) {
		System.out.println("姓名:" + student.getName() + ", 性别:" + student.getSex() + ", 年龄:" + student.getAge()
				+ ", 学号:" + student.getId());
	}
	//返回所有分数相关的计算结果
	public Map<String, Object> calculate(Course course) {
		int total = 0;
		int count = 0;
		int max = scores.get(0).getScore();
		int min = scores.get(0).getScore();
		Student maxScoreStudent = new Student();
		Student minScoreStudent = new Student();
		Map<String, Object> map = new HashMap<>();
		for (int i = 0; i < scores.size(); i++) {
			if (scores.get(i).getCourse().getId() == course.getId()) {
				total += scores.get(i).getScore();
				count ++;
			}
			if (max < scores.get(i).getScore()) {
				max = scores.get(i).getScore();
				maxScoreStudent = scores.get(i).getStudent();
			}
			if (min > scores.get(i).getScore()) {
				min = scores.get(i).getScore();
				minScoreStudent = scores.get(i).getStudent();
			}
		}
		map.put("total", total);
		map.put("count", count);
		map.put("max", maxScoreStudent);
		map.put("maxScore", max);
		map.put("min", minScoreStudent);
		map.put("minScore", min);
		return map;
	}
	//返回总分数
	public int getTotalScore() {
		Map<String, Object> score = calculate(null);
		int total = (int) score.get("total");
		return total;
	}
	//返回平均分
	public double getAverageScore(Course course) {
		Map<String, Object> score = calculate(course);
		int total = (int) score.get("total");
		int count = (int) score.get("count");
		double avg = total/count;
		return avg;
	}
	//返回最高分学生对象和最高分
	public Map<String, Object> getMaxScore(Course course) {
		Map<String, Object> score = calculate(course);
		Student maxScoreStudent = (Student) score.get("max");
		int maxScore = (int) score.get("maxScore");
		Map<String, Object> map = new HashMap<>();
		map.put("student", maxScoreStudent);
		map.put("score", maxScore);
		return map;
	}
	//返回最低分学生对象和最低分
	public Student getMinScore(Course course) {
		Map<String, Object> score = calculate(course);
		Student minScoreStudent = (Student) score.get("min");
		int minScore = (int) score.get("min");
		Map<String, Object> map = new HashMap<>();
		map.put("student", minScoreStudent);
		map.put("score", minScore);
		return minScoreStudent;
	}
}
5、测试类(随便写了一个,建议用Junit):

package classTest;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Test {
	public static void main(String[] args) {
		Student student1 = new Student("student1", 20, "female");
		Student student2 = new Student("student2", 22, "male");
		Student student3 = new Student("student3", 21, "female");
		Student student4 = new Student("student4", 19, "male");
		Student student5 = new Student("student5", 20, "female");

		Course java = new Course("Java 课程");
		Course math = new Course("高等数学");
		Course sports = new Course("体育");
		
		Score score1 = new Score(student1, java, 90);
		Score score2 = new Score(student2, java, 85);
		Score score3 = new Score(student3, java, 88);
		Score score4 = new Score(student4, java, 95);
		Score score5 = new Score(student5, java, 78);
		Score score6 = new Score(student1, math, 90);
		Score score7 = new Score(student2, math, 85);
		Score score8 = new Score(student3, math, 88);
		Score score9 = new Score(student4, math, 95);
		Score score10 = new Score(student5, math, 78);
		Score score11 = new Score(student1, sports, 90);
		Score score12 = new Score(student2, sports, 85);
		Score score13 = new Score(student3, sports, 88);
		Score score14 = new Score(student4, sports, 95);
		Score score15 = new Score(student5, sports, 78);
		
		StudentUtil studentCourse = new StudentUtil();
		List<Score> scores = new ArrayList<>();
		scores.add(score1);
		scores.add(score2);
		scores.add(score3);
		scores.add(score4);
		scores.add(score5);
		scores.add(score6);
		scores.add(score7);
		scores.add(score8);
		scores.add(score9);
		scores.add(score10);
		scores.add(score11);
		scores.add(score12);
		scores.add(score13);
		scores.add(score14);
		scores.add(score15);
		studentCourse.setScores(scores);
		
//		studentCourse.printAll();
		Map<String, Object> maxJava = studentCourse.getMaxScore(java);
		Student student = (Student) maxJava.get("student");
		System.out.println(maxJava.get("score") + ", " + student.getName());
	}
}
miaoch 2017-12-11
  • 打赏
  • 举报
回复
写个简单的demo吧:

Scanner sc = new Scanner(System.in);
		int [][] arr = new int[2][2];
		for (int i=0; i<2; i++) {
			for (int j=0; j<2; j++) {
				arr[i][j] = sc.nextInt();
			}
		}
		for (int i=0; i<2; i++) {
			for (int j=0; j<2; j++) {
				System.out.print(arr[i][j] + " ");
			}
			System.out.println();
		}
ooo-ooo 2017-12-11
  • 打赏
  • 举报
回复
返回总分那里写错了

public int getTotalScore(Course course) {
	Map<String, Object> score = calculate(course);
	int total = (int) score.get("total");
	return total;
}
什么都不能 2017-12-10
  • 打赏
  • 举报
回复
这是书本上的东西,应该从书本上找。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧