isLIUJIAO呐


私信TA

用户名:uq_56404513577

访问量:1584

签 名:

等  级
排  名 13489
经  验 863
参赛次数 1
文章发表 3
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

TA的其他文章

注意事项:
具本人目前了解,Java语言无法直接输入char型数组,这时需输入String字符串,再用charAt()函数将String型字符串转换为char型数组。

在此题目中,本人出现多次错误,首先时输入字符next()和nextLine()函数的区分,看到此篇初学者可自行尝试,nextLine()函数可接收空格。还有一点区分于C语言,如果把下列代码中创建数组大小改为固定数值,例如20,将会报‘StringIndexOutOfBoundsException’错误(未弄清楚,还请知悉人士解答)。


参考代码:

Scanner sc = new Scanner(System.in);
int n = sc.nextInt();//6
String str = sc.next();//abcdef
int m = sc.nextInt();//3
char ch[] = new char[n];//创建一个数组
for (int i = 0; i < ch.length; i++) {//取出字符串中的字符存入字符数组中
    ch[i] = str.charAt(i);
}
for (int i = m - 1; i < n; i++) {//输出规定范围内的字符
    System.out.print(ch[i]);
}

在翻阅时,还发现一种方式,是直接使用substring()函数:

此方式可用于进阶使用,初学应多尝试,最后用函数较好。

Scanner in = new Scanner(System.in);
int n = in.nextInt();      
String x = in.nextLine();
String a = in.nextLine();
int m = in.nextInt();
if(a.length() == n) {
    System.out.println(a.substring(m-1));
}
 

0.0分

1 人评分

  评论区