程序将提示用户输入姓名,然后输入其他名称 然后它将检查这些不同名称的首字母是否与其名字的首字母相似
我尝试过
Scanner input = new Scanner(System.in)
String MyName;
MyName = input.nextLine();
String FreindName;
FreindName = input.nextLine();
int count
char let1 = FreindName.CharAt(0);
char let2 = MyName.CharAt(0);
if (let1 == let2){
count++;}
问题是处理程序后,计数仍为0
答案 0 :(得分:0)
您可以执行以下操作:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
String myName = input.nextLine();
System.out.print("Number of friends whose names you want to enter: ");
int n = input.nextInt();
int count = 0;
String friendName;
for (int i = 1; i <= n; i++) {
System.out.printf("Enter the name of friend %d: ", i);
input = new Scanner(System.in);
friendName = input.nextLine();
if (myName.length() > 0 && friendName.length() > 0)
if (myName.charAt(0) == friendName.charAt(0))
count++;
}
System.out.println(count + " friends have first letter in their names same as yours");
}
}
示例运行:
Enter your name: Arvind
Number of friends whose names you want to enter: 3
Enter the name of friend 1: Anu
Enter the name of friend 2: Ravi
Enter the name of friend 3: Avinash
2 friends have first letter in their names same as yours
更新:为了满足您在评论中提到的要求,我发布了以下更新
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
String myName = input.nextLine();
int count = 0;
int matchCount=0;
String friendName;
do {
System.out.print("Type friend's name (Type stop to quit): ");
input = new Scanner(System.in);
friendName = input.nextLine();
if (myName.length() > 0 && friendName.length() > 0) {
if (myName.charAt(0) == friendName.charAt(0)) {
matchCount++;
}
}
count++;
}while(!"stop".equalsIgnoreCase(friendName));
count--;
System.out.println("Out of "+count+" friends, "+matchCount + " friends have first letter in their names same as yours");
System.out.println("Out of "+count+" friends, "+String.format("%1.2f",((double)matchCount/count)*100) + "% friends have first letter in their names same as yours");
}
}
示例运行:
Enter your name: Arvind
Type friend's name (Type stop to quit): Kumar
Type friend's name (Type stop to quit): Anuj
Type friend's name (Type stop to quit): Vikash
Type friend's name (Type stop to quit): Vaibhav
Type friend's name (Type stop to quit): Avinash
Type friend's name (Type stop to quit): stop
Out of 5 friends, 2 friends have first letter in their names same as yours
Out of 5 friends, 40.00% friends have first letter in their names same as yours
注意:您可以使用do/while
循环来代替使用while
循环,如下所示:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
String myName = input.nextLine();
int count = 0;
int matchCount=0;
String friendName="";
while(!"stop".equalsIgnoreCase(friendName)) {
System.out.print("Type friend's name (Type stop to quit): ");
input = new Scanner(System.in);
friendName = input.nextLine();
if (myName.length() > 0 && friendName.length() > 0) {
if (myName.charAt(0) == friendName.charAt(0)) {
matchCount++;
}
}
count++;
};
count--;
System.out.println("Out of "+count+" friends, "+matchCount + " friends have first letter in their names same as yours");
System.out.println("Out of "+count+" friends, "+String.format("%1.2f",((double)matchCount/count)*100) + "% friends have first letter in their names same as yours");
}
}