英文:
Reorder the String
问题
以下是已经翻译好的内容:
以下的代码是有效的,但产生的输出如下:
Bob Mary Geoff
82 65 23
我要如何修改代码,以使输出看起来像这样:
Bob 82
Mary 65
Geoff 23
此外,我想为每个成绩设置一个范围,例如,85分以上是"HD",85-75分是"D",74-65分是"C",64-55分是"P",54-0分是"F"。
因此,不是为Bob、Mary和Geoff分配实际的成绩,而是为他们分配一个区间。例如:Bob D,Mary C,Geoff F。
public class Student {
String[] names;
int[] grades;
public Student(int numStudents) {
this.names = new String[numStudents];
this.grades = new int[numStudents];
}
public void addStudent(String name, int grade) {
int i = 0;
while (i < this.names.length && names[i] != null) {
i++;
}
if (i < names.length) {
names[i] = name;
grades[i] = grade;
}
}
public String toString() {
String result = "";
for (int i = 0; i < this.names.length; i++) {
result += this.names[i] + " " + this.grades[i] + "\n";
}
return result;
}
public static void main(String[] args) {
Student classRoom = new Student(3);
classRoom.addStudent("Bob", 82);
classRoom.addStudent("Mary", 65);
classRoom.addStudent("Geoff", 23);
System.out.println(classRoom);
}
}
请注意,我已经修改了 toString
方法,以按照你要求的格式输出学生的姓名和分数,并且还未实现根据分数区间分配等级的功能。如果需要实现分配等级的功能,你可以在 addStudent
方法中根据分数来设置相应的等级。
英文:
The following code does works but producing as
Bob Mary Geoff
82 65 23
What can I change to reorder the String to make it looks like
Bob 82 \n
Mary 65 \n
Geoff 23
Furthermore, I want to set a range for each grade, for example, grade over 85 is"HD", 85-75 is "D", 74 -65 is "C", 64 -55 is "P", 54- 0 is "F".
So instead of giving Bob, Mary and Geoff a actual grade, it gives them an interval. For example: Bob D, Mary C, Geoff F.
public class Student {
String[] names;
int[] grades;
public Student(int numStudents) {
this.names = new String[numStudents];
this.grades = new int[numStudents];
}
public void addStudent(String name, int grade) {
int i = 0;
while (i < this.names.length && names[i] != null) {
i++;
}
if (i < names.length) {
names[i] = name;
grades[i] = grade;
}
}
public String toString() {
String result = "";
for (int i=0; i < this.names.length; i ++) {
result += this.names[i] + " ";
}
result += "\n";
for (int i = 0; i< this.grades.length; i++) {
result += this.grades[i] + " ";
}
return result;
}
public static void main(String[] args) {
Student classRoom = new Student(3);
classRoom.addStudent("Bob", 82);
classRoom.addStudent("Mary", 65);
classRoom.addStudent("Geoff", 23);
System.out.println(classRoom);
}
答案1
得分: 4
Sure, here's the translated code:
遍历所有的姓名,将它们添加到字符串中,然后再处理成绩。
如果同时遍历它们,可以将它们一个接一个地添加到字符串中:
public String toString() {
String result = "";
for (int i = 0; i < this.names.length; i++) {
result += this.names[i] + " " + this.grades[i] + "\n";
}
return result;
}
另外,请不要在循环中使用字符串连接,而应该使用 `StringBuilder`:
public String toString() {
StringBuilder result = new StringBuilder();
for (int i = 0; i < this.names.length; i++) {
result.append(this.names[i]).append(" ").append(this.grades[i]).append("\n");
}
return result.toString();
}
这是性能改进,因为不需要在每次迭代中创建新的字符串。
如果您想根据条件设置成绩,可以使用 if-else,就像在 nylanderDev 的回答 中所示(如果您想要支持我的回答,请不要忘记给它点赞):
public String toString() {
StringBuilder result = new StringBuilder();
for (int i = 0; i < this.names.length; i++) {
String grade;
if (this.grades[i] > 85) {
grade = "HD";
} else if (this.grades[i] >= 75) {
grade = "D";
} else if (this.grades[i] >= 65) {
grade = "C";
} else if (this.grades[i] >= 55) {
grade = "P";
} else {
grade = "F";
}
result.append(this.names[i]).append(" ").append(grade).append("\n");
}
return result.toString();
}
希望这对您有帮助。
英文:
You loop through all the names, add those to the string and then do the same with the grades.
If you loop through both of them, you can add them to the string one after another:
public String toString() {
String result = "";
for (int i=0; i < this.names.length; i ++) {
result += this.names[i] + " "+this.grades[i]+"\n";
}
return result;
}
Also, do not use string concadation with loops. Use StringBuilder
instead:
public String toString() {
StringBuilder result = new StringBuilder();
for (int i=0; i < this.names.length; i ++) {
result.append(this.names[i]).append(" ").append(this.grades[i]).append("\n");
}
return result;
}
This is a performance improvement as you do not need to create a new string in every iteration.
If you want to set the grades according to criterias, you can use if-else as demonstrated in e.g. the answer of nylanderDev (don't forget to also upvote it if you want to upvote my answer):
public String toString() {
StringBuilder result = new StringBuilder();
for (int i=0; i < this.names.length; i ++) {
String grade;
if (this.grades[i] > 85) {
grade= "HD";
} else if (this.grades[i] >= 75) {
grade= "D";
} else if (this.grades[i] >= 65) {
grade= "C";
} else if (this.grades[i] >= 55) {
grade= "P";
} else {
grade="F";
}
result.append(this.names[i]).append(" ").append(grade).append("\n");
}
return result.toString();
}
答案2
得分: 2
你的问题的第二部分可以如下解决:
定义一个新的方法geLetterGrade:
String geLetterGrade(int grade) {
if (55 <= grade && grade <= 64)
return "P";
...
}
然后将其包含在toString代码中:
result += this.names[i] + " " + getLetterGrade(this.grades[i]) + "\n";
英文:
The second part of your question can be solved as follows:
Define a new method geLetterGrade:
String geLetterGrade(int grade) {
if (55 <= grade && grade <= 64)
return "P";
...
}
And then include it in the toString code:
result += this.names[i] + " "+getLetterGrade(this.grades[i])+"\n";
答案3
得分: 2
这是如何通过检查成绩范围来生成一个字符串的方法。
String gradeToString(int grade) {
if (grade > 85) {
return "HD";
} else if (grade >= 75) {
return "D";
} else if (grade >= 65) {
return "C";
} else if (grade >= 55) {
return "P";
} else return "F";
}
英文:
Here is how you can produce a string from a grade by checking what range it's in.
String gradeToString(int grade) {
if (grade > 85) {
return "HD";
} else if (grade >= 75) {
return "D";
} else if (grade >= 65) {
return "C";
} else if (grade >= 55) {
return "P";
} else return "F";
}
答案4
得分: 2
将toString方法的实现更改如下:
public String toString() {
String result = "";
for (int i = 0; i < names.length && i < grades.length; i++) {
result += names[i] + " " + grades[i] + "\n";
}
return result;
}
此外,我建议如果要实现此功能,可以使用HashMap或简单的映射方式,这样您可以添加一个Grade字段,并使用新的方法根据提供的值来决定成绩。
英文:
Change toString implementation as following:
public String toString() {
String result = "";
for (int i = 0; i < names.length && i < grades.length; i++) {
result += names[i] + " " + grades[i] + "\n";
}
return result;
}
Also I'd suggest using HashMap or a simple map if you want to implement this, that way you can add a Grade field as well, with a new method that can decide upon the grade based on values provided
答案5
得分: 1
Change toString
implementation to include name and grade in the same line followed by a line break as shown below:
public String toString() {
String result = "";
for (int i = 0; i < names.length && i < grades.length; i++) {
result += names[i] + " " + grades[i] + "\n";
}
return result;
}
Change the implementation of toString
to put name and the calculated grade in the same line followed by a line break as shown below:
public String toString() {
String result = "";
String grade;
for (int i = 0; i < names.length && i < grades.length; i++) {
grade = grades[i] > 85 ? "HD"
: (grades[i] >= 75 ? "D" : (grades[i] >= 65 ? "C" : (grades[i] >= 55 ? "P" : "F")));
result += names[i] + " " + grade + "\n";
}
return result;
}
This should be your final version of toString
in order to meet your requirements.
英文:
> What can I change to reorder the String to make it looks like Bob 82
> \n Mary 65 \n Geoff 23
Change toString
implementation to include name and grade in the same line followed by a line break as shown below:
public String toString() {
String result = "";
for (int i = 0; i < names.length && i < grades.length; i++) {
result += names[i] + " " + grades[i] + "\n";
}
return result;
}
> Furthermore, I want to set a range for each grade, for example, grade
> over 85 is"HD", 85-75 is "D", 74 -65 is "C", 64 -55 is "P", 54- 0 is
> "F". So instead of giving Bob, Mary and Geoff a actual grade, it gives
> them an interval. For example: Bob D, Mary C, Geoff F.
Change the implementation of toString
to put name and the calulated grade in the same line followed by a line break as shown below follows:
public String toString() {
String result = "";
String grade;
for (int i = 0; i < names.length && i < grades.length; i++) {
grade = grades[i] > 85 ? "HD"
: (grades[i] >= 75 ? "D" : (grades[i] >= 65 ? "C" : (grades[i] >= 55 ? "P" : "F")));
result += names[i] + " " + grade + "\n";
}
return result;
}
This should be your final version of toString
in order to meet your requirements.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论