Java 如何重新打印从第一个字符到最后一个字符的字符串?
给定一个非空字符串str(如“Code”)打印一个字符串(如“CCoCodCode”)。其中,在字符串中的每个索引处,您必须将字符串重新打印到该索引 我知道我写的这个代码肯定有问题,因为答案应该是CCoCodCode,但它给了我字母表!我不知道该怎么改变它Java 如何重新打印从第一个字符到最后一个字符的字符串?,java,Java,给定一个非空字符串str(如“Code”)打印一个字符串(如“CCoCodCode”)。其中,在字符串中的每个索引处,您必须将字符串重新打印到该索引 我知道我写的这个代码肯定有问题,因为答案应该是CCoCodCode,但它给了我字母表!我不知道该怎么改变它 public static void main(String[] args) { Scanner scan = new Scanner(System.in); String str = scan.next();
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String str = scan.next();
int x = str.length();
for(char i = str.charAt(0); i <= str.charAt(x-1); i++)
{
System.out.print(i);
}
}
publicstaticvoidmain(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
String str=scan.next();
int x=str.length();
对于(char i=str.charAt(0);i可以将char
数据类型视为一个数字;您可以将其递增并将其作为一个数字进行操作
您真正想要的是打印str
的连续子字符串。在表示要打印的子字符串的结束位置的int
上循环
for (int i = 0; i < str.length(); i++)
{
System.out.print(str.substring(0, i + 1));
}
for(int i=0;i
end index参数,这就是我添加1
的原因char
数据类型可以被视为一个数字;您可以将其递增,并将其作为一个数字进行操作
您真正想要的是打印str
的连续子字符串。在表示要打印的子字符串的结束位置的int
上循环
for (int i = 0; i < str.length(); i++)
{
System.out.print(str.substring(0, i + 1));
}
for(int i=0;i
end index参数,这就是我添加1
的原因,假设str
是“code”
。我们可以执行一些心理替换,以查看循环发生了什么
str
是“code”
x
是4
str.charAt(0)
是'C'
str.charAt(x-1)
是'e'
进行这些替换时,您的循环是:
for(char i = 'C'; i <= 'e'; i++)
{
System.out.print(i);
}
for(char i='C';i让我们假设str
是“code”
。我们可以执行一些心理替换来查看循环发生了什么
str
是“code”
x
是4
str.charAt(0)
是'C'
str.charAt(x-1)
是'e'
进行这些替换时,您的循环是:
for(char i = 'C'; i <= 'e'; i++)
{
System.out.print(i);
}
for(char i='C';i它正在输出字母表,因为您正在打印计数器而不是字符串中的字符
实际上,for循环的第一次迭代将i设置为第一个字符,打印该字符,然后操作i++
将i
增加一个。等等,如果第一个字符是“C”,那么i='C'
,什么是i++
事实证明,字符可以用数字来表示。例如,“C”的值为67。因此,将其递增为68,表示“D”。因此,如果在“Code”上运行循环,它将使计数器递增4倍,即“CDEF”。如果在“Codecodecode”上运行,则循环将运行12次,即“CDEFGHIJKLMN”
您真正想要的是通过字符串的索引来循环:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.next();
int length = str.length();
for (int i = 0; i < length; i++) {
System.out.print(str.substring(0, i + 1));
}
}
publicstaticvoidmain(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
String str=scan.next();
int length=str.length();
for(int i=0;i
它正在输出字母表,因为您正在打印计数器而不是字符串中的字符
实际上,for循环的第一次迭代将i设置为第一个字符,打印该字符,然后操作i++
将i
增加一个。等等,如果第一个字符是“C”,那么i='C'
,什么是i++
事实证明,字符可以用数字来表示。例如,“C”的值为67。因此,将其递增为68,表示“D”。因此,如果在“Code”上运行循环,它将使计数器递增4倍,即“CDEF”。如果在“Codecodecode”上运行,则循环将运行12次,即“CDEFGHIJKLMN”
您真正想要的是通过字符串的索引来循环:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.next();
int length = str.length();
for (int i = 0; i < length; i++) {
System.out.print(str.substring(0, i + 1));
}
}
publicstaticvoidmain(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
String str=scan.next();
int length=str.length();
for(int i=0;i
有很多方法可以完成,假设我们将用户的输入存储在一个名为“c”的字符串中……然后
String c = "Code";
for (int i = 0; i < c.length(); i++) {
System.out.print(c.substring(0, i));
}
System.out.print(c);
String c=“code”;
对于(int i=0;i
这将打印您要查找的序列。很多方法都可以完成,假设我们将用户的输入存储在一个名为“c”的字符串中……然后
String c = "Code";
for (int i = 0; i < c.length(); i++) {
System.out.print(c.substring(0, i));
}
System.out.print(c);
String c=“code”;
对于(int i=0;i
这会打印出你想要的序列。非常感谢!你知道我如何在这些东西上多练习吗?我一直在把问题复杂化,而事实上它非常简单。非常感谢!你知道我如何在这些东西上多练习吗?我一直在把问题复杂化,而事实上它非常简单。哦!我的意思是反!哦s!我是说柜台!