Java 从循环中查找最大数

Java 从循环中查找最大数,java,Java,我试图从一个循环中找到最大数量,它是出现的次数。这是我到目前为止编写的代码 public class MaxNumbers { public static void main(String[] args) { Scanner input = new Scanner(System.in); int max = 0; int number = 0; int count_max = 0; while (true)

我试图从一个循环中找到最大数量,它是出现的次数。这是我到目前为止编写的代码

public class MaxNumbers {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int max = 0;
        int number = 0;
        int count_max = 0;
        while (true) {
             number = input.nextInt();
            if (number == 0) {
                break;
            }
            number = max;
            if (max > number) {
                max=number;
                count_max++;
            }

        }
        System.out.println("Max Number: " + number);
        System.out.println("Occurences: " + count_max);
    } 

我在输出中只得到零,是否有任何逻辑错误?

删除行
number=max
删除行
number=max
是的,有行:

number = max

if (max > number) {

max永远不会大于number,因为您只需在前一行中将它们设置为相等

是的,有一行:

number = max

if (max > number) {

max永远不会大于number,因为您只是在前一行中将它们设置为相等,代码中有两个问题。这应该适合您:

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int max = Integer.MIN_VALUE;
        int number = 0;
        int count_max = 0;
        while (true) {
            number = input.nextInt();
            if (number == 0) {
                break;
            }
            if(max == number){
                count_max++;
            }
            if (max < number) {
                max=number;
                count_max = 1;
            }
        }
        System.out.println("Max Number: " + max);
        System.out.println("Occurences: " + count_max);
    }
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int max=整数的最小值;
整数=0;
int count_max=0;
while(true){
number=input.nextInt();
如果(数字==0){
打破
}
如果(最大==数量){
计数_max++;
}
如果(最大值<数量){
最大值=数量;
计数_max=1;
}
}
System.out.println(“最大编号:+Max”);
System.out.println(“发生次数:+count\u max”);
}

代码中有几个问题。这应该适合您:

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int max = Integer.MIN_VALUE;
        int number = 0;
        int count_max = 0;
        while (true) {
            number = input.nextInt();
            if (number == 0) {
                break;
            }
            if(max == number){
                count_max++;
            }
            if (max < number) {
                max=number;
                count_max = 1;
            }
        }
        System.out.println("Max Number: " + max);
        System.out.println("Occurences: " + count_max);
    }
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int max=整数的最小值;
整数=0;
int count_max=0;
while(true){
number=input.nextInt();
如果(数字==0){
打破
}
如果(最大==数量){
计数_max++;
}
如果(最大值<数量){
最大值=数量;
计数_max=1;
}
}
System.out.println(“最大编号:+Max”);
System.out.println(“发生次数:+count\u max”);
}

我认为您的“number=max”行需要修改。这里的代码是每次通过循环都将number的值设置为max。由于第一次通过循环时它的值为0,因此它将无限期地保持为0。这意味着你的最大值永远不会改变

所以你不想要那句台词,但你确实想要类似的东西。如果该数字的值与(=)max相同,则需要将1添加到count_max


最后一个If块也需要稍微调整。如果您找到了一个新的最高值(即,数字>最大值,而不是相反),那么您需要将您的最大计数重置为1,而不是添加1。

我认为您的“数字=最大值”行需要修改。这里的代码是每次通过循环都将number的值设置为max。由于第一次通过循环时它的值为0,因此它将无限期地保持为0。这意味着你的最大值永远不会改变

所以你不想要那句台词,但你确实想要类似的东西。如果该数字的值与(=)max相同,则需要将1添加到count_max


最后一个If块也需要稍微调整。如果你找到了一个新的最高值(即,数字>最大值,而不是相反),那么你需要将你的最大计数重置为1,而不是加上1。

这是我的版本,我认为你正在努力实现,它基本上是有效的,请注意,如果以字符串开头,则必须开始键入数字。如您所述,字符串失败,请注意大于
long
的数字将被忽略或“修剪”

import java.util.Scanner;

public class Test {

    public static final void main(String[] args) {
        long a = 0, b = 0, c = 0, d = 0;

        System.out.println("Type some numbers!");

        Scanner sc = new Scanner(System.in);
        String in = sc.nextLine();
        Scanner ln = new Scanner(in);

        while (ln.hasNextLong()) {
            a = ln.nextLong();
            if (a > b) {
                b = a;
                ++d;
            }
            ++c;
        }

        System.out.println("\n   info:");
        System.out.println("     highest:" + b);
        System.out.println("     iterations:" + c);
        System.out.println("     overrides:" + d);
    }
} 

