[英]How can I get the for loop to stop to stop running and proceed to the next methid once the program has been executed in Java?
The code is first getting the user to input the number of students in a class. Then the system will allow the user to add the names of those students.该代码首先让用户输入 class 中的学生人数。然后系统将允许用户添加这些学生的姓名。 After which the user can assign a mark and credit hours for subjects for each of those students.之后,用户可以为每个学生的科目分配分数和学分。 Once the marks and credit hours have been entered for each student, the system should then calculate the GPA.一旦为每个学生输入了分数和学分,系统就会计算 GPA。 However, the system continues to run the loop and it does not execute and display the GPA.然而,系统继续运行循环,它不执行和显示 GPA。 Can I be assisted as the why this loop does not execute:为什么这个循环不执行,我能得到帮助吗:
public class testing1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int result;
String[] names = new String[100];
System.out.println("Enter number of students (Must be between 1-50)");
result = in .nextInt();
System.out.println("Thank you, Please enter the names for the " + result + " Students");
int nameCount = 0;
while (nameCount < result) {
System.out.println("Enter First Name: ");
String s1 = in .next();
System.out.println("The students name is" + " " + s1);
System.out.println();
names[nameCount] = s1;
nameCount++;
}
double[][] marks = new double[100][2];
int[][] credits = new int[100][2];
double[] gpa = new double[100];
String[] subjects = {"Math", "Science",};
int cnt = 0;
Scanner s1 = new Scanner(System.in);
gpa[cnt] = 0;
int totalCredits = 0;
for (int i = 0; i < names.length; i++) {
for(int j = 0; j < subjects.length; j++) {
System.out.println();
System.out.println();
System.out.println("Please enter " + names[i] +"'s mark in " + subjects[j] + ": ");
marks[cnt][i] = s1.nextDouble();
System.out.println("Please enter " + names[i] +"'s Credit hours for "+ subjects[j] + ": ");
credits[cnt][i] = s1.nextInt();
totalCredits += credits[cnt][i];
gpa[cnt] += marks[cnt][i] * credits[cnt][i];
}
cnt++;
}
gpa[cnt] /= totalCredits;
System.out.printf("GPA of :" + names[cnt] + " is %.2f.", gpa[cnt]);
return;
}
}
Below is the output when I run the system from the beginning:下面是我从头开始运行系统时的output:
Enter number of students (Must be between 1-50)
2
Thank you, Please enter the names for the 2 Students
Enter First Name:
Alan
The students name is Alan
Enter First Name:
Bob
The students name is Bob
Please enter Alan's mark in Math:
12
Please enter Alan's Credit hours for Math:
12
Please enter Alan's mark in Science:
12
Please enter Alan's Credit hours for Science:
12
Please enter Bob's mark in Math:
12
Please enter Bob's Credit hours for Math:
12
Please enter Bob's mark in Science:
12
Please enter Bob's Credit hours for Science:
12
Please enter null's mark in Math:
12
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
at testing1.testing1.main(testing1.java:59)
In the first loop you're iterating over the whole array of names.在第一个循环中,您将遍历整个名称数组。 You also need a i < nameCount
condition because the arrays have data from the begining until as many students as the user has entered.您还需要一个i < nameCount
nameCount 条件,因为 arrays 具有从开始到与用户输入的学生一样多的数据。 So, the condition will be as follow:因此,条件如下:
//other code
for (int i = 0; i < names.length && i < nameCount; i++) {
//other code
}
If you want to stop a loop, then you can set to true some condition variable and break the loop depending on its state. Something like:如果你想停止循环,那么你可以将一些条件变量设置为 true 并根据其 state 中断循环。类似于:
for (...) {
//sentences
if (someCondition)
break;
}
Notes after the code.代码后的注释。
import java.util.Scanner;
public class Testing2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of students (between 1-50): ");
int result = in.nextInt();
in.nextLine();
String[] names = new String[result];
System.out.println("Thank you, please enter the names for the " + result + " students.");
for (int i = 0; i < result; i++) {
System.out.printf("Enter student %d first name: ", (i + 1));
names[i] = in.nextLine();
}
double[] gpa = new double[result];
String[] subjects = {"Math", "Science",};
double[][] marks = new double[result][subjects.length];
int[][] credits = new int[result][subjects.length];
for (int i = 0; i < result; i++) {
int totalCredits = 0;
for(int j = 0; j < subjects.length; j++) {
System.out.printf("Please enter %s's mark in %s: ", names[i], subjects[j]);
marks[i][j] = in.nextDouble();
System.out.printf("Please enter %s's credit hours for %s: ", names[i], subjects[j]);
credits[i][j] = in.nextInt();
totalCredits += credits[i][j];
gpa[i] += marks[i][j] * credits[i][j];
}
gpa[i] /= totalCredits;
System.out.printf("GPA of %s is %.2f%n", names[i], gpa[i]);
}
}
}
Scanner
object. No need to create a new Scanner
each time you want to get input from the user.您只需要一个Scanner
object。无需在每次要从用户那里获取输入时都创建一个新的Scanner
。result
to declare your names
array.您可以使用result
来声明您的names
数组。 Then you can use a for
loop to get the names.然后您可以使用for
循环来获取名称。s1
.不需要变量s1
。 Assign the value returned by method nextLine
directly to the names
array.将方法nextLine
返回的值直接分配给names
数组。result
when you declare the other arrays, including marks
, credits
and gpa
.您也可以在声明其他arrays时使用result
,包括marks
, credits
和gpa
。 In fact, you can use any expression that returns an int
result for any dimension of an array.事实上,您可以使用为数组的任何维度返回int
结果的任何表达式。nameCount
and cnt
.不需要变量nameCount
和cnt
。for
loops.所有计算都需要在嵌套for
循环中完成。totalCredits
for each student.您需要为每个学生重置totalCredits
。Output from a sample run:样本运行中的 Output:
Enter number of students (between 1-50): 2
Thank you, please enter the names for the 2 students.
Enter student 1 first name: George
Enter student 2 first name: Edson
Please enter George's mark in Math: 2.0
Please enter George's credit hours for Math: 3
Please enter George's mark in Science: 4.0
Please enter George's credit hours for Science: 2
GPA of George is 2.80
Please enter Edson's mark in Math: 2.67
Please enter Edson's credit hours for Math: 3
Please enter Edson's mark in Science: 1.33
Please enter Edson's credit hours for Science: 4
GPA of Edson is 1.90
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.