Java 打印数组中的字符

Java 打印数组中的字符,java,arrays,string,Java,Arrays,String,假设lines是一个字符串数组(如下面的向后应用程序所示)。编写一个for循环,在列中打印数组中字符串的长度,从数组中最后一个条目的长度开始,以位置0处的数组元素结束 例如,如果行由以下三个字符串组成: 现在是 时间 为了所有的好人 您的代码应打印: 16 8 六, 如何打印数组中的字符数?我尝试过这个问题至少20次都没有用!!!请帮忙 import java.util.*; public class Backwards { public static void main(String

假设lines是一个字符串数组(如下面的向后应用程序所示)。编写一个for循环,在列中打印数组中字符串的长度,从数组中最后一个条目的长度开始,以位置0处的数组元素结束

例如,如果行由以下三个字符串组成:

现在是
时间
为了所有的好人

您的代码应打印:

16
8
六,

如何打印数组中的字符数?我尝试过这个问题至少20次都没有用!!!请帮忙

import java.util.*;

public class Backwards {
    public static void main(String[] args) {
        String[] lines = new String[50];
        Scanner scan = new Scanner(System.in);
        int pos = 0;
        String t = " ";

        while(t.length() > 0){
            t = scan.nextLine();
            lines[pos] = t;
            pos++;
        }

        for(int j = pos  - 1; j >= 0; j--){
            lines[j] = lines[j].toUpperCase();
            System.out.println(lines[j]);
        }
    }
}

这显然是某种任务,所以我不会为您编写代码,但我会给您足够的提示,让您走上正确的轨道

您可以在数组中的字符串上反向循环,在运行时打印每个字符串的长度(您已经有了反向正确的循环)


字符串对象的名称应该是感兴趣的;-)

在println中,您应该打印行[j].length(),否则,您将打印出输入字符串。
另外,使用arrayList会更方便。

1)定义arrayList,比如arrLines。
1) Define an ArrayList, say arrLines.
2) Read each line of the input, convert to string, if required using .toString();and
   a)Calculate string length: strLength = <stringName>.length()
   b)Add the length calculated in step a) to arrLines : arrLines.add(strLength);
3) Print the arrLines in reverse:
 for(int i=arrLines.size();i>0;i--)
 {
   System.out.println(" "+arrLines.get(i));
 }
2) 读取输入的每一行,如果需要,使用.toString()转换为字符串;和 a) 计算字符串长度:strLength=.length() b) 将步骤a)中计算的长度添加到arrLines:arrLines.Add(strLength); 3) 按相反顺序打印线条: 对于(int i=arrLines.size();i>0;i--) { System.out.println(“+arrLines.get(i)); }
想想往回看:

for(int i = lines.length-1; i>=0; i--){  //starts the loop from the last array
  System.out.println(lines[i].length()); //prints out the length of the string at that index
}

这不应该标记在
作业
下吗?您的代码正在反转该数组中字符串的排列。是否确实要以相反的方式获取每行中的字符数?如果是,请阅读以下答案。。。