Java 为什么我的代码不计算字符串的第一个字
我试着把单词的长度放在一个数组中。但这不算我的第一个字。 这是我的密码:Java 为什么我的代码不计算字符串的第一个字,java,arrays,Java,Arrays,我试着把单词的长度放在一个数组中。但这不算我的第一个字。 这是我的密码: public class wordLength { public static void main(String[] args){ String s="please come in"; String collection=""; String [] wordslength=new String[10]; for(int k=0;k<s.lengt
public class wordLength {
public static void main(String[] args){
String s="please come in";
String collection="";
String [] wordslength=new String[10];
for(int k=0;k<s.length();k++){
char c=s.charAt(k);
if(c==' '){
wordslength[collection.length()]=collection;
collection="";
}
collection =collection+c;
}
for(int k=1;k<10;k++){
System.out.println("at index "+ (k-1) +"result is "+wordslength[k]);
}
}
}
公共类字长{
公共静态void main(字符串[]args){
String s=“请进来”;
字符串集合=”;
String[]wordslength=新字符串[10];
对于(int k=0;k变化
for(int k=1;k<10;k++)
{
System.out.println("at index "+ (k-1) +"result is "+wordslength[k]);
}
to
for(int k=0;k<10;k++)
{
System.out.println("at index "+ k +"result is "+wordslength[k]);
}
for(int k=1;k数组索引从0开始,您已从1获取它。请访问第0个索引。将其更改为
for(int k=1;k<10;k++){
System.out.println("at index "+ (k-1) +"result is "+wordslength[k-1]);
}
对于(int k=1;k您可以尝试此选项
public class WordLength {
public static void main(String[] args) {
String s = "please come in";
String collection = "";
String[] wordslength = new String[10];
for (int k = 0; k < s.length(); k++) {
char c = s.charAt(k);
if (c == ' ') {
wordslength[collection.length()] = collection;
collection = "";
}
collection = collection + c;
}
for (int k = 0; k < 10; k++) {
System.out.println("at index " + (k) + " result is " + wordslength[k]);
}
}
公共类字长{
公共静态void main(字符串[]args){
String s=“请进来”;
字符串集合=”;
String[]wordslength=新字符串[10];
对于(int k=0;k
}
也要记住这是好的{位于您创建的正文的前一行。类使用大写字母。这两个是java世界中更好的约定,以便将来更好地阅读代码。internet上几乎所有IDE的格式都有许多样式规则。首先,您的代码必须更准确,我想您应该这样做使用
导入java.util.ArrayList
public class wordLength {
public static void main(String[] args)
{
String s="please come in";
String collection="";
ArrayList<String> wordslength=new ArrayList<String>();
for(int k=0;k<s.length();k++)
{
char c=s.charAt(k);
if(c==' ')
{
wordslength.add( collection );
collection="";
}
collection = collection + c;
}
if( collection != " ")
{
wordslength.add( collection );
collection="";
}
for( int k = 0 ; k < wordslength.size() ; k++ )
{
System.out.println( "at index " + (k) + " result is " + wordslength.get(k) + " with length " +wordslength.get(k).length() );
}
}
}
公共类字长{
公共静态void main(字符串[]args)
{
String s=“请进来”;
字符串集合=”;
ArrayList wordslength=新的ArrayList();
对于(int k=0;kfor(int k=1;kYour循环仅运行9次(包括1到9次)!“at index”+(k-1)+”结果是
给出-1
然后在打印中Oochangek-1
到k
,我更改了它,但没有更改,只是更改了索引。
public class wordLength {
public static void main(String[] args)
{
String s="please come in";
String collection="";
ArrayList<String> wordslength=new ArrayList<String>();
for(int k=0;k<s.length();k++)
{
char c=s.charAt(k);
if(c==' ')
{
wordslength.add( collection );
collection="";
}
collection = collection + c;
}
if( collection != " ")
{
wordslength.add( collection );
collection="";
}
for( int k = 0 ; k < wordslength.size() ; k++ )
{
System.out.println( "at index " + (k) + " result is " + wordslength.get(k) + " with length " +wordslength.get(k).length() );
}
}
}