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

时间:2018-04-30 20:34:10

标签: java palindrome

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

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 个答案:

答案 0 :(得分:0)

对于问题#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.

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