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

时间:2018-11-11 06:11:43

标签: java string input

我有一个字符串数组-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

2 个答案:

答案 0 :(得分:0)

这是因为sc.nextInt()方法不会读取输入中的换行符,因此您需要调用sc.nextLine()

来自文档

  

将此扫描程序前进到当前行之后,并返回输入   被跳过了。

     

此方法返回当前行的其余部分,不包括任何行   末尾的分隔符。该位置设置为下一个开始   线。

     

由于此方法继续搜索输入以寻找   行分隔符,它可以缓冲所有搜索   如果没有行分隔符,则跳过该行。

您的代码现在看起来像:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter number of students: ");
    int n = sc.nextInt();
    sc.nextLine();                         // <----- observe this
    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();
        sc.nextLine();                     // <----- observe this  
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum = sum + totalmarks[i];
    }
    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);
    }
}

答案 1 :(得分:0)

尝试一下。.输入整数值后,您的sc.nextLine()读取空字符串

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 emp = sc.nextLine();
        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();
        emp = sc.nextLine();
        }
        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