本文介绍了在控制台中有没有办法在按下 Enter 时阻止 java 开始新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果用户输入 7 * 4我希望它输出 7 * 4 = 28

For example, if the user enters 7 * 4I want it to output 7 * 4 = 28

代替

7*4
=28

我已经找了几个小时了,但没有找到任何东西.提前感谢您的帮助.

I've been looking for a couple hours and haven't found anything. Thanks for any help in advance.

public class RecursiveMultiplication
{
public static void main(String[] args)
{
    Console console = System.console();
    String input[] = new String[2];
    String multiplicand, multiplier;
    int product;

    while(true)
    {
        input = console.readLine("?> ").split("\\D+");
        multiplicand = input[0];
        multiplier = input[1];
        product = multiply(Integer.parseInt(multiplicand), Integer.parseInt(multiplier));

        console.printf(" = %d", product);

    }
}

public static int multiply(int multiplicand, int multiplier){
    if(multiplier == 0)
        return 0;
    if(multiplier % 2 == 0)
        return multiplicand + multiplicand + multiply(multiplicand, multiplier - 2);
    return multiplicand + multiply(multiplicand, --multiplier);
}

}

推荐答案

这是运行 Java 程序的控制台的限制.这并不容易,但您可以查看 Java Curses 库.就个人而言,我认为不值得麻烦.改为编写 Java Swing GUI 会更容易.

This is a limitation of the console your Java program is running on. It is not easy to get around, but you can take a look at the Java Curses Library. Personally, I don't think it is worth the hassle. It would be easier to write a Java Swing GUI instead.

这篇关于在控制台中有没有办法在按下 Enter 时阻止 java 开始新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 14:14