2016-09-08 50 views
-4

我正在编写我的第一个java程序,我终于得到了它所有的书面,但在赋值我需要添加一个函数(主除外)。我的程序计算了名字中的字母数量,所以我想也许我可以添加一个读取多少字母大写的函数?有任何想法吗?将函数添加到java程序

public class NameLetters{ 

    public static void main(String []args){ 

     Scanner in = new Scanner(System.in); 

     String firstName; //Asks for the users first name 

     int count=0;  //Count the letters passing through loop 

     System.out.print("Hello, this program will ask for your first name and then output the number of letters in your name."); 

     System.out.print("Please enter your first name:"); 

     String firstName = input.NextLine(); 

     for (int i=0; i<firstName.length(); i++) { 
      if (firstName.charAt(i) != ' ') 
       Count ++; 
     } 

     system.out.print("There are "+Count+" s in the first name "+firstName+" , Thank you for participating. Goodbye!"); 
    } 
} 
+0

是,读的教程,例如http://www.learnjavaonline.org/en/Functions –

+0

使用['Character.isUpperCase()'](https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html #isUpperCase-煤焦)。 – Boann

回答

-1
  String s = "NaMe"; 
      int upper = 0; 
      int lower = 0; 
      for(int i = 0; i<s.length();i++) { 
       int charASCII= (int)s.charAt(i); 
       if (charASCII <91 && charASCII > 64){ 
        upper ++; 
       } 
       if (charASCII <123 && charASCII > 96){ 
        lower ++; 
       } 
      } 
      System.out.print("Given name string contains "+upper+ " uppercase letters & "+lower + " lowercase letters"); 
+0

这不是独立的功能,它只适用于US-ASCII字符串 –