为什么我的代码在 for 循环中没有要求用户输入(Java)

huangapple go评论84阅读模式
英文:

Why is my code not asking for the user input on the for loop (Java)

问题

我无法在for循环中获取用户输入,对此我毫无头绪。我对Java相当新手,所以请原谅我的粗心。我贴出了整个代码,因为我完全不知道问题出在哪里。

  1. package grade.average.calculator;
  2. import java.util.Scanner;
  3. @SuppressWarnings("empty-statement")
  4. public class GradeAverageCalculator {
  5. public static void main(String[] args) {
  6. Scanner student = new Scanner(System.in);
  7. System.out.println("Please enter your name: ");
  8. String person;
  9. person = student.nextLine();
  10. Scanner muid = new Scanner(System.in);
  11. System.out.println("Please enter your MUID: ");
  12. String id;
  13. id = muid.nextLine();
  14. }
  15. int grades[] = new int[3];
  16. int i;
  17. float total = 0, avg;
  18. Scanner marks = new Scanner(System.in);
  19. {
  20. for (i = 0; i < 3; i++) {
  21. System.out.print("Enter Grades of Subject" + (i + 1) + ":");
  22. grades[i] = marks.nextInt();
  23. total = total + grades[i];
  24. }
  25. marks.close();
  26. avg = total / 3;
  27. }
  28. }
英文:

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.

  1. package grade.average.calculator;
  2. import java.util.Scanner;
  3. @SuppressWarnings(&quot;empty-statement&quot;)
  4. public class GradeAverageCalculator {
  5. /**
  6. * @param args the command line arguments
  7. */
  8. public static void main(String[] args) {
  9. // TODO code application logic here
  10. Scanner student = new Scanner(System.in);
  11. System.out.println(&quot;Please enter your name: &quot;);
  12. String person;
  13. person = student.nextLine();
  14. Scanner muid = new Scanner(System.in);
  15. System.out.println(&quot;Please enter your MUID: &quot;);
  16. String id;
  17. id = muid.nextLine();
  18. }
  19. int grades[]= new int[3];
  20. int i;
  21. float total=0, avg;
  22. Scanner marks = new Scanner(System.in);
  23. {
  24. for(i=0;i&lt;3;i++){
  25. System.out.print(&quot;Enter Grades of Subject&quot;+(i+1)+&quot;:&quot;);
  26. grades[i] = marks.nextInt();
  27. total = total + grades[i];
  28. }
  29. marks.close();
  30. avg = total/3;

答案1

得分: 1

你的逻辑代码位于主类之外。
这不是可达代码,因此无法执行。

因此,最简单的方式是:

  1. public static void main(String[] args) {
  2. // TODO code application logic here
  3. Scanner student = new Scanner(System.in);
  4. System.out.println("请输入您的姓名:");
  5. String person;
  6. person = student.nextLine();
  7. Scanner muid = new Scanner(System.in);
  8. System.out.println("请输入您的MUID:");
  9. String id;
  10. id = muid.nextLine();
  11. method();
  12. }
  13. public static void method() {
  14. int grades[] = new int[3];
  15. int i;
  16. float total = 0, avg;
  17. Scanner marks = new Scanner(System.in);
  18. {
  19. for (i = 0; i < 3; i++) {
  20. System.out.print("请输入第" + (i + 1) + "门科目的成绩:");
  21. grades[i] = marks.nextInt();
  22. total = total + grades[i];
  23. }
  24. marks.close();
  25. avg = total / 3;
  26. }
  27. }
英文:

Your logic is outside of your main class.
It is not a reachable code, so it can't be executed.

So, the easiest:

  1. public static void main(String[] args) {
  2. // TODO code application logic here
  3. Scanner student = new Scanner(System.in);
  4. System.out.println(&quot;Please enter your name: &quot;);
  5. String person;
  6. person = student.nextLine();
  7. Scanner muid = new Scanner(System.in);
  8. System.out.println(&quot;Please enter your MUID: &quot;);
  9. String id;
  10. id = muid.nextLine();
  11. method();
  12. }
  13. public static void method() {
  14. int grades[] = new int[3];
  15. int i;
  16. float total = 0, avg;
  17. Scanner marks = new Scanner(System.in);
  18. {
  19. for (i = 0; i &lt; 3; i++) {
  20. System.out.print(&quot;Enter Grades of Subject&quot; + (i + 1) + &quot;:&quot;);
  21. grades[i] = marks.nextInt();
  22. total = total + grades[i];
  23. }
  24. marks.close();
  25. avg = total / 3;
  26. }
  27. }

huangapple
  • 本文由 发表于 2020年10月9日 00:00:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64266351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定