Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java拆分后字符串的最大值_Java_Sorting_Split_Max - Fatal编程技术网

java拆分后字符串的最大值

java拆分后字符串的最大值,java,sorting,split,max,Java,Sorting,Split,Max,我正在学习java初学者课程,我有这样的任务: 用户输入单词+数字,例如,“动物”+“年龄”: 马:3匹 狗:5只 鹦鹉:2只 类别:7 我必须制作程序打印出最老动物的年龄。 例如: 最老的动物的年龄是:7岁 现在,这就是我到目前为止写的。我的问题是我不知道如何让程序比较数字 while (true) { String luettu = x.nextLine(); // user inputs animals and numbers if (data

我正在学习java初学者课程,我有这样的任务:

用户输入单词+数字,例如,“动物”+“年龄”:

马:3匹
狗:5只
鹦鹉:2只
类别:7

我必须制作程序打印出最老动物的年龄。 例如:

最老的动物的年龄是:7岁

现在,这就是我到目前为止写的。我的问题是我不知道如何让程序比较数字

    while (true) {

        String luettu = x.nextLine();   // user inputs animals and numbers
        if (dataIn.equals("")) {        // when inputs nothing program stops
            break;
    }
        // Here I separate the animal and number with star symbol
        String[] separatedData = dataIn.split(":"); 

        // From now on, I'm supposed to focus on the numbers, create the rule for how to find the highest value

        x = x Integer.valueOf(separatedData[1]);  // Must target the values in the index 1 (the numbers)  but how?
    }
    int maxNumber = separatedData.lenght; 
    int i = 0;

    // I don't know how to loop and compare the numbers... and I thin "while" doesn't make sense here
    while ( i < size of separatedData) {
        if(maxNumber < separatedData) {
            maxNumber = separatedData;
        }
    System.out.println("The age of the oldest animal is: " + maxNumber); // printing the result
while(true){
字符串luettu=x.nextLine();//用户输入动物和数字
如果(dataIn.equals(“”){//when输入,则程序停止
打破
}
//在这里,我用星号分隔动物和数字
字符串[]separatedData=dataIn.split(“:”);
//从现在开始,我应该把重点放在数字上,为如何找到最高值创建规则
x=x Integer.valueOf(separatedData[1]);//必须以索引1(数字)中的值为目标,但如何实现?
}
int maxNumber=separatedData.lenght;
int i=0;
//我不知道如何循环和比较数字。。。我觉得“while”在这里没有意义
而(i<分离数据的大小){
如果(最大值<分隔数据){
maxNumber=分离数据;
}
System.out.println(“最老动物的年龄为:“+maxNumber”);//打印结果
所以一半的代码都是乱七八糟的,我被卡住了。
请帮助我,如果你能给出解释,那就太好了,谢谢!:)

当找到某事物的最大值时,将最大值设置为可能的最小值。这将是

maxAge = Integer.MIN_VALUE
然后像这样设置一个字符串变量

String oldestAnimal = "";
现在,当您循环遍历这些值时,将
当前年龄
最大年龄
进行比较。如果年龄更大,则设置
max to age
并设置与该年龄相关的动物的
oldestanmal

比较结果如下:

if (age > max) {
   // set appropriate values.
}

完成后,您将得到结果。

即使OP的要求没有提到打印动物的名称,这个答案也很好。+1