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

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

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(&quot;empty-statement&quot;)
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(&quot;Please enter your name: &quot;);
       String person;
            person = student.nextLine();
       Scanner muid = new Scanner(System.in);
            System.out.println(&quot;Please enter your MUID: &quot;);
       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&lt;3;i++){
        System.out.print(&quot;Enter Grades of Subject&quot;+(i+1)+&quot;:&quot;);
        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(&quot;Please enter your name: &quot;);
        String person;
        person = student.nextLine();
        Scanner muid = new Scanner(System.in);
        System.out.println(&quot;Please enter your MUID: &quot;);
        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 &lt; 3; i++) {
                System.out.print(&quot;Enter Grades of Subject&quot; + (i + 1) + &quot;:&quot;);
                grades[i] = marks.nextInt();
                total = total + grades[i];
            }
            marks.close();
            avg = total / 3;
        }
    }
    

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:

确定