具有条件的java中的多个for循环

时间:2018-03-24 15:51:34

标签: java

我知道这是一个非常简单的问题,但我刚开始学习一门新语言。因此需要一些投入。

问题:我有三个不同长度的字符串。我需要调用该方法每次显示每个字符串的一个字符。如果相应的字符不可用,那么我需要传递一个空字符(默认值)

public class test {

    public static void main(String[] args) {
        String a = "hi";
        String b = "hey";
        String c = "hello";

        int len1 = a.length();
        int len2 = b.length();
        int len3 = c.length();
        int max = 0;
         if ( len1 > len2 && len1 > len3 )
                max = len1;
          else if ( len2 > len1 && len2 > len3 )
              max = len2;
          else if ( len3 > len1 && len3 > len2 )
              max = len3;

         for(int i=0; i<= max; i++) {
             char c1 = 0; char c2 = 0; char c3 = 0;
             //h,h,h
             //i,e,e
             //'',y,l
             //'','',l
             //'','',o
             printCharMerge(c1, c2, c3);
         }
    }

    public static void printCharMerge(char a, char b, char c) {
        System.out.println("A char val :"+ a + "B char val :"+ b + "C char val :"+ c);
    }
}

任何帮助和代码改进都将受到赞赏。

4 个答案:

答案 0 :(得分:0)

也许这可能有所帮助。

public class R
{
    public static void main(String[] args) {
        String a = "hi";
        String b = "hey";
        String c = "hello";

        int len1 = a.length();
        int len2 = b.length();
        int len3 = c.length();
        int max = 0;
        if ( len1 > len2 && len1 > len3 )
            max = len1;
        else if ( len2 > len1 && len2 > len3 )
            max = len2;
        else if ( len3 > len1 && len3 > len2 )
            max = len3;

        for(int i=0; i<= max; i++) {
            char c1 = 0; char c2 = 0; char c3 = 0;

            if(i >= a.length()) c1 = '\u0000';
            else c1 = a.charAt(i);

            if(i >= b.length()) c2 = '\u0000';
            else c2 = b.charAt(i);

            if(i >= c.length()) c3 = '\u0000';
            else c3 = c.charAt(i);

            printCharMerge(c1, c2, c3);
        }
    }

    public static void printCharMerge(char a, char b, char c) {
        System.out.println("A char val : '"+ a + "', B char val :'"+ b + "', C char val ':"+ c + "'");
    }
}

我冒昧地修改了printCharMerge的输出,因此可以清楚地知道打印的值是空字符。

另请注意,char的默认值为'\u0000'

逻辑 -

如果为字符串(a,b或c)调用charAt(i),并且i等于或大于该字符串的长度,那么StringIndexOutOfBoundsException将是i抛出。

因此,如果'\u0000'等于或大于该字符串的长度,则该字符变为char

然而:即使这会打印一个空格,因为空' '打印为public class R { public static void main(String[] args) { String a = "hi"; String b = "hey"; String c = "hello"; int len1 = a.length(); int len2 = b.length(); int len3 = c.length(); int max = 0; if ( len1 > len2 && len1 > len3 ) max = len1; else if ( len2 > len1 && len2 > len3 ) max = len2; else if ( len3 > len1 && len3 > len2 ) max = len3; for(int i=0; i<= max; i++) { String c1 = ""; String c2 = ""; String c3 = ""; if(i >= a.length()) c1 = ""; else c1 = ""+a.charAt(i); if(i >= b.length()) c2 = ""; else c2 = ""+b.charAt(i); if(i >= c.length()) c3 = ""; else c3 = ""+c.charAt(i); printCharMerge(c1, c2, c3); } } public static void printCharMerge(String a, String b, String c) { System.out.println("A char val : '"+ a + "',\tB char val : '"+ b + "',\tC char val : '"+ c + "'"); } } 。所以,你应该像这样使用Strings:

\t

请注意,字符串中使用的JMH代表水平制表符:)

答案 1 :(得分:0)

你快到了,替换你的for循环:

gnome-terminal -e "./my-router topology.txt A | tee -a outputA.txt";

您还可以缩短搜索最大值:

#!/bin/bash

gnome-terminal -e "./my-router topology.txt A | tee -a outputA.txt";
gnome-terminal -e "./my-router topology.txt B | tee -a outputB.txt";
gnome-terminal -e "./my-router topology.txt C | tee -a outputC.txt";
gnome-terminal -e "./my-router topology.txt D | tee -a outputD.txt";
gnome-terminal -e "./my-router topology.txt E | tee -a outputE.txt";
gnome-terminal -e "./my-router topology.txt F | tee -a outputF.txt";

答案 2 :(得分:0)

首先,您可以通过用逗号分隔在一行上声明多个相同类型的变量。其次,您可以通过使用Math.max(int, int)的嵌套调用来确定最多三个值。第三,您可以使用Character包装来存储null(没有&#34;空字符&#34;)。最后,您可以在打印例程中使用带有null防护的三元组。像,

public static void main(String[] args) {
    String a = "hi", b = "hey", c = "hello";
    int max = Math.max(a.length(), Math.max(b.length(), c.length()));
    for (int i = 0; i < max; i++) {
        Character x = i >= a.length() ? null : a.charAt(i), 
                y = i >= b.length() ? null : b.charAt(i),
                z = i >= c.length() ? null : c.charAt(i);
        printChar(x, y, z);
    }
}

public static void printChar(Character a, Character b, Character c) {
    if (a != null) {
        System.out.printf("A char val : %c ", a);
    }
    if (b != null) {
        System.out.printf("B char val : %c ", b);
    }
    if (c != null) {
        System.out.printf("C char val : %c", c);
    }
    System.out.println();
}

输出

A char val : h B char val : h C char val : h
A char val : i B char val : e C char val : e
B char val : y C char val : l
C char val : l
C char val : o

答案 3 :(得分:0)

使用。charAt,例如:

public class test {

    public static void main(String[] args) {
        String a = "hi";
        String b = "hey";
        String c = "hello";

        int len1 = a.length();
        int len2 = b.length();
        int len3 = c.length();
        int max = 0;
         if ( len1 > len2 && len1 > len3 )
                max = len1;
          else if ( len2 > len1 && len2 > len3 )
              max = len2;
          else if ( len3 > len1 && len3 > len2 )
              max = len3;

         for(int i=0; i<= max; i++) {
             char c1 = getChar(a, i); char c2 = getChar(b, i); char c3 = getChar(c, i);
             //h,h,h
             //i,e,e
             //'',y,l
             //'','',l
             //'','',o
             printCharMerge(c1, c2, c3);
         }
    }

    public static void printCharMerge(char a, char b, char c) {
        System.out.println("A char val :"+ a + "B char val :"+ b + "C char val :"+ c);
    }

    public static char getChar(String text, int index) {
        return (text.length() > index) ? text.charAt(index) : '\u0000';
    }
}