向java程序中添加函数
我正在写我的第一个java程序,我终于把它全部写出来了,但是在作业中我需要添加一个函数而不是main。我的程序计算名字中字母的数量,所以我想也许我可以添加一个函数来读取大写字母的数量?有什么想法吗向java程序中添加函数,java,function,uppercase,Java,Function,Uppercase,我正在写我的第一个java程序,我终于把它全部写出来了,但是在作业中我需要添加一个函数而不是main。我的程序计算名字中字母的数量,所以我想也许我可以添加一个函数来读取大写字母的数量?有什么想法吗 public class NameLetters{ public static void main(String []args){ Scanner in = new Scanner(System.in); String firstName; //A
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!");
}
}
是的,请阅读教程,例如使用。这不是单独的函数,它只适用于我们的ascii字符串
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");