英文:
Why is my code not asking for the user input on the for loop (Java)
问题
我无法在for循环中获取用户输入,对此我毫无头绪。我对Java相当新手,所以请原谅我的粗心。我贴出了整个代码,因为我完全不知道问题出在哪里。
package grade.average.calculator;
import java.util.Scanner;
@SuppressWarnings("empty-statement")
public class GradeAverageCalculator {
public static void main(String[] args) {
Scanner student = new Scanner(System.in);
System.out.println("Please enter your name: ");
String person;
person = student.nextLine();
Scanner muid = new Scanner(System.in);
System.out.println("Please enter your MUID: ");
String id;
id = muid.nextLine();
}
int grades[] = new int[3];
int i;
float total = 0, avg;
Scanner marks = new Scanner(System.in);
{
for (i = 0; i < 3; i++) {
System.out.print("Enter Grades of Subject" + (i + 1) + ":");
grades[i] = marks.nextInt();
total = total + grades[i];
}
marks.close();
avg = total / 3;
}
}
英文:
I can't get the user input in the for loop and I have no clue why. I am fairly new to java so please forgive my sloppiness. I posted my whole code because I do not know where the issue is at all.
package grade.average.calculator;
import java.util.Scanner;
@SuppressWarnings("empty-statement")
public class GradeAverageCalculator {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner student = new Scanner(System.in);
System.out.println("Please enter your name: ");
String person;
person = student.nextLine();
Scanner muid = new Scanner(System.in);
System.out.println("Please enter your MUID: ");
String id;
id = muid.nextLine();
}
int grades[]= new int[3];
int i;
float total=0, avg;
Scanner marks = new Scanner(System.in);
{
for(i=0;i<3;i++){
System.out.print("Enter Grades of Subject"+(i+1)+":");
grades[i] = marks.nextInt();
total = total + grades[i];
}
marks.close();
avg = total/3;
答案1
得分: 1
你的逻辑代码位于主类之外。
这不是可达代码,因此无法执行。
因此,最简单的方式是:
public static void main(String[] args) {
// TODO code application logic here
Scanner student = new Scanner(System.in);
System.out.println("请输入您的姓名:");
String person;
person = student.nextLine();
Scanner muid = new Scanner(System.in);
System.out.println("请输入您的MUID:");
String id;
id = muid.nextLine();
method();
}
public static void method() {
int grades[] = new int[3];
int i;
float total = 0, avg;
Scanner marks = new Scanner(System.in);
{
for (i = 0; i < 3; i++) {
System.out.print("请输入第" + (i + 1) + "门科目的成绩:");
grades[i] = marks.nextInt();
total = total + grades[i];
}
marks.close();
avg = total / 3;
}
}
英文:
Your logic is outside of your main class.
It is not a reachable code, so it can't be executed.
So, the easiest:
public static void main(String[] args) {
// TODO code application logic here
Scanner student = new Scanner(System.in);
System.out.println("Please enter your name: ");
String person;
person = student.nextLine();
Scanner muid = new Scanner(System.in);
System.out.println("Please enter your MUID: ");
String id;
id = muid.nextLine();
method();
}
public static void method() {
int grades[] = new int[3];
int i;
float total = 0, avg;
Scanner marks = new Scanner(System.in);
{
for (i = 0; i < 3; i++) {
System.out.print("Enter Grades of Subject" + (i + 1) + ":");
grades[i] = marks.nextInt();
total = total + grades[i];
}
marks.close();
avg = total / 3;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论