简体   繁体   English

计算首尾之间的字母-Java

[英]Counting Letters Between the First and Last - Java

OK, so I'm doing this project that requires that I have the first and last setters of a string appear with the number of letters in between them counted, and output. OK,所以我正在做这个项目,要求我显示一个字符串的第一个和最后一个设置器,并计算和输出它们之间的字母数。 I've tried repurposing some reverse a string code I had handy, but I cannot get the output to appear in my IDE. 我尝试重新使用一些方便的反向字符串代码,但是我无法使输出出现在我的IDE中。

Can anyone look over my code, and make some suggestions? 谁能看看我的代码,并提出一些建议?

public static void main(String[] args) {
    String countWord;
    countWord = JOptionPane.showInputDialog(null,
            "Enter the string you wish to have formatted:");
}

static String countMe(String countWord) {
    int count = 1;
    char first = countWord.charAt (0);
    char last = countWord.charAt(-1);
    StringBuilder word = new StringBuilder();
    for(int i = countWord.length() - 1; i >= 0; --i)
        if (countWord.charAt(i) != first ) {
            if (countWord.charAt(i) != last) {
                count++;
            }
        }
        return countWord + first + count + last;
    }
}

Just build it using charAt() : 只需使用charAt()构建它:

return "" + str.charAt(0) + (str.length() - 2) + str.charAt(str.length() - 1);

The "" at the front causes the numeric values that follow to be concatenated as Strings (instead of added arithmetically). 前面的""使后面的数字值连接为字符串(而不是算术添加)。


A slightly more terse alternative is: 稍微更简洁的方法是:

return countWord.replaceAll("(.).*(.)", "$1" + (str.length() - 2) + "$2")

Once you determined the first and last chars, it is no need for unnecessary conditions. 一旦确定了第一个和最后一个字符,就不需要不必要的条件。 Just try this: 尝试一下:

static String countMe(String countWord) {

char first = countWord.charAt(0);
char last = countWord.charAt(countWord.length()-1);

int count=0;

for (int i = 1; i < countWord.length()-1; i++)
    {
        count++;
    }
    return first + String.valueOf(count) + last;
    }

Or, if it is not mandatory to use for loop, you can make it simple as this 或者,如果不是强制使用for循环,则可以使它变得简单

static String countMe(String countWord) {
char first = countWord.charAt(0);
char last = countWord.charAt(countWord.length()-1);

int count = countWord.substring(1, countWord.length()-1).length();

return first + String.valueOf(count) + last;
}

You could use the string.length() method to obtain the total length of the string. 您可以使用string.length()方法获取字符串的总长度。 Your code would be something like: 您的代码将类似于:

int totalLength = countWord.length();
int betweenLength = totalLength - 2; // This gives the count of characters between first and last letters
char first = countWord.charAt(0);
char last = countWord.charAt(str.length() - 1);
String answer = first + betweenLength + last;
import javax.swing.JOptionPane;

public class Main{

    public static void main(String[] args) {
        String countWord;
        countWord = JOptionPane.showInputDialog(null,
                "Enter the word you wish to have formatted:");
        JOptionPane.showMessageDialog(null, countMe(countWord));
    }

    static String countMe(String countWord) {
        int count = 0;
        String first = String.valueOf(countWord.charAt(0));
        String last = String.valueOf(countWord.charAt(countWord.length() - 1));
        for(int i = 1; i < countWord.length() - 1; i++) {
            if (String.valueOf(countWord.charAt(i)) != first ) {
                count++;
            }
        }
        return first + count + last;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM