java 具有范围的Java“for”循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27000113/
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 'for' loop with range
提问by Niral Mehta
I am working on a basic program (console).
我正在开发一个基本程序(控制台)。
The program should allow the user to enter in various marks, until the mark entered exceeds 100. At this point the program should display a histogram. Each star represents a student who achieved a module mark in the range shown.
程序应该允许用户输入各种标记,直到输入的标记超过 100。此时程序应该显示一个直方图。每颗星代表在所示范围内获得模块分数的学生。
This is an example of the output.
这是输出的示例。
0 - 29 xxx
0 - 29 xxx
30 - 39 xxxxx
30 - 39 xxx
40 - 69 xxxxxxx
40 - 69 xxxxxxx
70 - 100 xxxx
70 - 100 xxxx
20 students in total.
共20名学生。
As the user enters each mark, there ought to be a counter that increases in value and print the total number of marks entered.
当用户输入每个标记时,应该有一个计数器增加值并打印输入的标记总数。
I want to make sure that the program is as efficient as possible but also understandable
我想确保程序尽可能高效但也可以理解
Code
代码
Working with @Dici on collabedit I have an amazing answer to my question:
在 collabedit 上与 @Dici 合作,我对我的问题有一个惊人的答案:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] ranges = { 0,29,39,69,100 };
int[] inRange = new int[ranges.length - 1];
int mark;
do {
System.out.println("Enter Mark:");
mark = sc.nextInt();
for (int j=1 ; j<ranges.length ; j++)
if (ranges[j-1] <= mark && mark <= ranges[j]) {
inRange[j-1]++;
break;
}
} while (mark <= 100);
System.out.println(Arrays.toString(inRange));
String s = "The number of students that have scored between %d and %d is: ";
int k = 0;
for (int i=0 ; i<ranges.length - 1 ; i++) {
System.out.print(String.format(s,ranges[i] + k,ranges[i + 1]));
for (int r = 0; r<inRange[i] ; r++)
System.out.print("*");
System.out.println();
k = 1;
}
sc.close();
Thank you again for your amazing help!
再次感谢您的大力帮助!
回答by Dici
As we did it together on Collabedit, here is a working code for your question :
正如我们在 Collabedit 上一起做的那样,这是您问题的有效代码:
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] ranges = { 0,29,39,69,100 };
int[] inRange = new int[ranges.length - 1];
int mark;
do {
System.out.println("Enter Mark:");
mark = sc.nextInt();
for (int j=1 ; j<ranges.length ; j++)
if (ranges[j-1] <= mark && mark <= ranges[j]) {
inRange[j-1]++;
break;
}
} while (mark <= 100);
System.out.println(Arrays.toString(inRange));
String s = "The number of students that have scored between %d and %d is : ";
int k = 0;
for (int i=0 ; i<ranges.length - 1 ; i++) {
System.out.print(String.format(s,ranges[i] + k,ranges[i + 1]));
for (int r = 0; r<inRange[i] ; r++)
System.out.print("*");
System.out.println();
k = 1;
}
sc.close();
}
}
回答by APerson
Quick suggestion: do not alter marksInsp
inside the loop if that's your loop index.
快速建议:marksInsp
如果这是您的循环索引,请不要在循环内更改。
Paulinho made the excellent suggestion of using the modulo operator. I'll extend that here to a map, since you didn't specify the upper mark limit:
保利尼奥提出了使用模运算符的绝妙建议。我将在此处将其扩展到地图,因为您没有指定上限:
public static void main(String[] args) {
int currentMark, currentBucket;
final int BUCKET_WIDTH = 30;
Map<Integer, Integer> markRanges = new HashMap<Integer, Integer>();
for(int i = 0; i < 101; i++) {
Scanner input = new Scanner(System.in);
System.out.print("Enter Marks: ");
currentMark = input.nextInt();
currentBucket = currentMark % BUCKET_WIDTH;
if(markRanges.containsKey(currentBucket)) {
// There's already an entry for this bucket
markRanges.put(currentBucket, markRanges.get(currentBucket) + 1);
} else {
// If there isn't, make a new entry
markRanges.put(currentBucket, 1);
}
// Print out grade buckets
for(Integer j : markRanges.keySet) {
System.out.println(String.format("There are %d students with scores between %d and %d", markRanges.get(j), j, j.intValue() + BUCKET_WIDTH - 1));
}
}
}
回答by DejaVuSansMono
Okay. Now that makes sense. If you have a number
好的。现在这是有道理的。如果你有号码
int mark = 3;
You can use an if statement
您可以使用 if 语句
if(mark <= 10){
//do this
}else if(mark <= 20){
//do this
}
You can also use a switch statement, however personal preference leads me to almost always use if statements.
您也可以使用 switch 语句,但是个人偏好使我几乎总是使用 if 语句。
If you want "buckets" you could use
如果你想要“桶”,你可以使用
ArrayList<String> marksForStudent = new ArrayList<>();
the documentation for that is here.
相关文档在这里。
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
Or, alternatively and probably what you want is a hashmap. With the student's name or other unique identifier as the key and then a collection to contain their marks.
或者,或者,您可能想要的是哈希图。以学生的姓名或其他唯一标识符为键,然后是一个包含他们分数的集合。
HashMap<String, ArrayList<String>> gradesForStudents = new HashMap<>();
gradesForStudents.put("Marc",Arrays.asList("99","98","100"));
Something like that.
类似的东西。