I just started learning Java at school for cse142, but I've known a bit of programming so am considerably ahead of the class and have been trying my own little projects. Normally if I run into a problem I just work around it because I figure I just haven't learned that yet, but this one bugs me enough to make a whole thread about it.
我刚刚开始在学校学习Java for cse142,但我已经知道了一些编程,所以我已经远远超过了课程并且一直在尝试我自己的小项目。通常情况下,如果我遇到问题,我只是解决它,因为我想我还没有学到这一点,但这个问题足以让我对它做出一个完整的线索。
I want generate the following input/output at console.
我想在控制台生成以下输入/输出。
Window size? (x, y) : 250, 200
The 250/200 would be user input, so for example, at first you see
250/200将是用户输入,因此,例如,首先你看到
Window size? (x, y) :
and you type "250" and hit enter and then you see
然后输入“250”然后按Enter键然后你会看到
Window size? (x, y) : 250,
and after typing "200" and hitting enter you get
输入“200”并按下输入后即可获得
Window size? (x, y) : 250, 200
and then it continues on with the program.
然后它继续该程序。
As a second question, I'm wondering if it's possible to have a user input an equation. I was thinking about storing what they type into a string, but that would just be text and would need to be transferred into a Java runnable equation.
作为第二个问题,我想知道是否可以让用户输入方程式。我正在考虑将它们键入的内容存储到字符串中,但这只是文本,需要转换为Java可运行的等式。
3 个解决方案
#1
You can use the Scanner class to get input from the user, like this:
您可以使用Scanner类从用户获取输入,如下所示:
import java.util.Scanner;
public class test
{
public static void main (String args[])
{
Scanner input = new Scanner(System.in);
System.out.print("Window size? (x, y) : ");
int xValue = input.nextInt();
System.out.print("\nWindow size? (x, y) : " + xValue + " , ");
int yValue = input.nextInt();
System.out.print("\nWindow size? (x, y) : " + xValue + " , " + yValue);
}
}
More on scanner here: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
有关扫描仪的更多信息,请访问:http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
#2
I believe that jLine can do what you're looking for. It's BSD licensed, so you're pretty free with how you can use it.
我相信jLine可以做你想要的。它是BSD许可的,所以你可以很*地使用它。
From the jline site:
来自jline网站:
It is similar in functionality to BSD editline and GNU readline. People familiar with the readline/editline capabilities for modern shells (such as bash and tcsh) will find most of the command editing features of JLine to be familiar.
它的功能类似于BSD editline和GNU readline。熟悉现代shell(例如bash和tcsh)的readline / editline功能的人会发现JLine的大多数命令编辑功能都很熟悉。
As for the second part of your question (user-entered equations), take a look at beanshell. For your purposes, it's as easy as:
至于问题的第二部分(用户输入的方程式),请查看beanshell。为了您的目的,它就像:
bsh.Interpreter i = new bsh.Interpreter();
String formula = "2 * 2;"
Object result = i.eval(formula);
You can do a lot more that that with beanshell; the formula can essentially be Java code, and you can put whatever objects you want into the interpreter's "context" for access by the script being evaluated.
你可以用beanshell做更多的事情;公式本质上可以是Java代码,您可以将所需的任何对象放入解释器的“上下文”中,以便被评估的脚本进行访问。
#3
Unfortunately, there isn't any support for what you're trying to do in just Java. You could print "Window size? (x,y): " via System.out, and read in text via BufferedReader/System.in but you don't have any control over what appears in the console as a result of user input. If you need that behavior, try and find a CURSES library; I can't recommend any personally.
不幸的是,没有任何支持你在Java中尝试做的事情。您可以通过System.out打印“窗口大小?(x,y):”,并通过BufferedReader / System.in读取文本,但是由于用户输入,您无法控制控制台中显示的内容。如果您需要这种行为,请尝试查找CURSES库;我个人不能推荐。
As for evaluating an equation, you could either parse the string you read in (look into building an abstract syntax tree of some sort). You could also use the string to construct a .java file, and compile and run it by invoking javac and java through the Process class, reading the result out via Process.getInputStream(); which is a pretty nasty solution, but would accept all one line java statements. Such a solution is predicated on the JDK being installed, the JRE alone does not supply javac.
至于评估方程式,您可以解析您读入的字符串(查看构建某种抽象语法树)。您还可以使用该字符串构造.java文件,并通过Process类调用javac和java来编译和运行它,通过Process.getInputStream()读取结果;这是一个非常讨厌的解决方案,但会接受所有一行java语句。这样的解决方案基于正在安装的JDK,仅JRE不提供javac。
#1
You can use the Scanner class to get input from the user, like this:
您可以使用Scanner类从用户获取输入,如下所示:
import java.util.Scanner;
public class test
{
public static void main (String args[])
{
Scanner input = new Scanner(System.in);
System.out.print("Window size? (x, y) : ");
int xValue = input.nextInt();
System.out.print("\nWindow size? (x, y) : " + xValue + " , ");
int yValue = input.nextInt();
System.out.print("\nWindow size? (x, y) : " + xValue + " , " + yValue);
}
}
More on scanner here: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
有关扫描仪的更多信息,请访问:http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
#2
I believe that jLine can do what you're looking for. It's BSD licensed, so you're pretty free with how you can use it.
我相信jLine可以做你想要的。它是BSD许可的,所以你可以很*地使用它。
From the jline site:
来自jline网站:
It is similar in functionality to BSD editline and GNU readline. People familiar with the readline/editline capabilities for modern shells (such as bash and tcsh) will find most of the command editing features of JLine to be familiar.
它的功能类似于BSD editline和GNU readline。熟悉现代shell(例如bash和tcsh)的readline / editline功能的人会发现JLine的大多数命令编辑功能都很熟悉。
As for the second part of your question (user-entered equations), take a look at beanshell. For your purposes, it's as easy as:
至于问题的第二部分(用户输入的方程式),请查看beanshell。为了您的目的,它就像:
bsh.Interpreter i = new bsh.Interpreter();
String formula = "2 * 2;"
Object result = i.eval(formula);
You can do a lot more that that with beanshell; the formula can essentially be Java code, and you can put whatever objects you want into the interpreter's "context" for access by the script being evaluated.
你可以用beanshell做更多的事情;公式本质上可以是Java代码,您可以将所需的任何对象放入解释器的“上下文”中,以便被评估的脚本进行访问。
#3
Unfortunately, there isn't any support for what you're trying to do in just Java. You could print "Window size? (x,y): " via System.out, and read in text via BufferedReader/System.in but you don't have any control over what appears in the console as a result of user input. If you need that behavior, try and find a CURSES library; I can't recommend any personally.
不幸的是,没有任何支持你在Java中尝试做的事情。您可以通过System.out打印“窗口大小?(x,y):”,并通过BufferedReader / System.in读取文本,但是由于用户输入,您无法控制控制台中显示的内容。如果您需要这种行为,请尝试查找CURSES库;我个人不能推荐。
As for evaluating an equation, you could either parse the string you read in (look into building an abstract syntax tree of some sort). You could also use the string to construct a .java file, and compile and run it by invoking javac and java through the Process class, reading the result out via Process.getInputStream(); which is a pretty nasty solution, but would accept all one line java statements. Such a solution is predicated on the JDK being installed, the JRE alone does not supply javac.
至于评估方程式,您可以解析您读入的字符串(查看构建某种抽象语法树)。您还可以使用该字符串构造.java文件,并通过Process类调用javac和java来编译和运行它,通过Process.getInputStream()读取结果;这是一个非常讨厌的解决方案,但会接受所有一行java语句。这样的解决方案基于正在安装的JDK,仅JRE不提供javac。