请问如何用键盘向二维数组内的元素赋值啊? 请问如何用键盘向二维数组内的元素赋值啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 写个简单的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(); } 无聊写了一下,不过跟你的题目思路不太一致,我是根据关系数据库的思想来做的。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()); }} 返回总分那里写错了public int getTotalScore(Course course) { Map<String, Object> score = calculate(course); int total = (int) score.get("total"); return total;} 一个关于io的问题?? SWT Table控件不可双击添加内容 下面代码的数据结构如何理解 Map<Integer, List>? System.out.printf("%d个数的和为%f\n", m, sum);什么意思? 关于JTale删除行的问题! 请教一个问题。在线等 请教一个正则表达式匹配的问题 如何在JTable的单元格内部显示一张图片呢? System.getProperty(String key) 请帮我!! 菜鸟求教,感谢各位大佬 java多线程怎么使主线程不用等待子线程执行完,就返回页面
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();
}
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());
}
}
Map<String, Object> score = calculate(course);
int total = (int) score.get("total");
return total;
}