Java:为什么这个代码不工作?无限循环? [英] Java: why is this code not working? Infinite loop?

查看:189
本文介绍了Java:为什么这个代码不工作?无限循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我可以从我的尝试中得知,我想知道如何让一个程序给用户5秒输入一些文本行,然后扫描仪将计算多少行输入。我刚刚开始学习Java作为我的第二语言,所以请尽量解释一切尽可能简单:)

So as you may be able to tell from my attempt, I'm trying to figure out how I'd make a program which gives the user 5 seconds to enter some lines of text, then the Scanner will count how many lines were entered. I've just started learning Java as my 2nd language, so please try to explain everything as simply as possible :)

我有两个理论,为什么它不是加工。第一个是nextLine()将返回整行,不管它是空的还是不是意味着而不是NL等于,它实际上将等于整行(即)。而我的第二个理论是,我不知道我在做什么,程序流是所有的地方。无论如何,这里是我的代码:

I've got two theories as to why it's not working. The first is that nextLine() will return the whole line, regardless of whether it's empty or not meaning rather than NL equaling "", it will actually equal the whole line (ie " "). And my second theory is that I've got no idea what I'm doing and the program flow is all over the place. Anyway, here's my code:

class OrigClass{
    public static void main(String args[]){
        Scanner ScanObj = new Scanner(System.in);
        int Count = 0;
        String NL = ScanObj.nextLine();
        try{
            Thread.sleep(5000);}
        catch (InterruptedException e){
            e.printStackTrace();
        }
        while (!NL.equals("")){
            Count++;
            NL = ScanObj.nextLine();
        }
        System.out.print("You Entered " + Count + " Lines.");
        ScanObj.close();
    }
}

哦,我忘了提到hasNext我最初尝试:

Oh, I forgot to mention hasNext() was what I originally tried:

       import java.util.Scanner;

    class OrigClass{
public static void main(String args[]){
    Scanner ScanObj = new Scanner(System.in);
    int Count = 0;
    try{
    Thread.sleep(5000);}
    catch (InterruptedException e){
        e.printStackTrace();
    }
    while (ScanObj.hasNext() == true){
    Count++;
    ScanObj.nextLine();
    }
    System.out.print("You Entered " + Count + " Lines.");
    ScanObj.close();
}
    }


推荐答案

这个解决方案不是真的好。但工作。

Well this solution is not really good. but works.

public class FiveSecond {
  public static void main(String args[]){
    new Thread(new Count(new Reader())).start();
  }
}
class Count implements Runnable{
  Reader r;Thread t;
  Robot ro;
  public Count(Reader t){this.r=t;
  try {
    ro=new Robot();
  } catch (AWTException e) {e.printStackTrace();}
  }
  @Override
  public void run() {
    t=new Thread(r);
    //t.setDaemon(true); //[S2]
    t.start();
    try{
    Thread.sleep(5000);
    }catch(Exception e){}
    t.interrupt();
    //Implicitly press the enter key in order to release the readLine() method :D
    //not recommended, and it's not a good idea, but works
    ro.keyPress(KeyEvent.VK_ENTER);
    ro.keyRelease(KeyEvent.VK_ENTER);
    /*
     * this is possible to save the strings lines in somewhere in order to access from invoker application
     * or send back the strings by socket, etc . . .
     */
    System.out.println("number of entered lines "+r.getCount()+"\n");
    //you would run this main as a process and get the number of counts 
    //System.exit(r.getCount()); //[S2]
  }
}

class Reader implements Runnable{
  private List<String> lines;
  private volatile int count;
  private BufferedReader br;
  public Reader(){
    lines=new ArrayList<String>();
    br=new BufferedReader(new InputStreamReader(System.in));
  }
  @Override
  public void run() {
    try{String line;
      System.out.println("you have 5 second to detect a 2048 length character, then your system will broken");
      while((line=br.readLine())!=null){
        if(!Thread.currentThread().isInterrupted()){
      count++;lines.add(line);}else{break;}
      }
      //for showing the lines entered
      //System.out.println(lines.toString());
    }catch(Exception ex){}
  }
  public int getCount(){return this.count;}  
}

但是最好的办法是运行一个独立的进程来计算行数,你只需删除 [S2] 注释来实现它。

but the best approach is about running a separated process to count the lines, and you would just remove the [S2] comments to achieve it.

这篇关于Java:为什么这个代码不工作?无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