java - 如何计算数组中字符串值中的字符数,然后将其与其他字符进行比较以找到最长的字符?
问题描述
以下是我现在所拥有的。我只需要知道的主要事情是如何检查数组中字符串的字符数,然后将其与所有其他条目进行检查并仅提取最大的条目。我正在处理的问题是:
编写一个程序,从用户那里读取姓名和出生年份,直到输入一个空行。姓名和出生年份用逗号隔开。
之后,程序会打印最长的姓名和出生年份的平均值。如果多个名称的长度相同,则可以打印其中任何一个。您可以假设用户至少输入了一个人。样本输出
塞巴斯蒂安,2017 卢卡斯,2017 莉莉,2017 汉娜,2014 加布里埃尔,2009
最长的名字:塞巴斯蒂安平均出生年数:2014.8
import java.util.Scanner;
public class Test112 {
//Asks the user for input and will print out who is the oldest
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int checkVal = 0;
String checkName = null;
System.out.println("Provide a name and age in this format: name,#");
while (true) {
//The unique thing about this code, is each input is a separate array that is not put in memory
//You just use the array system to pull data of the values entered at that specific time
String userIn = scanner.nextLine();
if (userIn.equals("")) {
break;
}
//Here we get the info from the user, then split it
String[] valArray = userIn.split(",");
//This now uses the variable to check which value of arguments entered is the greatest
//It'll then assign that value to the variable we created
**if (checkVal < Integer.valueOf((int)valArray[0].length) {**
//We need the checkVal variable to be a constant comparison against new entries
checkVal = Integer.valueOf(valArray[1]);
//This pulls the name of the value that satisfies the argument above
checkName = valArray[0];
}
}
System.out.println("Name of the oldest: " + checkName);
scanner.close();
}
}
解决方案
如何计算数组中字符串值中的字符数,然后将其与其他字符进行比较以找到最长的字符?
执行以下操作:
if (checkVal < valArray[0].length()) {
checkVal = valArray[0].length();
checkName = valArray[0];
}
请注意,length()
用于查找 a 的长度String
。
其他一些重要的点:
- 您还需要一个变量来存储所有出生年份的总和,以便计算它们的平均值。或者,您可以计算运行平均值。
- 在对条目执行任何操作之前修剪条目(姓名和出生年份)。
- 不要关闭
Scanner
forSystem.in
。
下面给出了包含注释的完整程序:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int checkVal = 0;
String checkName = "", name, birthYearStr;
int sum = 0, count = 0;
while (true) {
System.out.print("Provide a name and age in this format: name,#");
String userIn = scanner.nextLine();
if (userIn.equals("")) {
break;
}
String[] valArray = userIn.split(",");
name = valArray[0].trim();
birthYearStr = valArray[1].trim();
if (valArray.length == 2 && birthYearStr.length() == 4) { // Birth year should be of length 4
try {
sum += Integer.parseInt(birthYearStr);
if (checkVal < name.length()) {
checkVal = name.length();
checkName = name;
}
count++;
} catch (NumberFormatException e) {
System.out.println("Birth year should be an integer. Please try again.");
}
} else {
System.out.println("This is an invalid entry. Please try again.");
}
}
System.out.println("Longest name: " + checkName);
System.out.println("Average of birth years: " + (sum / count));
}
}
示例运行:
Provide a name and age in this format: name,#sebastian,2017
Provide a name and age in this format: name,#lucas,2017
Provide a name and age in this format: name,#lily,2017
Provide a name and age in this format: name,#hanna,2014
Provide a name and age in this format: name,#gabriel,2009
Provide a name and age in this format: name,#
Longest name: sebastian
Average of birth years: 2014
另一个示例运行:
Provide a name and age in this format: name,#sebastian,201
This is an invalid entry. Please try again.
Provide a name and age in this format: name,#sebastian,hello
This is an invalid entry. Please try again.
Provide a name and age in this format: name,#sebastian,2017
Provide a name and age in this format: name,#lucas,2017
Provide a name and age in this format: name,#lily,2017
Provide a name and age in this format: name,#hanna,2014
Provide a name and age in this format: name,#gabriel,2009
Provide a name and age in this format: name,#
Longest name: sebastian
Average of birth years: 2014
另一个示例运行:
Provide a name and age in this format: name,#hello,2018,sebastian,2017
This is an invalid entry. Please try again.
Provide a name and age in this format: name,#hello,2018
Provide a name and age in this format: name,#sebastian,2017
Provide a name and age in this format: name,#
Longest name: sebastian
Average of birth years: 2017
另一个示例运行:
Provide a name and age in this format: name,#sebastian,rama
Birth year should be an integer. Please try again.
Provide a name and age in this format: name,#sebastian,12.5
Birth year should be an integer. Please try again.
Provide a name and age in this format: name,#sebastian,2017
Provide a name and age in this format: name,#abcdefghi,2018
Provide a name and age in this format: name,#rama,2009
Provide a name and age in this format: name,#
Longest name: sebastian
Average of birth years: 2014
另一个示例运行:
Provide a name and age in this format: name,#sebastian,2017
Provide a name and age in this format: name,#lucas,2017
Provide a name and age in this format: name,#lily , 2017
Provide a name and age in this format: name,#
Longest name: sebastian
Average of birth years: 2017
推荐阅读
- ios - iOS 是否支持 react 原生库
- java - 标准 Java 项目的 log4j2 配置
- php - http://example.com/fruits/apple 我想重定向到 http://example.com/pen/apple
- javascript - 当用 jest 测试一个反应组件时,一个导入的方法是未定义的
- django - DRF 中的查找字段使其不区分大小写
- zend-framework - 如何解决zendframework中的错误
- influxdb - influxdb 按值查询分组
- javascript - 在通过 AJAX 动态添加的元素上第一次未触发点击事件
- regex - 如何在scala中为值为1-9的字符串编写精炼的类型正则表达式
- r - 无法加载 ggtern 包