猿问

数组越界?带字符串拆分

数组越界?我正在尝试执行图片中的输出:

使用此输入“Java 是一种编程语言”


到目前为止,这是我的代码


import java.util.Scanner;

public class Main

{

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        System.out.print("Input Phrase:");

        String s = in.nextLine();


        String[] word=s.split(" ");

        String rts=" ";


        for(int i=0;i<word.length;i++){

            if(word[i].length()>=rts.length()){

                rts=word[i];

            }

        }


        int thisislength = rts.length();


        for (int a = 0; a < thisislength ;a++ ) {

            for (int b = 0; b < word.length ;b++ ) {

                System.out.print(word[b].charAt(a)+" ");  

            }

            System.out.println();

        }

    }

}

当第二个单词到达它的最后一个字母时,它不会继续 for 循环,即使第二个单词达到其最大长度,有没有办法继续循环。


杨魅力
浏览 124回答 3
3回答

MMTTMM

<应该是<=。我认为将左右手颠倒使其更具可读性。&nbsp; &nbsp; for (int a = 0; a < thisislength; a++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("%3d ", a+1);&nbsp; &nbsp; &nbsp; &nbsp; for (int b = 0; b < word.length; b++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (a >= word[b].length()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(' ');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(word[b].charAt(a));&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(' ');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; }或者代替 if-else 语句:&nbsp; &nbsp; &nbsp; &nbsp; for (String w : word) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(a >= w.length() ? ' ' : w.charAt(a));&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }

慕后森

这给出了你想要的结果:for (int a = 0; a < thisislength ;a++ ){&nbsp; &nbsp; for (int b = 0; b < word.length ;b++ ){&nbsp; &nbsp; &nbsp; &nbsp; if(word[b].length() < a + 1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("&nbsp; ");&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(word[b].charAt(a) + " ");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println();}这一行被改变了:if(word[b].length() < a + 1)and notif(word[b].length() < a)和 2 个空格在 if 语句中打印

明月笑刀无情

试试这个解决方案希望它能帮助你:import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class Main{&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; // GET VALUE FROM THE CONSOLE&nbsp; &nbsp; &nbsp; &nbsp; Scanner in = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Input Phrase:");&nbsp; &nbsp; &nbsp; &nbsp; String s = in.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; // SPLIT STRING TO WORDS&nbsp; &nbsp; &nbsp; &nbsp; String[] words = s.split(" ");&nbsp; &nbsp; &nbsp; &nbsp; // CREATE A LIST OF CHAR_ARRAY CALLED : matrix&nbsp; &nbsp; &nbsp; &nbsp; List<char[]> matrix = new ArrayList<char[]>();&nbsp; &nbsp; &nbsp; &nbsp; // REFERENCE THE LARGEST WORD IN WORDS ARRAY EX : PROGRAMMING IS THE LARGEST&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; int max = 0;&nbsp; &nbsp; &nbsp; &nbsp; // FILL OUR LIST OF ARRAY OF CHARS&nbsp; &nbsp; &nbsp; &nbsp; for (int b = 0; b < words.length ;b++ ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char[] chars =&nbsp; words[b].toCharArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max = (chars.length >= max)? chars.length :&nbsp; max ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matrix.add( chars );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // PRINT OUR CHAR&nbsp; &nbsp; &nbsp; &nbsp; for (int a = 0; a < max ;a++ ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int b = 0; b < words.length ;b++ ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(a < matrix.get(b).length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(matrix.get(b)[a]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(" ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(" ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(" ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答