重新排列字符串

huangapple go评论104阅读模式
英文:

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"
因此不是为BobMary和Geoff分配实际的成绩而是为他们分配一个区间例如Bob DMary CGeoff 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 &lt; this.names.length &amp;&amp; names[i] != null) {
i++;
}
if (i &lt; names.length) {
names[i] = name;
grades[i] = grade;
}
}
public String toString() {
String result = &quot;&quot;;
for (int i=0; i &lt; this.names.length; i ++) {
result += this.names[i] + &quot; &quot;;
}
result += &quot;\n&quot;;
for (int i = 0; i&lt; this.grades.length; i++) {
result += this.grades[i] + &quot; &quot;;
}
return result;
}
public static void main(String[] args) {
Student classRoom  = new Student(3);
classRoom.addStudent(&quot;Bob&quot;, 82);
classRoom.addStudent(&quot;Mary&quot;, 65);
classRoom.addStudent(&quot;Geoff&quot;, 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 = &quot;&quot;;
for (int i=0; i &lt; this.names.length; i ++) {
result += this.names[i] + &quot; &quot;+this.grades[i]+&quot;\n&quot;;
}
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 &lt; this.names.length; i ++) {
result.append(this.names[i]).append(&quot; &quot;).append(this.grades[i]).append(&quot;\n&quot;);
}
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 &lt; this.names.length; i ++) {
String grade;
if (this.grades[i] &gt; 85) {
grade= &quot;HD&quot;;
} else if (this.grades[i] &gt;= 75) {
grade= &quot;D&quot;;
} else if (this.grades[i] &gt;= 65) {
grade= &quot;C&quot;;
} else if (this.grades[i] &gt;= 55) {
grade= &quot;P&quot;;
} else {
grade=&quot;F&quot;;
}
result.append(this.names[i]).append(&quot; &quot;).append(grade).append(&quot;\n&quot;);
}
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 &lt;= grade &amp;&amp; grade &lt;= 64)
return &quot;P&quot;;
...
}

And then include it in the toString code:

            result += this.names[i] + &quot; &quot;+getLetterGrade(this.grades[i])+&quot;\n&quot;;

答案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 &gt; 85) {
return &quot;HD&quot;;
} else if (grade &gt;= 75) {
return &quot;D&quot;;
} else if (grade &gt;= 65) {
return &quot;C&quot;;
} else if (grade &gt;= 55) {
return &quot;P&quot;;
} else return &quot;F&quot;;
}

答案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 = &quot;&quot;;
for (int i = 0; i &lt; names.length &amp;&amp; i &lt; grades.length; i++) {
result += names[i] + &quot; &quot; + grades[i] + &quot;\n&quot;;    
}
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 = &quot;&quot;;
for (int i = 0; i &lt; names.length &amp;&amp; i &lt; grades.length; i++) {
result += names[i] + &quot; &quot; + grades[i] + &quot;\n&quot;;    
}
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 = &quot;&quot;;
String grade;
for (int i = 0; i &lt; names.length &amp;&amp; i &lt; grades.length; i++) {
grade = grades[i] &gt; 85 ? &quot;HD&quot;
: (grades[i] &gt;= 75 ? &quot;D&quot; : (grades[i] &gt;= 65 ? &quot;C&quot; : (grades[i] &gt;= 55 ? &quot;P&quot; : &quot;F&quot;)));
result += names[i] + &quot; &quot; + grade + &quot;\n&quot;;
}
return result;
}

This should be your final version of toString in order to meet your requirements.

huangapple
  • 本文由 发表于 2020年8月2日 22:05:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63216918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定