2012-07-09 42 views
1

我怎樣才能讓一些數字合理?我不明白。我對Java真的很陌生。 : -/ 例如,無論何時我試圖使這段代碼合理,它給我錯誤。 下面是一些示例代碼,我發現:如何讓數字正確調整?

import java.util.Scanner; 

public class JFindAlphabete 
{ 
    static Scanner sc = new Scanner(System.in              ); 

public static void main(String[] Theory) 
{ 

    JWaffles MyWaffles = new JWaffles(); 

    MyWaffles.ProgramHeading(); 

    System.out.println("Enter a string:"                     ); 
    String SentenceContents = sc.nextLine(                     ); 

    int SpaceCount  = SentenceContents.length() - SentenceContents.replaceAll(" ", "").length(      ); 
    int VowelCount  = SentenceContents.length() - SentenceContents.replaceAll("(?i)[aeiou]", "").length(    ); 
    int ConsonantCount = SentenceContents.length() - SentenceContents.replaceAll("(?i)(?=[a-z])[^aeiou]", "").length( ); 
    int SpecialCharCount = SentenceContents.length() - SentenceContents.replaceAll("(?i)[^a-z ]", "").length(    ); 
    int WordCount  = SentenceContents.trim().split("\\s+").length; 




    System.out.println("There are " + VowelCount + " vowels in this sentance"     ); 
    System.out.println("There are " + ConsonantCount + " consonants in this sentance"   ); 
    System.out.println("There are " + SpaceCount + " spaces in this sentance"     ); 
    System.out.println("There are " + SpecialCharCount + " special characters in this sentance"); 
    System.out.println("There are " + WordCount + " words in this sentance"     ); 






} 
} 
+0

它「給你錯誤」?也許描述這些錯誤的性質會有所幫助;)另外,你應該能夠正確地使用printf/format。 – 2012-07-09 00:32:22

+0

@fvu意思printf,手機自動更正。 – 2012-07-09 00:36:23

回答

1

一個很好的教程,你可以使用System.outprintf方法,而不是println,並使用格式化選項右鍵調整值,你打印:

System.out.printf("There are %5d vowels in this sentance\n"  , VowelCount); 
System.out.printf("There are %5d consonants in this sentance\n" , ConsonantCount); 

等等。

0

java.util.Formatter看看。

它允許您根據預定義的格式進行打印,包括固定寬度數字(帶有空格填充)。

這可能是最接近你的'正確的'這些數字。