Loop->Array Output

依然范特西╮ 提交于 2019-12-13 02:05:02

问题


I'm writing a loop to fill an array. I think I have the coding down, but when I run the compiled code through Java, it doesn't come out right in the command prompt.

Here's the code:

import java.util.Scanner;
import java.io.*;
public class Pr42
{
    public static void main(String[]args) throws IOException
{
    int k,m,g;
    String n;
    //double g;
    Scanner input1=new Scanner(System.in);
    String[]Name=new String [5];
    double[]Grade=new double[Name.length];
    k=0;
    while (k<Name.length)
        {
        m=k+1;
        System.out.print("Enter the name of student "+m+": ");
        Name[k]=input1.nextLine();
        System.out.print("");
        System.out.print("Please enter the grade of student "+m+": ");
        Grade[k]=input1.nextInt();
        k++;
        }
    }
}

Here's the output in the command prompt:

Enter the name of student 1:

Please enter the grade of student 1:

Please enter the name of student 2: Please enter the grade of student 2:

The problem is that line regarding the second student.

What did I do wrong in the code to get an output like that?


回答1:


You need to identify name has input in next line with Name[k] = input1.nextLine();

int k, m, g;
String n;
//double g;
Scanner input1 = new Scanner(System.in);
String[] Name = new String[5];
double[] Grade = new double[Name.length];
k = 0;
while (k < Name.length) {
    m = k + 1;
    System.out.print("Enter the name of student " + m + ": ");
    Name[k] = input1.nextLine();
    System.out.print("");
    System.out.print("Please enter the grade of student " + m + ": ");
    Grade[k] = input1.nextDouble();
    input1.nextLine();
    k++;
}

EDITED: As per mentioned in Tom's comment under this answer when you have Name[k] = input1.nextLine(); instead of input1.nextLine(); program works correctly, but it messed up with value of array.




回答2:


the nextInt of the Scannerdoes not read the "new line" character.
there are 2 ways to fix it.
1. call input1.nextLine(); after the input1.nextInt(); ignore what you get here, it is just to make it go to the next line.

Grade[k] = input1.nextInt();
input1.nextLine();

2. call input1.nextLine(); for the grade. the String you get you can cast to int and save in Grade[k].

String str = input1.nextLine();
Grade[k] = Integer.parseInt(str);



回答3:


This works:

public static void main(String[] args) throws IOException {
    int k, m, g;
    String n;
    // double g;
    Scanner input1 = new Scanner(System.in);
    String[] Name = new String[5];
    double[] Grade = new double[Name.length];
    k = 0;

    while (k < Name.length) {

        m = k + 1;
        System.out.print("Enter the name of student " + m + ": ");
        Name[k] = input1.nextLine();
        System.out.print("Please enter the grade of student " + m + ": ");
        Grade[k] = input1.nextInt();
        input1.nextLine();
        k++;
    }
}

I recommend you to please go through this question, you shall have your doubts clarified. Excerpt from the answer given in that post:

Scanner#nextInt method does not read the last newline character of your input, and thus that newline is consumed in the next call to Scanner#nextLine.




回答4:


The problem is that: Grade[k] = input1.nextInt(); don't reads the end of line or anything after the number.

Try placing a input1.nextLine(); after Grade[k]=input1.nextInt(); should solve the issue:

while (k<Name.length)
{
        m=k+1;
        System.out.print("Enter the name of student "+m+": ");
        Name[k]=input1.nextLine();
        System.out.print("");
        System.out.print("Please enter the grade of student "+m+": ");
        Grade[k]=input1.nextInt();
        input1.nextLine();
        k++;
}


来源:https://stackoverflow.com/questions/30955555/loop-array-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!