Java - 使用 for 循环来平均从键盘读入的一组成绩
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25818028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - Using for loop to average set of grades read in from keyboard
提问by Tekt
First time poster here. I'm aware of the negative stigma carried with asking for help on homework assignments, however I believe this would be an exception as this is an intro course and the professor stated specifically to use Google to find examples of for loops in Java (of which we have yet to even cover in class). I have absolutely no Java experience and would really appreciate any feedback:
第一次海报在这里。我知道在家庭作业中寻求帮助会带来负面的耻辱,但是我相信这将是一个例外,因为这是一门介绍课程,教授特别指出要使用 Google 来查找 Java 中 for 循环的示例(其中我们甚至还没有在课堂上讲过)。我完全没有 Java 经验,非常感谢任何反馈:
Program asks user how many grades there are.
程序询问用户有多少个等级。
Program asks user for each grade (for loop needed and should sum grades within loop).
程序要求用户提供每个等级(需要循环并且应该在循环内总结等级)。
Take sum of all grades, compute average and store in a float variable grade.
取所有成绩的总和,计算平均值并存储在浮点变量成绩中。
Print grade value to console and append a number to a string such as "Grade Average is: " + grade
将成绩值打印到控制台并将数字附加到字符串中,例如 "Grade Average is: " + grade
Example should read as:
示例应为:
Enter number of grades: 2
Enter grade: 90
Enter grade: 81
Grade Average is: 85.5
Enter number of grades: 2
Enter grade: 90
Enter grade: 81
Grade Average is: 85.5
My code so far (not much here):
到目前为止我的代码(这里不多):
// This program computes the letter grades for number of grades given by user
import java.util.*;
public class GradeAverage
{
public static void main(String[] args)
{
int count;
float sum = 0;
float grade;
Scanner scan = new Scanner(System.in);
}
}
Edit:
编辑:
// This program computes the letter grades for number of grades given by user
import java.util.*;
public class GradeAverage
{
public static void main(String[] args)
{
int count;
float sum = 0;
float grade;
Scanner scan = new Scanner(System.in);
System.out.print("Enter number of grades: ");
count = scan.nextInt();
for (int i = 0; i < count; ++i)
System.out.print("Enter grade " + (i + 1) + ": ");
grade = scan.nextFloat();
sum += grade;
System.out.println("The average of the grades is: " + sum/count);
}
}
This is what I have now, however a test displays incorrect results (example):
这就是我现在所拥有的,但是测试显示不正确的结果(示例):
Enter number of grades: 2
Enter grade 1: Enter grade 2: 50 50
The average of the grades is: 25.0
Enter number of grades: 2
Enter grade 1: Enter grade 2: 50 50
The average of the grades is: 25.0
Each grade needs to be entered on separate lines so the averaging is skewed as a result.
每个等级都需要在单独的行上输入,因此平均值会出现偏差。
采纳答案by Shane
import java.util.Scanner;
public static void main(String[] args) {
int count = 0;
float sum = 0;
float grade = 0;
Scanner scan = new Scanner(System.in);
System.out.print("Enter number of grades: ");
count = scan.nextInt();
for (int i = 0; i < count; i++) {
System.out.print("Enter grade no " + (i + 1) + " : ");
grade = scan.nextFloat();
sum += grade;
}
System.out.println("Sum = " + sum);
System.out.println("Average = " + sum / (float) count);
}
回答by vlatkozelka
Break the big task into smaller tasks , like @user2864740 said , write the algorithm (not code) on a paper then start translating that to code
把大任务分解成小任务,就像@user2864740 说的,在纸上写算法(不是代码),然后开始把它翻译成代码
u reached the part where u created a scanner to read input , now read the input and ....figure out the rest .
你到达了你创建一个扫描仪来读取输入的部分,现在读取输入并......弄清楚其余部分。
To learn how to read user input read this.
要了解如何读取用户输入,请阅读此内容。
To learn how to make an integer out of Strings ur scanning read this
要了解如何从字符串中生成整数,请阅读 此内容
the rest is basic math really , read your textbook , and good luck ;)
剩下的真的是基础数学,阅读你的教科书,祝你好运;)
edit : at least come out with some algorithm then maybe we'll help with the code
编辑:至少提出一些算法,然后也许我们会帮助编写代码
回答by ThaBomb
I won't solve the homework for you, I will help you however:
我不会为你解决作业,但我会帮助你:
How to use a for loop:
如何使用 for 循环:
for (int i = #startValue#; #booleanCondition#; #runTheFollowingCodeAtEachIteration#)
{
//code
}
ex:
前任:
for(int i = 0; i<10; i++)
{
System.out.println(i);
}
will display:
将显示:
0
1
2
3
4
5
6
7
8
9
Your homework:
你的家庭作业:
Program asks how many grades there are:
Scan a value called NumberOfGrades (called count in your code) inputted by the user.
程序询问有多少个等级:
扫描用户输入的一个名为 NumberOfGrades(在您的代码中称为 count)的值。
Program asks user for each grade + sums the grades:
Use a for loop, with a starting value of i, and a upper limit of NumberOfGrades. Scan each grade and add it to a value called GradeSum, which initially should be 0 before entering the for loop.
程序询问用户每个等级 + 对等级求和:
使用 for 循环,起始值为 i,上限为 NumberOfGrades。扫描每个等级并将其添加到名为 GradeSum 的值中,该值在进入 for 循环之前最初应为 0。
Print value to console... :
Divide GradeSum by NumberofGrades, and display it how you would like it to be displayed.
将值打印到控制台...:将
GradeSum 除以 NumberofGrades,并以您希望的方式显示它。
Tips:
-Use System.out.print("\nEnter grade: "); in your for loop before each scan.
提示:-
使用 System.out.print("\n输入成绩:"); 在每次扫描之前的 for 循环中。
回答by Mshnik
To avoid directly answering your homework question (which both won't help you get it and is probably not allowed), let's start with "what is a for loop?"
为了避免直接回答你的作业问题(这两者都不会帮助你得到它,而且可能是不允许的),让我们从“什么是 for 循环?”开始。
A for loop is a fancy loop that does the following things for you:
for 循环是一个花哨的循环,它为您执行以下操作:
- Initializes one (or more) variables to initial values the first time the loop statement is executed
- Each iteration, checks a boolean condition to determine if it should loop again. If the expression evaluates to true, iterate again. Otherwise, break the loop and continue with the code following the loop.
- A statement that is run each time the loop finishes iterating.
- 第一次执行循环语句时将一个(或多个)变量初始化为初始值
- 每次迭代,检查一个布尔条件以确定它是否应该再次循环。如果表达式的计算结果为真,则再次迭代。否则,中断循环并继续循环后面的代码。
- 每次循环完成迭代时运行的语句。
For example, the following loop would print the numbers 1 .. 10.
例如,以下循环将打印数字 1 .. 10。
for(int i = 1; i <= 10; i++){
System.out.println(i);
}
- The first part of the loop statement
int i = 1
is the initialization block.i
is initialized to anint
with value1
when the for loop is executed for the first time. - The second part of the loop statement
i <= 10
is the boolean condition to check to determine if another iteration is required. In this case,i <= 10
evaluates to true ifi
is less than or equal to10
, and false oncei
hits11
(or any larger number). - Finally, the third part
i++
is the statement run when the for loop finishes an iteration.i++
adds1
to the current value ofi
, thusi
will increase in value by1
each iteration.
- 循环语句的第一部分
int i = 1
是初始化块。当 for 循环第一次执行时i
被初始化为int
with 值1
。 - 循环语句的第二部分
i <= 10
是要检查以确定是否需要另一次迭代的布尔条件。在这种情况下,i <= 10
如果i
小于或等于,则计算为真10
,一旦i
命中11
(或任何更大的数字),则为假。 - 最后,第三部分
i++
是当 for 循环完成迭代时运行的语句。i++
添加1
到 的当前值i
,因此每次迭代i
都会增加值1
。