2012-02-06 66 views
0

我正在试图从循环中查找最大数,并且它是出现次数。这是我迄今为止编写的代码。从循环中查找最大数

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

我在输出中只有零,我正在做的任何逻辑错误?

+0

您正在打破循环,如果数量== 0也许你从来没有让一个迭代检查input.nextInt()''的价值,你的'while'语句看起来无限的我有一种感觉,编译器做了一些奇怪的优化周围,你的逻辑获得最大数量(最高数字?)是值得怀疑的 – 2012-02-06 00:50:05

+0

如果这是一个家庭作业问题,那么请标记它。 :) – Erica 2012-02-06 01:14:05

+1

如果下面的代码示例不起作用,请输出变量'number'或使用调试器来查看发生了什么。 – UmNyobe 2012-02-06 01:51:12

回答

2

这是我的版本是什么,我认为你正在努力实现的,它主要工作,请注意,你必须开始在数字输入,如果你开始你描述它失败的字符串,还要注意数字越大则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); 
    } 
} 
0

删除行number = max

+0

我仍然收到零。 – Kaushank 2012-02-06 00:46:40

+0

IF(MAX>号)也有错..最大开始了为0 ..如果其比数大..它不会,除非数字为负..尝试如果(号码> max)是唯一的重新分配,而不是 – 2012-02-06 00:49:16

+0

但仍然最大数字为0,并且出现次数为1. – Kaushank 2012-02-06 00:51:25

1

是有,你有一行:

number = max 

其次

if (max > number) { 

最大绝不会大于数量更大,因为你只需将它们设置为在前面的行中相等

+0

仍然是零。 – Kaushank 2012-02-06 00:48:39

+1

你做了什么改变? – 2012-02-06 02:23:16

0

几个问题wi代码。这应该适合你:

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

我认为你有“number = max”的行需要修改。您在那里的代码将每次在循环中将数值设置为最大值。由于在循环中第一次具有0的值,所以它将无限期地保持为零。这意味着你的最大值永远不会改变。

所以,你不希望这样行,但你希望类似的东西。 如果的数字具有相同的值(==)max,然后您需要添加1到count_max。

最后如果块需要轻微的调整了。 如果你找到一个新的最高值(即数>最大,而不是其他方式),然后需要您count_max重置为1,而不是把它加1。

0

这里与你们所强调的错误代码的修正版本。

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