[英]Multiple for loop in java with conditions
I know this is very simple question, but I just started leaning a new language. 我知道这是一个非常简单的问题,但是我刚刚开始学习一种新语言。 Hence need some inputs.
因此需要一些输入。
Problem: I had three strings with different lengths. 问题:我有三个长度不同的琴弦。 I need to invoke the method to display one character of each string every time.
我需要调用该方法以每次显示每个字符串的一个字符。 If respective character is not available then I need to pass an empty char (default value)
如果没有相应的字符,那么我需要传递一个空字符(默认值)
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);
}
}
Any help and code improvement will be appreciated. 任何帮助和代码改进将不胜感激。
Perhaps this could help. 也许这会有所帮助。
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 + "'");
}
}
I've taken the liberty of modifying the output of printCharMerge a bit, so that it is clearly understood that the value printed is a null character. 我已经自由地修改了printCharMerge的输出,以便可以清楚地理解所打印的值是一个空字符。
Also, keep in mind that the default value of char
is '\ '
另外,请记住,
char
的默认值为'\ '
If charAt(i)
is called for a string (a, b, or c), and i
is equal to or more than the length of that string, then a StringIndexOutOfBoundsException
would be thrown. 如果为字符串(a,b或c)调用
charAt(i)
,并且i
等于或大于该字符串的长度,则将引发StringIndexOutOfBoundsException
。
Hence, if i
is equal to or more than the length of that string, then the character becomes '\ '
因此,如果
i
等于或大于该字符串的长度,则字符变为'\ '
However: even this will print a space, because an empty char
is printed as ' '
. 但是:即使这样也会打印一个空格,因为空
char
被打印为' '
。 So, you should use Strings everywhere like this: 因此,应该在各处使用字符串,如下所示:
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 + "'");
}
}
And do note that \\t
used in a string stands for horizontal tab :) 并请注意,字符串中的
\\t
代表水平制表符:)
You are almost there, replace your for loop: 您快到了,替换for循环:
for(int i=0; i< max; i++) {
String c1 = (a.length() <= i ? "" : a.substring(i, i + 1));
String c2 = (b.length() <= i ? "" : b.substring(i, i + 1));
String c3 = (c.length() <= i ? "" : c.substring(i, i + 1));
System.out.println("A char val: "+ c1 + " B char val: "+ c2 + " C char val: "+ c3);
}
Also you can shorten the search for max value: 您也可以缩短对最大值的搜索:
int max = Math.max(a.length(), Math.max(b.length(), c.length()));
First, you can declare multiple variables of the same type on one line by separating with a comma. 首先,您可以通过用逗号分隔在一行上声明多个相同类型的变量。 Second, you can determine the maximum of three values by using nested calls to
Math.max(int, int)
. 其次,可以通过使用对
Math.max(int, int)
嵌套调用来确定三个值中的最大值。 Third, you can use Character
wrappers to store null
(there is no "empty character"). 第三,您可以使用
Character
包装器存储null
(没有“空字符”)。 Finally, you could use a ternary with a null
guard in your print routine. 最后,您可以在打印例程中使用具有
null
保护的三元组。 Like, 喜欢,
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();
}
Outputs 输出
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
Use . 采用 。
charAt
, like: 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';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.