问题:在我的标记变量中读取10个不同的标记。 FOR循环

时间:2017-11-11 15:22:08

标签: java java.util.scanner

import java.util.*;

public class loops

{

public static void main (String []args)

    {

        Scanner input = new Scanner (System.in).useDelimiter("\n");

        for (int i = 0; i <= 9; i++)
        {
                    System.out.print("Enter your mark:  ");
                    int marks = input.nextInt(); 
        }

        int marks = + input.nextInt();
        int totalmarks = marks / 10;

        System.out.println("The class average was:"+ totalmarks + ".");
    }
}

1 个答案:

答案 0 :(得分:1)

问题没有明确要求,但根据我的解释,问题是添加所有输入标记并给出平均值,因此声明一个初始值为0的sum变量并添加所有作为输入的标记并取平均值

import java.util.*;

public class loops

{

public static void main (String []args)

{

    Scanner input = new Scanner (System.in).useDelimiter("\n");
    int sum=0;
    for (int i = 0; i <= 9; i++)
    {
                System.out.print("Enter your mark:  ");
                int marks = input.nextInt(); 
                sum=sum+marks;
    }


    int totalmarks = sum / 10;

    System.out.println("The class average was:"+ totalmarks + ".");
}
}