英文:
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 < term.length()-1; i++){
if(term.charAt(i)==' ' && term.charAt(i+1)!=' ')
numberWords = 1;
}
for(int numberWords = 1; numberWords > 0;){
JOptionPane.showMessageDialog(null,"Please try again");
JOptionPane.showInputDialog(null,"Enter a word");
numberWords = 0;
}
for(int numberWords = 0; numberWords < 1;){
for(int j = 0; j < 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("Enter a word");
if (null == term) return;
// Check if more than 1 word (has a space)
String [] words = term.split(" ");
// 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, "Please try again");
// 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("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....
}
答案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("enter a single word.");
while(myWord.contains(" "))
{
JOptionPane.showMessageDialog("Cannot be more than one word.");
myWord = JOptionPane.showInputDialog("enter a single word.");
}
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("\\s+").length;
if(numberWords > 0 ){
JOptionPane.showMessageDialog(null,"Please try again");
term = JOptionPane.showInputDialog(null,"Enter a word");
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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论