【问题标题】:Loop->Array Output循环->数组输出
【发布时间】:2015-06-20 15:16:10
【问题描述】:

我正在编写一个循环来填充一个数组。我想我已经把代码写下来了,但是当我通过 Java 运行编译后的代码时,它并没有在命令提示符下正确显示。

代码如下:

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++;
        }
    }
}

这是命令提示符中的输出:

输入学生 1 的姓名:

请输入学生1的年级:

请输入学生2的姓名:请输入学生2的年级:

问题是关于第二个学生的那句话。

为了得到这样的输出,我在代码中做错了什么?

【问题讨论】:

  • 输入是否正确?你能检查一下学生2的名字和成绩是否正确?
  • 将扫描仪声明带入循环也应该修复它。但我的偏好是阅读 nextLine() 而不是 nextInt() 进行评分并相应地解析/处理异常。

标签: java arrays loops output


【解决方案1】:

你需要用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++;
}

已编辑:当您使用 Name[k] = input1.nextLine(); 而不是 input1.nextLine(); 程序正常工作时,正如汤姆在此答案下的评论中所述,但它与数组的值混淆了。

【讨论】:

  • 我想知道为什么 OP 接受了这个答案。 Saleh 和 OP 都没有测试过这段代码吗?作为提示,您应该在while 循环之后打印Name 的内容。
  • @SudhansuChoudhary 声明 System.out.print(""); 取自 OPs 原始代码,这里没有意义。我猜萨利赫没有触及那条线,因为他不打算优化该代码(这是可以理解的)。
  • @SalehParsa “我已经测试了这段代码,它最终可以工作。” 让我猜猜,你只是运行代码对吗?您没有检查两个数组的内容是否正确?好吧,你应该这样做。
  • @SudhansuChoudhary “这可能会造成一些混乱,因为您在 while 循环中两次为 Name[k] 赋值。它可以正常工作” 不不。第二个赋值是错误的,它会引起麻烦。您可能会注意到,如果您在 while 循环之后检查 Name 数组的内容。
  • @Tom 你是对的!我编辑了我的答案 :-) 感谢您的提醒!
【解决方案2】:

Scanner 的 nextInt 不读取“换行”字符。
有两种方法可以解决它。
1.在input1.nextInt();之后调用input1.nextLine();忽略你在这里得到的东西,只是让它进入下一行。

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

2。致电input1.nextLine(); 查询成绩。 你得到的String 可以转换为int 并保存在Grade[k]。

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

【讨论】:

  • 还有另一种方式(我们称之为 2.5 :D):OP 可以使用Name[k]=input1.next() 而不是Name[k]=input1.nextLine();。这避免了上述问题,但每个名字都可以是名字或姓氏,因为next() 停止读取空格。
  • @Tom 是的。我不建议这样做,因为当我开始使用 java 时,我使用了next(); 并在不知道他的缺点的情况下继续使用它(读到空白)。我最终得到了答案,并且浪费了很多时间来弄清楚。无论如何,好主意:)
【解决方案3】:

这行得通:

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++;
    }
}

我建议您通过this question,您的疑问将得到澄清。摘自该帖子中给出的答案:

Scanner#nextInt 方法不会读取您输入的最后一个换行符,因此在下次调用 Scanner#nextLine 时会使用该换行符。

【讨论】:

    【解决方案4】:

    问题在于:Grade[k] = input1.nextInt(); 不读取行尾或数字后面的任何内容。

    尝试在Grade[k]=input1.nextInt(); 之后放置input1.nextLine(); 应该可以解决问题:

    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++;
    }
    

    【讨论】:

    • 即使使用 println 它仍然显示错误的输出。它将在最后一次输入后添加一个新行,它不能解决初始问题
    • 更有意义的哥们:-)
    • “尝试在 Grade[k]=input1.nextInt(); 之后放置 input1.nextLine(); 应该可以解决问题” 为什么?由于 OP 不知道问题出在哪里,这样的修复应该有一个解释,为什么它会起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 2020-02-27
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多