预期产量如下:
1
12
123
1234
12345
123456
1234567
12345678
123456789
1234567890
12345678901
123456789012
Run Code Online (Sandbox Code Playgroud)
以下是我将使用的起始代码:
import java.util.Scanner;
public class Pyramid {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Type in an integer value");
Scanner in = new Scanner(System.in);
int input = in.nextInt();
String str = "";
for(int i=1;i<=input;i++){
str += i;
System.out.println(str);
}
}
}
Run Code Online (Sandbox Code Playgroud)
以下是截至目前的输出.
Type in an integer value
15
1
12
123
1234
12345
123456
1234567
12345678
123456789
12345678910 …Run Code Online (Sandbox Code Playgroud) 我想知道两种不同用途之间的区别.我相信一些非常微妙的区别.
这是IBM参考手册中的解释.不过也许我的英语很糟糕,我无法想象其中的差异.
也许向我展示两种情况的例子可以帮助我更好地理解这一点.
以下是IBM的解释:
strchr子例程返回一个指针,该指针指向由String参数指向的字符串中的Character (转换为无符号字符)参数指定的第一个字符.如果字符串中没有出现该字符,则返回空指针.终止字符串的空字节被视为字符串的一部分.
strrchr子例程返回一个指针,该指针指向由String参数指向的字符串中的Character (转换为字符)参数指定的最后一个字符.如果字符串中没有出现该字符,则返回空指针.终止字符串的空字节被视为字符串的一部分.
考虑一个像这样的简单案例:
public static void array()
{
String myString = "STACKOVERFLOW";
int [] checkVal = new int[myString.length()];
for(int i=0; i<myString.length();i++){
checkVal[i] = (int)myString.charAt(i);
}
System.out.println(checkVal[0]);
System.out.println(checkVal[1]);
System.out.println(checkVal[2]);
System.out.println(checkVal[3]);
System.out.println(checkVal[4]);
System.out.println(checkVal);
}
Run Code Online (Sandbox Code Playgroud)
这将输出以下内容:
83
84
65
67
75
[I@fc9944
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释一下吗?如何从Array中检索正确的信息而不是内存分配?