gpt4 book ai didi

java - 运行时异常 - 功能尚未实现

转载 作者:太空宇宙 更新时间:2023-11-04 12:05:07 25 4
gpt4 key购买 nike

下面的代码块在 jelliot 中显示此错误“运行时异常 - 功能尚未实现”,当它到达该行时

char arr [] = w.toCharArray();

在其他编译器中,它不会接受它应该接受的输入数量。如果我设置 n=4,则只需要 2 个输入。

    Scanner Sc = new Scanner (System.in);
int n = Sc.nextInt();
for(int count = 1;count<=n; count++){
String w = Sc.nextLine();
char arr [] = w.toCharArray();
if(arr.length > 4){
System.out.print(arr[0]);
System.out.print(arr.length-2);
System.out.print(arr[arr.length-1]);
}
else{
System.out.println(w);
}
System.out.println();
}

最佳答案

该异常表明 String 类的此特定方法 (toCharArray()) 未针对您正在使用的 java 实现(可能是非标准)实现。您可以通过不使用 char 数组,而是使用 String 方法 length()charAt() 来解决此问题。试试这个:

Scanner Sc = new Scanner (System.in);
int n = Sc.nextInt();
for(int count = 1;count<=n; count++){
String w = Sc.nextLine();
if(w.length() > 4){
System.out.print(w.charAt(0));
System.out.print(w.length()-2);
System.out.print(w.chatAt(w.length()-1));
}
else{
System.out.println(w);
}
System.out.println();
}

关于java - 运行时异常 - 功能尚未实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40452775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com