我要检查用户第一次输入的第一个字母是否类似于第二次用户输入的第一个字母

问题描述 投票:0回答:1

程序将提示用户输入他的名字,然后输入其他名字然后它将检查这些不同名称的首字母是否与其名字的首字母相似

我尝试过

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

java netbeans-7
1个回答
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");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.