这是我的版本,我认为你正在努力实现,它主要是工作,请注意,你必须开始键入数字,如果你用一个字符串开始,它失败了,正如你所描述的,还要注意大于
long
的数字被忽略或“修剪”

import java.util.Scanner;

public class Test {

    public static final void main(String[] args) {
        long a = 0, b = 0, c = 0, d = 0;

        System.out.println("Type some numbers!");

        Scanner sc = new Scanner(System.in);
        String in = sc.nextLine();
        Scanner ln = new Scanner(in);

        while (ln.hasNextLong()) {
            a = ln.nextLong();
            if (a > b) {
                b = a;
                ++d;
            }
            ++c;
        }

        System.out.println("\n   info:");
        System.out.println("     highest:" + b);
        System.out.println("     iterations:" + c);
        System.out.println("     overrides:" + d);
    }
} 

下面是代码的更正版本,突出显示了您所犯的错误

Scanner input = new Scanner(System.in);
int max = 0;
int number = 0;
int count_max = 0;
while (true) {
    number = input.nextInt();
    if (number == 0) {
        break;
    }
    // you are assigning number = max, and max is 0 initially, so number
    // =0;
    // number =max;
    // if number =0 and max =0 then below condition will never          // satisfied.
    if (max < number) {
        max = number;
        // it means every time when you have any number greater then the                // second one just add it.
            // count_max++;
            count_max = 0;
        }

    // This condition will make sure that only add count by 1 if max =
    // currentNum
    if (max == number) {

        count_max++;
    }

}
// You have written number instead of max down there.
System.out.println("Max Number: " + max);
System.out.println("Occurences: " + count_max);
扫描仪输入=新扫描仪(System.in);
int max=0;
整数=0;
int count_max=0;
while(true){
number=input.nextInt();
如果(数字==0){
打破
}
//您正在分配number=max,max最初是0,所以number
// =0;
//数量=最大值;
//如果number=0且max=0,则永远不会满足以下条件。
如果(最大值<数量){
最大值=数量;
//这意味着每次当你有任何大于//第二个的数字时,只要把它加起来。
//计数_max++;
计数_max=0;
}
//此条件将确保如果最大值为,则仅按1添加计数=
//当前数
如果(最大==数量){
计数_max++;
}
}
//你在下面写的是数字而不是max。
System.out.println(“最大编号:+Max”);
System.out.println(“发生次数:+count\u max”);

以下是代码的更正版本,其中突出显示了您所犯的错误

Scanner input = new Scanner(System.in);
int max = 0;
int number = 0;
int count_max = 0;
while (true) {
    number = input.nextInt();
    if (number == 0) {
        break;
    }
    // you are assigning number = max, and max is 0 initially, so number
    // =0;
    // number =max;
    // if number =0 and max =0 then below condition will never          // satisfied.
    if (max < number) {
        max = number;
        // it means every time when you have any number greater then the                // second one just add it.
            // count_max++;
            count_max = 0;
        }

    // This condition will make sure that only add count by 1 if max =
    // currentNum
    if (max == number) {

        count_max++;
    }

}
// You have written number instead of max down there.
System.out.println("Max Number: " + max);
System.out.println("Occurences: " + count_max);
扫描仪输入=新扫描仪(System.in);
int max=0;
整数=0;
int count_max=0;
while(true){
number=input.nextInt();
如果(数字==0){
打破
}
//您正在分配number=max,max最初是0,所以number
// =0;
//数量=最大值;
//如果number=0且max=0,则永远不会满足以下条件。
如果(最大值<数量){
最大值=数量;
//这意味着每次当你有任何大于//第二个的数字时,只要把它加起来。
//计数_max++;
计数_max=0;
}
//此条件将确保如果最大值为,则仅按1添加计数=
//当前数
如果(最大==数量){
计数_max++;
}
}
//你在下面写的是数字而不是max。
System.out.println(“最大编号:+Max”);
System.out.println(“发生次数:+count\u max”);
if(max>number)也有错误。。max以0开头。。这只是重新分配