Java 用户输入检查回文并打印出计数



>我有一个这样的用户输入: 香蕉, 打喷嚏, 雷达, 屋顶, 皮划艇, 我的, 赛车手, 赛车, 参考, 詹姆斯, 乔伊斯

对此有2个问题: 首先,我想打印整行用户输入。System.out.println("有" + 总计 + " 单词 " + 香蕉, 打喷嚏, 雷达, 屋顶, 皮划艇, 我的, 赛车手, 赛车, 参考, 詹姆斯, 乔伊斯(; 其次,我无法打印最后一行。请教训我,谢谢。

法典:

public static void main(String[] args)  {
    palindromeCheck();                      // test your method
}
// Part 3
/**
 * This program reads words, identifies, counts and writes all the palindromes and the total
 * palindrome count.
 */
public static void palindromeCheck(){
    // you could use any of the words below to test your method:
    // banana, sneeze, radar, roof, kayak, mine, racer, racecar, refer, james, joyce
    String someWord = ""; // Stores words read from user input
    int count = 0;        // keeps track of Palindrome words only (define algorithm to count # of palindrome words
    int total = 0;        // Counts the total number of lines read from the given text file
    System.out.println(" Enter some words separated by white space");    // Ask for user input
    Scanner keyboard = new Scanner(System.in);
    // hint 1: Using keybord.next() will only return what comes before a space.
    // hint 2: Using keybord.nextLine() automatically reads the entire current line.
           // store each word in a string variable and then do your operations
    while (keyboard.hasNext()) {
        String temp = keyboard.next();
        total++;
        int Len = temp.length();
        for(int i=0 ; i < (Len/2) ; i++) {
            //debug: System.out.println("first word "+temp.charAt(i)+"  second word "+temp.charAt(Len-1-i));
            if(temp.charAt(i)== temp.charAt(Len-1-i))count++;
        }
        someWord += temp;
        System.out.println("There are " + total + " words in " + someWord);   // test
    }
    System.out.println("There are "+count +" palindromes out of "+total+" words.");
    keyboard.close();
    // x is a variable for count and y is variable total
    // #2. print “There are x palindromes out of y words”
    System.out.println("There are "+count +" palindromes out of "+total+" words.");

对于问题 #1,将变量someWords更改为List<String>,对于问题 #2,您应该使用 for 循环而不是while循环。您的最后一行从未打印,因为while循环从未退出。

请参阅下面更新的palindromeCheck方法:

法典

/** *该程序读取单词,识别,计数和写入所有回文和总数 *回文计数。 */public static void palindromeCheck(( {

    // you could use any of the words below to test your method:
    // banana, sneeze, radar, roof, kayak, mine, racer, racecar, refer, james, joyce
    List<String> someWords = new ArrayList<>(); // Stores words read from user input
    int count = 0;        // keeps track of Palindrome words only (define algorithm to count # of palindrome words
    int total = 0;        // Counts the total number of lines read from the given text file
    System.out.println(" Enter some words separated by white space");    // Ask for user input
    Scanner keyboard = new Scanner(System.in);
    // hint 1: Using keybord.next() will only return what comes before a space.
    // hint 2: Using keybord.nextLine() automaticatlly reads the enire current line.
    // store each word in a string variable and then do your operations
    for (String word : keyboard.nextLine().split(" ")) {
        int length = word.length();
        for (int i = 0; i < (length / 2); i++) {
            //debug: System.out.println("first word "+temp.charAt(i)+"  second word "+temp.charAt(Len-1-i));
            if (word.charAt(i) == word.charAt(length - 1 - i)) count++;
        }
        someWords.add(word);
        System.out.println("There are " + someWords.size() + " words in " + someWords);   // test
    }
    System.out.println("There are " + count + " palindromes out of " + total + " words.");
    keyboard.close();
    // x is a variable for count and y is variable total
    // #2. print “There are x palindromes out of y words”
    System.out.println("There are " + count + " palindromes out of " + total + " words.");
}

输出

Enter some words separated by white space
banana sneeze radar roof kayak mine racer racecar refer james joyce
There are 1 words in [banana]
There are 2 words in [banana, sneeze]
There are 3 words in [banana, sneeze, radar]
There are 4 words in [banana, sneeze, radar, roof]
There are 5 words in [banana, sneeze, radar, roof, kayak]
There are 6 words in [banana, sneeze, radar, roof, kayak, mine]
There are 7 words in [banana, sneeze, radar, roof, kayak, mine, racer]
There are 8 words in [banana, sneeze, radar, roof, kayak, mine, racer, racecar]
There are 9 words in [banana, sneeze, radar, roof, kayak, mine, racer, racecar, refer]
There are 10 words in [banana, sneeze, radar, roof, kayak, mine, racer, racecar, refer, james]
There are 11 words in [banana, sneeze, radar, roof, kayak, mine, racer, racecar, refer, james, joyce]
There are 12 palindromes out of 0 words.
There are 12 palindromes out of 0 words.

只是要注意,检查单词是否是回文的逻辑存在一些问题。

最新更新