How can I get a char array in reverse order?(如何以相反的顺序获取 char 数组?)
问题描述
我的作业题是这样的
编写一个程序,以相反的顺序打印字符数组中的字母
Write a program which prints the letters in a char array in reverse order using
void printReverse(char letters[], int size);
例如,如果数组包含 {'c', 's', 'c', '2', '6', '1'},则输出应为162csc".
For example, if the array contains {'c', 's', 'c', '2', '6', '1'} the output should be "162csc".
试过了,不知道什么意思
I tried, but I don't know what it means
void printReverse(char letters[], int size);
我这样做了,但调用方法printReverse"时出现问题.进入main方法
I did this but there's a problem with calling the method "printReverse" into the main method
import java.util.Arrays;
import java.util.Collections;
public class search {
public static void main(String[] args) {
char[] letters = {'e', 'v', 'o', 'l', '4'};
printReverse();
}
public void printReverse(char[] letters, int size) {
for (int i = letters.length-1; i >= 0 ; i--) {
System.out.print(letters[i]);
}
}
推荐答案
我相信你写的就是你必须创建的方法的签名.
I believe what you wrote is the signature of the method you have to create.
public void printReverse(char[] letters, int size){
//code here
}
您必须迭代数组并向后打印它包含的内容.使用反向for 循环"遍历字母"中的每个项目.我会让你自己把这些结合起来,因为这是一项任务.下面是一个 for 循环的例子:
You would have to iterate the array and print what it contains backwards. Use a reverse "for loop" to go through each item in "letters". I'll let you combine these yourself as it's an assignment. Here's an example of a for loop:
for (int i = array.length-1; i >= 0 ; i--){
System.out.print(array[i]);
}
这篇关于如何以相反的顺序获取 char 数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何以相反的顺序获取 char 数组?


- Eclipse 的最佳 XML 编辑器 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 转换 ldap 日期 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 获取数字的最后一位 2022-01-01