2014-01-29 156 views
0

我是新来的Java编程尝试通过使用下面的代码通过扫描仪类输入值的世界。问题与扫描仪

问题是扫描仪没有打开用户输入的控制台,并且默认显示平均值为零。调试控制台会抛出文件未找到异常错误。请指教...

import java.util.Scanner; 

public class Avg { 

    int no = 0; 
    int sum = 0; 

    void average(){ 
    System.out.println("pls enter 5 numbers"); 
    Scanner s = new Scanner(System.in); 
    for(int i = 0; i > 5; ) { 
     no = s.nextInt(); 
     sum = no + sum; 
     i++; 
    } 
    int avg = sum/5; 
    System.out.println(avg); 
    } 

    public static void main(String[] h){ 
    Avg s = new Avg(); 
    s.average(); 
    } 
} 
+0

'sum/5'总是0.写入'sum/5.0'。 for循环条件为 – Maroun

+0

错误。 –

+0

将'>'扭曲为'<';) –

回答

3

第一个for循环应该有i < 5条件,但它是在你的代码i > 5。所以它会进行0次迭代。

1

For循环有问题。您将i的值设置为0并检查它是否大于5。根本不可能。它变成false。请更改条件检查

for(int i=0;i<5;){ 
no=s.nextInt(); 
sum=no+sum; 
i++; 
} 

否则,i将永远比5较小和内环路不会得到。因此,avg的值将始终为0