解析输入而不使用除System.in.read()之外的任何内容 [英] Parse input without using anything but System.in.read()

查看:99
本文介绍了解析输入而不使用除System.in.read()之外的任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到有关 System.in.read(); 如何运作的详细信息,也许有人可以帮助我。看起来像扫描仪会更好,但我不允许使用它。

I'm having a hard time finding details on how exactly System.in.read(); works, maybe somebody could help me out. Seems like Scanner would be preferable, but I'm not allowed to use it.

我得到了一个赋值,我应该以Boolean-Operator-Boolean的形式读取控制台用户输入,例如: T ^ F或T& T通过 System.in.read()并简单地打印语句返回的内容。

I was given an assignment where I'm supposed to read console user input in the form Boolean-Operator-Boolean, e.g. T^F or T&T via System.in.read() and simply print what the statement returns.

普通人可能会使用不同的方法,但该任务明确指出只有 System.in.read() System.out.println( )是允许的。

Normal people would probably use a different method, but the assignment specifically states that only System.in.read() and System.out.println() are allowed.

这是我尝试解决它:

import java.io.*;

public static void main(String[] args) {

  String error = "Reading error, please use T or F";

  boolean a = true;  //char: 84 or 116 for T and t
  boolean b = false; //char: 70 or 102 for F and f
  int userChar1;
  int userOperator;
  int userChar2;
  int chosenOperator = 0;

  try {

    //Get first char
    System.out.println("Enter the first value (T or F):");
    userChar1 = System.in.read();

    if((userChar1==84)||(userChar1==116)) { // T or t
      a = true;
    } else if ((userChar1==70)||(userChar1==102)) { // F or f
      a = false;
    } else {
      System.out.println(error);
    }


    //Get second char
    System.out.println("Select an operator:  &  |  ^");
    userOperator = System.in.read();

    if(userOperator==38) { // &
      chosenOperator = 0;
    } else if (userOperator==124) { // |
      chosenOperator = 1;
    } else if (userOperator==94) { // ^
      chosenOperator = 2;
    } else {
      System.out.println(error);
    }


    //Get third char 
    System.out.println("Enter the second value:");
    userChar2 = System.in.read();
    System.in.close();
    if((userChar2==84)||(userChar2==116)) {
      b = true;
    } else if ((userChar2==70)||(userChar2==102)) {
      b = false;
    } else {
      System.out.println(error);
    }


    //Figure out result
    boolean result;
    switch (chosenOperator) {
    case 0:
      result = a&b;   
    case 1:
      result = a|b;
    case 2:
      result = a^b;

      System.out.println(result);

    }

  } catch(IOException e) {

  }
}

执行此代码会使控制台在第一个 System.in.read()之后等待用户输入并正确检查char输入。然后,所有后续 System.in.read()都将被忽略,程序终止。

Executing this code makes the console wait for user input after the first System.in.read() and has it check the char input correctly. After that however, all succeeding System.in.read() are ignored and the program terminates.

我发现一段使用 System.in.close()的代码,所以在每个系统之后仍然不知道我拼接的方法究竟是什么。 in.read()。这导致程序在 System.in.close() System.in.read()时终止>被叫。

I found a piece of code that used System.in.close(), so still not knowing what exactly the methods do I spliced that in after every System.in.read(). This results in the programs termination when the first System.in.read() after a System.in.close() is called.

那么,到底发生了什么?你将如何正确使用 System.in.read()

So, what is going on exactly? How would you use System.in.read() correctly?

推荐答案

问题不在于System.in.read(),而在于控制台。
控制台通常是缓冲的,这意味着只有在按下回车键后才能将数据发送到您的程序(因此可由System.in.read()读取)。
所以你必须将控制台切换到无缓冲模式,但没有可移植的方法来执行此操作,因为有太多不同类型的控制台(unix shell,cmd窗口,Eclipse控制台等)。 br>
如果不允许使用除System.in.read()和System.out.println()方法之外的任何内容,则必须让用户在一行中输入完整的术语,按回车键并输入然后你可以处理输入的字符,例如:

The problem is not with System.in.read(), but with the console. The console is usually buffered, that means data is sent to your program (and thus readable by System.in.read()) only after the enter key is pressed. So you would have to switch the console to an unbuffered mode, but there is no portable way to do this, because there are so much different types of consoles (unix shell, cmd window, Eclipse console etc.).
If you are not allowed to use anything other than System.in.read() and System.out.println() methods, you have to let the user enter the complete term in a line, press enter and then you can process the characters entered, e.g:

public static void main(String[] args) throws IOException {
    boolean a = true; // char: 84 or 116 for T and t
    boolean b = false; // char: 70 or 102 for F and f
    int userChar1;
    int userOperator;
    int userChar2;

    System.out.println("Please enter the term, e.g. T&F:");

    userChar1 = System.in.read();
    userOperator = System.in.read();
    userChar2 = System.in.read();

    a = userChar1 == 'T' || userChar1 == 't';
    b = userChar2 == 'T' || userChar1 == 't';

    switch (userOperator) {
    case '&':
        System.out.println(a & b);break;
    case '|':
        System.out.println(a | b);break;
    case '^':
        System.out.println(a ^ b);break;
    default:
        System.out.println("unknow operator");
    }
}

这篇关于解析输入而不使用除System.in.read()之外的任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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