第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

函數(shù)式編程 2022-01-12 14:50:44
我有一個字符串數(shù)組 - name[]。當我嘗試使用 Scanner 類從用戶那里獲取輸入時,程序似乎忽略了該語句并繼續(xù)下一個。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條經(jīng)驗 獲得超7個贊

那是因為該sc.nextInt()方法不會讀取您輸入中的換行符,因此您需要調(diào)用sc.nextLine()


從文檔


將此掃描器前進到當前行并返回被跳過的輸入。


此方法返回當前行的其余部分,不包括末尾的任何行分隔符。位置設(shè)置為下一行的開頭。


由于此方法繼續(xù)搜索輸入以查找行分隔符,因此如果不存在行分隔符,它可能會緩沖所有搜索要跳過的行的輸入。


您的代碼現(xiàn)在看起來像:


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條經(jīng)驗 獲得超4個贊

試試這個..你的 sc.nextLine() 在你輸入整數(shù)值后讀取空字符串


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 關(guān)注
  • 194 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號