我怎么能算的字符数的字符串值,在阵列内,然后把它比作他人找到最长的一个?

mightymorphinParkRanger:

下面是我现在所拥有的。我只需要知道的主要事情是我怎么能检查一个字符串的字符数,阵列中,然后检查,对所有其他的条目,只拉最大的一个。我处理这样的问题:

编写一个程序,直到进入一个空行从用户读取姓名和出生年份。这个名字和出生年份是用逗号分隔。

此后,程序打印名字最长,平均诞生年。如果有多个名字同样最长的,你可以打印任何人。你可以假设用户进入至少一个人。示例输出

塞巴斯蒂安,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();

    }
}
Arvind的库马尔阿维纳什:

我怎么能算的字符数的字符串值,在阵列内,然后把它比作他人找到最长的一个?

做到这一点,如下所示:

if (checkVal < valArray[0].length()) {
    checkVal = valArray[0].length();
    checkName = valArray[0];
}

请注意,length()是用来寻找的长度String

其他一些重要的点:

  1. 您还需要一个变量来存储所有的出生年的总和,这样就可以计算出它们的平均值。或者,你可以计算出平均运行。
  2. 对他们进行任何操作之前修剪的条目(姓名和出生年份)。
  3. 不要关闭ScannerSystem.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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我怎么能压扁一个List <名单<名单<字符串>>>

Java的访问一个ArrayList把它比作一个字符串参数?

我怎么能保存一个字符串字节而不丢失信息?

我怎么能指定匹配字符串和长到一个类型?

我怎么能确定一个字符串中只包含一个数字吗?

我怎么能转换我的字符串(表示十六进制值)字节?

我怎么能代替值匹配的查询字符串的JSON包装某个键?

我怎么能一个字符串分隔成列,如果我有多个行和字符串都在SQL不同长度的?

我想创建一个可以在字符串中找到空格的循环,然后对字符串进行处理

我怎么能一个函数内的变量的值传递给一个命令?

我怎么能正则表达式匹配最少字符的字符串?

我怎么能猜出已知字符串哈希中缺少的字符?

一个不属于输入语言的字符串怎么能无限循环设置图灵机呢?

我怎么能说我的字符串模式匹配

找到最长的单词之前先分割一个字符串

我怎么能一个字符转换成int类型的Java?

使用正则表达式查看一个数字是否在其他人之前在字符串中?

合并字母,以形成一个字符串,并与现有的字符串比较?我很新到Java,我怎么能写什么逻辑?

我怎么能在 kotlin 中拆分字符串

我怎么能缩进字符串时小写符合大写,像thisSentence?

我怎么能得到任何行的子字符串呢?

我怎么能改变字符串数据格式..?

python列表,需要找到一个字符串,然后对该字符串进行切片,以获取附加的值

我怎么能一个键排序字典中的值?

我怎么能只模拟一个非空值?

获得一个表元素的旧值,我怎么能得到新的价值?

我有一个格式为“12/Sep/21”的日期字符串。我想将格式更改为“2021-09-12”。我怎么能在python中做到这一点?

Python:如何打印字符串中找到的最后一个X字符?我的代码始终无法正常工作是怎么回事?

我怎么能减少基于字符串的Java中的其他名单上我的结构列表?