简体   繁体   English

扫描仪不会扫描

[英]scanner won't scan

I am having trouble coding a hw program that is made to generate test with multiple choice and essay questions. 我在编写一个用于生成具有多项选择和论文问题的测试的hw程序时遇到了麻烦。 Everything works except my program skips lines when it goes to read a part of the essay class. 一切都有效,除了我的程序在阅读论文课的一部分时跳过行。 I know it has to do with the scanner and scan.nextline, scan.nextInt and scan.next, etc but I am confused on how exactly to fix it. 我知道它与扫描仪和scan.nextline,scan.nextInt和scan.next等有关,但我对如何解决它感到困惑。

Thank you for your help. 谢谢您的帮助。

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 ();
        }
    }
}

here is the essay class 这是论文课

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 ("");
    }
}

Using scan.nextInt() will generate the following problems If your input is "5 5", nextInt() will get the next integer leaving the remaining " 5" of the buffer line. 使用scan.nextInt()将产生以下问题如果输入为“5 5”,则nextInt()将获得下一个整数,留下缓冲线的剩余“5”。 Of which the remaining " 5" will be caught by 剩下的“5”将被捕获

type = scan.next();

In the class test writer: 在班级测试作者:

  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();

This will generate the issue as i have mentioned above. 这将产生我上面提到的问题。

To fix this you can either 要解决此问题,您可以

a) Ensure that input is solely a number a)确保输入只是一个数字

b) Get the entire line like so String temp = scan.nextLine(); b)得到整行,如此String temp = scan.nextLine(); then convert it to a integer. 然后将其转换为整数。 This will you can play with the string and check if its the input you require ie if the 1st letter / set of numerical digits is an e/m or an integer. 这将使您可以使用字符串并检查它是否是您需要的输入,即第一个字母/数字组是e / m还是整数。

The problem with scan.nextInt() is that it only gets the next integer of the input line. scan.nextInt()的问题是它只获取输入行的下一个整数。 If there are spaces after the input it was taken from ie "5 5" it will grab only the next int 5 and leave " 5" behind. 如果在输入之后有空格,即从“5 5”获取它将仅获取下一个int 5并且留下“5”。

Thus i would recommend using scan.nextLine() and manipulating the string to ensure that the input can be handled and verified and at the same time ensuring that you do not get confused of where the scanner is at. 因此,我建议使用scan.nextLine()并操纵字符串以确保输入可以被处理和验证,同时确保您不会对扫描仪所处的位置感到困惑。

You should use .next() / .nextInt() if you are handling an input with various parameters you want to specifically catch such as "25 Male Student 1234" in this case the code would be as such 你应该使用.next()/ .nextInt(),如果你正在处理一个你要特别捕获的各种参数的输入,例如“25 Male Student 1234”,在这种情况下,代码将是这样的

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

Your readQuestion function should be ... 你的readQuestion函数应该是......

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

  question = scan.nextLine();

 }

It should be scan.nextLine(); 它应该是scan.nextLine(); to add an empty new line at the end 在最后添加一个空的新行

In your TestWriter.main() method what are you expecting at 3 line in following code: 在您的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.

the control flow will stuck at this point unless you enter something on the console. 除非您在控制台上输入内容,否则控制流将停留在此点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM