How to output an error message to the user and repeat to allow them to enter the word again if the user enters more than one word

huangapple go评论117阅读模式
英文:

How to output an error message to the user and repeat to allow them to enter the word again if the user enters more than one word

问题

请帮忙!
如果用户输入多于一个词,向用户输出错误信息,并重复允许其重新输入单词。

这是我现在的代码:

public void computeLetters(){
    for(int i = 0; i < term.length()-1; i++){
        if(term.charAt(i)==' ' && term.charAt(i+1)!=' ')
            numberWords = 1;
    }

    for(int numberWords = 1; numberWords > 0;){
        JOptionPane.showMessageDialog(null,"请再试一次");
        JOptionPane.showInputDialog(null,"输入一个词");
        numberWords = 0;
    }

    for(int numberWords = 0; numberWords < 1;){
        for(int j = 0; j < term.length()-1; j++){
            numberLetters++;
        }
    }
}
英文:

Please Help!
If the user enters more than one word, output an error message to the user and repeat to allow them to enter the word again.

This is the code I have:

public void computeLetters(){
	for(int i = 0; i &lt; term.length()-1; i++){
		if(term.charAt(i)==&#39; &#39; &amp;&amp; term.charAt(i+1)!=&#39; &#39;)
		numberWords = 1;
	}

	for(int numberWords = 1; numberWords &gt; 0;){
		JOptionPane.showMessageDialog(null,&quot;Please try again&quot;);
		JOptionPane.showInputDialog(null,&quot;Enter a word&quot;);
		numberWords = 0;
	}

	for(int numberWords = 0; numberWords &lt; 1;){
		for(int j = 0; j &lt; term.length()-1; j++){
						numberLetters++;
		}
	}
}

答案1

得分: 1

public void computeLetters()
{
    int numberLetters = 0;
    while (numberLetters == 0) {
        // Get user input
        String term = JOptionPane.showInputDialog("Enter a word");
        if (null != term) {
            // Check if more than 1 word (has a space)
            for (int i = 0; i < term.length(); i++) {
                if (' ' == term.charAt(i)) {
                    numberLetters = 0;
                    break;
                }
                numberLetters += 1;
            }
        }
        if (numberLetters == 0) {
            JOptionPane.showMessageDialog(null, "Please try again");
        }
    }
    
    // Do something with numberLetters here....
}
英文:

A loop should work...

public void computeLetters()
{
    while (true) {
        // Get user input
        String term = JOptionPane.showInputDialog(&quot;Enter a word&quot;);
        if (null == term) return;
        // Check if more than 1 word (has a space)
        String [] words = term.split(&quot; &quot;);
		// Single word?
		if (words.length == 1) {
			// Get letter count
			int numberLetters = words[0].length();
            // Do something else here???
            // Done
            return;
        }
        // Show error message
        JOptionPane.showMessageDialog(null, &quot;Please try again&quot;);
        // And try again...
    }
}

Edit based on comment using only if statements, loops or chars & strings (and ints).

public void computeLetters()
{
    int numberLetters = 0;
	while (numberLetters == 0) {
		// Get user input
		String term = JOptionPane.showInputDialog(&quot;Enter a word&quot;);
		if (null != term) {
			// Check if more than 1 word (has a space)
			for (int i = 0; i &lt; term.length(); i++) {
				if (&#39; &#39; == term.charAt(i)) {
					numberLetters = 0;
					break;
				}
				numberLetters += 1;
			}
		}
		if (numberLetters == 0) {
			JOptionPane.showMessageDialog(null, &quot;Please try again&quot;);
		}
	}
	
	// Do something with numberLetters here....
}

答案2

得分: 0

以下是翻译好的内容:

如果此方法试图从用户获取单词,请验证它是一个单词,然后计算该单词中字母的数量。以下是我的做法。

public int computeLetters()
{
    String myWord = JOptionPane.showInputDialog("输入一个单词。");
    while(myWord.contains(" "))
    {
        JOptionPane.showMessageDialog("不能超过一个单词。");
        myWord = JOptionPane.showInputDialog("输入一个单词。");
    }
    return myWord.toCharArray().length;
}
英文:

If this method is trying to get a single word from the user, validate it is one word, then count the number of letters in that word. This is how I would do it.

public int computeLetters()
 {
   
   String myWord = JOptionPane.showInputDialog(&quot;enter a single word.&quot;);
   while(myWord.contains(&quot; &quot;))
   {
    JOptionPane.showMessageDialog(&quot;Cannot be more than one word.&quot;);
    myWord = JOptionPane.showInputDialog(&quot;enter a single word.&quot;);
   }
  return myWord.toCharArray().length;
}

答案3

得分: 0

public void computeLetters(){
    numberWords = term.trim().split("\\s+").length;

    if(numberWords > 0 ){
        JOptionPane.showMessageDialog(null,"请重试");
        term = JOptionPane.showInputDialog(null,"输入一个单词");
        computeLetters();
    }
}

// Number of words check, changed to a single line of code.

// Next, the for loop in your code can be replaced by a simple 'if' as shown.

// Then the next loop would have been an infinite loop. Just calling the function again after getting value from the user will do!
英文:
public void computeLetters(){
    numberWords = term.trim().split(&quot;\\s+&quot;).length;


    if(numberWords &gt; 0 ){
        JOptionPane.showMessageDialog(null,&quot;Please try again&quot;);
        term = JOptionPane.showInputDialog(null,&quot;Enter a word&quot;);
        computeLetters();
    }

}

Number of words check, changed to a single line of code.

Next, the for loop in your code can be replaced by a simple 'if' as shown.

Then the next loop would have been an infinite loop. Just calling the function again after getting value from the user will do!

huangapple
  • 本文由 发表于 2020年4月4日 01:29:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/61017312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定