gpt4 book ai didi

java - 我想检查用户第一个输入的首字母是否类似于第二个用户输入的首字母

转载 作者:行者123 更新时间:2023-12-01 19:37:07 26 4
gpt4 key购买 nike

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

我试过了

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

最佳答案

您可以按照以下步骤进行操作:

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");
}
}

关于java - 我想检查用户第一个输入的首字母是否类似于第二个用户输入的首字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59204184/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com