Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 太长了_Java_Eclipse - Fatal编程技术网

Java 太长了

Java 太长了,java,eclipse,Java,Eclipse,这是我解决代码力中单词过长问题的方法。 尽管我得到了正确的输出,但它仍然被报告为codeforces对一个问题的错误答案 问题的链接 import java.util.Scanner; public class Main { public static void main(String[] args) { //charAt() is an inbuilt method which can read a string letter by letter

这是我解决代码力中单词过长问题的方法。 尽管我得到了正确的输出,但它仍然被报告为codeforces对一个问题的错误答案

问题的链接

import java.util.Scanner;


public class Main {

    public static void main(String[] args) {

        //charAt() is an inbuilt method which can read a string letter by letter
        Scanner input=new Scanner(System.in);
        int n;
        //System.out.println("how many words do you wish to enter?");
        n=input.nextInt();
        String[] words=new String[n+1];
        for(int i=0;i<n+1;i++)
        {
            words[i]=input.nextLine();
        }

        for(int j=0;j<n+1;j++)
        {
            if(words[j].length()<10)
            {
                System.out.print(words[j]);
            }

            else
            {
                System.out.print(words[j].charAt(0));
                System.out.print(words[j].length()-2);
                System.out.print(words[j].charAt(words[j].length()-1));
            }

            System.out.print("\n");
        }

    }

}

问题在于这种情况,您只跳过长度小于10的单词,而不考虑长度精确为10的单词

 if(words[j].length()<=10)
 {
        System.out.print(words[j]);
 }

改变它应该工作的条件

在int j的for循环中执行以下操作-

if(words[j].length()<=10) //boundary check
        {
            System.out.print(words[j]);
        }

        else
        {
            System.out.println(words[j].charAt(0).toString() + words[j].length()-2 + words[j].charAt(words[j].length()-1).toString());
        }

以下是我在Python 3中的答案


你的问题是什么?为了更好地理解StackOverflow存在的原因,请不要只发布代码作为答案,还要解释代码的作用以及如何解决问题。带有解释的答案通常更有帮助,质量更好,更容易吸引选票。
Because where r you entering string in your program. Once you run, you will get to know.
Btw this is the solution of actual problem.
 public static void main(String[] args){
String str=null;
        int count=0;
        Scanner scanner= new Scanner(System.in);
        System.out.println("enter string :");
        str=scanner.nextLine();
        for(int i=0; i<str.length(); i++) {
            count ++;
        }
        char first=str.charAt(0);
        char last=str.charAt(count-1);
        int num=count-2;
        System.out.println("abbreviation is :" +first+num+last); 
}
#include<iostream>
#include<string>
#include<sstream> 
using namespace std;

string change_abb(string str)
{
    string result;
  if(str.length()>10)
  {
      int k=str.length()-2;
      stringstream ss;  
      ss<<k;  
      string s;  
      ss>>s;
    result+=str[0];
    result+=s;
    result+=str[str.length()-1];
  }
  else
  {
      result=str;
  }
  return result;
}

int main()
{
  string in_str;
  int n;
  cin>>n;
  for(int i=0;i<n;i++)
  {
    cin>>in_str;
    cout<<change_abb(in_str)<<endl;
  }
}
n = int(input())
p=""
for i in range(n):
    s = input()
    if len(s)>10:
        p = s[0]+str((len(s)-2))+s[len(s)-1]
    else:
        p=s
    print(p)