亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我的 Java 程序拒絕接受字符串輸入

我的 Java 程序拒絕接受字符串輸入

函數式編程 2022-01-12 14:50:44
我有一個字符串數組 - 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 回答

?
喵喵時光機

TA貢獻1846條經驗 獲得超7個贊

那是因為該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);

    }

}


查看完整回答
反對 回復 2022-01-12
?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

試試這個..你的 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


查看完整回答
反對 回復 2022-01-12
  • 2 回答
  • 0 關注
  • 202 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號