java 用户输入校验回文并打印出计数

java user input check palindrome and print out counting

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

2 个问题: 首先,我想打印整行用户输入。 System.out.println("There are " + 总计 + " words in " + banana, sneeze, radar, roof, kayak, mine, racer, racecar, refer, james, joyce); 其次,我无法打印最后一行。请多多指教,谢谢

代码:

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.

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