我的 Java 程序拒绝接受字符串输入

我有一个字符串数组 - name[]。当我尝试使用 Scanner 类从用户那里获取输入时,程序似乎忽略了该语句并继续下一个。


import java.util.Scanner;

class Student { //start of class

    public static void main(String[] args) { //start of method main

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter number of students: ");

        int n = sc.nextInt();

        String name[] = new String[n];

        int totalmarks[] = new int[n];

        for (int i = 0; i < n; i++) {

            System.out.println("Student " + (i + 1));

            System.out.print("Enter name: ");

            name[i] = sc.nextLine();

            System.out.print("Enter marks: ");

            totalmarks[i] = sc.nextInt();

        }

        int sum = 0;

        for (int i = 0; i < n; i++) {

            sum = sum + totalmarks[i]; //calculating total marks

        }

        double average = (double) sum / n;

        System.out.println("Average is " + average);

        for (int i = 0; i < n; i++) {

            double deviation = totalmarks[i] - average;

            System.out.println("Deviation of " + name[i] + " is " + deviation);

        }

    } //end of method main

} //end of class


函数式编程
浏览 225回答 2
2回答

喵喵时光机

那是因为该sc.nextInt()方法不会读取您输入中的换行符,因此您需要调用sc.nextLine()从文档将此扫描器前进到当前行并返回被跳过的输入。此方法返回当前行的其余部分,不包括末尾的任何行分隔符。位置设置为下一行的开头。由于此方法继续搜索输入以查找行分隔符,因此如果不存在行分隔符,它可能会缓冲所有搜索要跳过的行的输入。您的代码现在看起来像:public static void main(String[] args) {&nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; System.out.print("Enter number of students: ");&nbsp; &nbsp; int n = sc.nextInt();&nbsp; &nbsp; sc.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// <----- observe this&nbsp; &nbsp; String name[] = new String[n];&nbsp; &nbsp; int totalmarks[] = new int[n];&nbsp; &nbsp; for (int i = 0; i < n; i++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Student " + (i + 1));&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter name: ");&nbsp; &nbsp; &nbsp; &nbsp; name[i] = sc.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter marks: ");&nbsp; &nbsp; &nbsp; &nbsp; totalmarks[i] = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; sc.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// <----- observe this&nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; int sum = 0;&nbsp; &nbsp; for (int i = 0; i < n; i++) {&nbsp; &nbsp; &nbsp; &nbsp; sum = sum + totalmarks[i];&nbsp; &nbsp; }&nbsp; &nbsp; double average = (double) sum / n;&nbsp; &nbsp; System.out.println("Average is " + average);&nbsp; &nbsp; for (int i = 0; i < n; i++) {&nbsp; &nbsp; &nbsp; &nbsp; double deviation = totalmarks[i] - average;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Deviation of " + name[i] + " is " + deviation);&nbsp; &nbsp; }}

慕村9548890

试试这个..你的 sc.nextLine() 在你输入整数值后读取空字符串import java.util.Scanner;class Student&nbsp; &nbsp; {//start of class&nbsp; &nbsp; &nbsp; &nbsp; public static void main(String[] args)&nbsp; &nbsp; &nbsp; &nbsp; {//start of method main&nbsp; &nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter number of students: ");&nbsp; &nbsp; &nbsp; &nbsp; int n = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; String emp = sc.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; String name[] = new String[n];&nbsp; &nbsp; &nbsp; &nbsp; int totalmarks[] = new int[n];&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0;i<n;i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Student " + (i + 1));&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter name: ");&nbsp; &nbsp; &nbsp; &nbsp; name[i] = sc.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter marks: ");&nbsp; &nbsp; &nbsp; &nbsp; totalmarks[i] = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; emp = sc.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; int sum = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < n; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; sum = sum + totalmarks[i];//calculating total marks&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; double average = (double) sum / n;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Average is " + average);&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < n; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; double deviation = totalmarks[i] - average;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Deviation of " + name[i] + " is " + deviation);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }//end of method main&nbsp; &nbsp; &nbsp; &nbsp; }//end of class
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java