首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扫描仪无法扫描

扫描仪无法扫描
EN

Stack Overflow用户
提问于 2012-02-17 13:26:42
回答 3查看 451关注 0票数 3

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

谢谢你的帮助。

代码语言:javascript
复制
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 ();
        }
    }
}

这是作文课

代码语言:javascript
复制
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 ("");
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-05-09 00:17:14

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

type = scan.next();

在类测试编写程序中:

代码语言:javascript
复制
  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()。在本例中,代码将如下所示

代码语言:javascript
复制
int age = scan.nextInt(); 
String sex = scan.next(); 
String job = scan.next(); 
int score = scan.nextInt();
票数 2
EN

Stack Overflow用户

发布于 2012-02-17 13:55:12

你的readQuestion函数应该是...

代码语言:javascript
复制
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();

票数 0
EN

Stack Overflow用户

发布于 2012-02-17 14:27:42

在您的TestWriter.main()方法中,您希望在以下代码的第3行中得到什么:

代码语言:javascript
复制
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.

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9323338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档