【问题标题】:scanner won't scan扫描仪不会扫描
【发布时间】:2012-02-17 05:26:41
【问题描述】:

我在编写一个硬件程序时遇到问题,该程序用于生成包含多项选择和论文问题的测试。一切正常,除了我的程序在阅读论文课的一部分时会跳行。我知道它与扫描仪和 scan.nextline、scan.nextInt 和 scan.next 等有关,但我对如何修复它感到困惑。

感谢您的帮助。

import java.util.*;

public class TestWriter
{
    public static void main (String [] args)
    {
        Scanner scan = new Scanner (System.in);
        String type=null;
        System.out.println ("How many questions are on your test?");
        int num = scan.nextInt ();
        Question [] test = new Question [num];
        for (int i=0; i <num; i++)
        {
            System.out.println ("Question " + (i+1) + ": Essay or multiple choice question? (e/m)");
            type = scan.next ();
            scan.nextLine ();
            if (type.equals ("e"))
            {
                test [i] = new Essay ();
                test [i].readQuestion ();
            }
            if (type.equals ("m"))
            {
                test [i] = new MultChoice ();
                test [i].readQuestion ();
            }
        }

        for (int i=0; i <num; i++)
        {
            System.out.println ("Question " + (i+1)+": "+ type);
            test [i].print ();
        }
    }
}

这是作文课

public class Essay extends Question
{
    String question;
    int line;
    public void readQuestion ()
    {
        System.out.println ("How many lines?");
        line = scan.nextInt ();
        scan.next ();
        System.out.println ("Enter the question");
        question = scan.nextLine ();
    }
    public void print ()
    {
        System.out.println (question);
        for (int i=0; i <line; i++)
        System.out.println ("");
    }
}

【问题讨论】:

  • 你听说过缩进的用处吗?

标签: java


【解决方案1】:

使用scan.nextInt()会产生以下问题 如果您的输入是“5 5”,nextInt() 将获得下一个整数,留下缓冲区行的剩余“5”。其中剩下的“5”将被

type = scan.next();

在类测试编写器中:

  System.out.println("How many questions are on your test?");
  int num = scan.nextInt();
  Question[] test = new Question[num];  for(int i=0; i<num; i++)
  {
   System.out.println("Question " + (i+1) + ": Essay or multiple choice question? (e/m)");

    type = scan.next();

这将产生我上面提到的问题。

要解决此问题,您可以

a) 确保输入只是一个数字

b) 像String temp = scan.nextLine(); 那样获取整行,然后将其转换为整数。这将您可以使用字符串并检查它是否是您需要的输入,即第一个字母/数字集是 e/m 还是整数。

scan.nextInt() 的问题在于它只能获取输入行的下一个整数。如果输入后有空格,即“5 5”,它将仅抓取下一个 int 5 并将“5”留在后面。

因此,我建议使用 scan.nextLine() 并操作字符串以确保可以处理和验证输入,同时确保您不会混淆扫描仪的位置。

如果您正在处理带有各种您想要特别捕获的参数的输入,您应该使用 .next() / .nextInt(),例如“25 男学生 1234”,在这种情况下,代码就是这样

int age = scan.nextInt(); 
String sex = scan.next(); 
String job = scan.next(); 
int score = scan.nextInt();

【讨论】:

    【解决方案2】:

    你的readQuestion 函数应该是...

    public void readQuestion()
     {
      System.out.println("How many lines?");
      line = scan.nextInt();
      scan.nextLine();
      System.out.println("Enter the question");
    
      question = scan.nextLine();
    
     }
    

    应该是scan.nextLine();在末尾添加一个空的新行

    【讨论】:

      【解决方案3】:

      在您的 TestWriter.main() 方法中,您对以下代码的第 3 行有什么期望:

      System.out.println("Question " + (i+1) + ": Essay or multiple choice question? (e/m)");
      
      type = scan.next();
      scan.nextLine();     //LINE 3: What are you expecting user to enter over here.
      

      除非您在控制台上输入内容,否则控制流将停留在这一点上。

      【讨论】:

      • 我希望它能清除行并阻止代码使用 e 作文作为我作文的问题
      猜你喜欢
      • 2017-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-29
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      相关资源
      最近更新 更多