如何在输出持续运行时进行输入

huangapple go评论137阅读模式
英文:

How to take input while an output is constantly running

问题

以下是翻译的内容:

所以我实际上正在尝试制作一个类似于精确秒表的秒表应用程序,因此它需要接受输入,我想做的是,每当用户输入内容时,循环将会中断......

这是我的代码..........

public class Stop {
    public static void stopwatch() {
        Scanner sc = new Scanner(System.in);       

        System.out.println("在你想停止时输入任何内容");
        for(double i = 0; true; i+=0.5) {
            System.out.println(i);
          
            if (sc.hasNextLine()) {
                String input = sc.nextLine();
                if (!input.isEmpty()) {
                    System.out.println("停止");
                    System.out.println("你停止了");
                    break;
                }
            }

            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } 
        }        
    }
}

所以,我想要接受一个输入,但用户是否输入是可选的,如果他输入了,循环将会中断,否则将会继续...

我的输出应该是这样的

在你想停止时输入任何内容
0.0
0.5
1.0
1.5
停止
你停止了

我该如何做到这一点?

英文:

So I am actually trying to make a stopwatch application that acts like an exact stopwatch so it needs to take input so what I want to do is whenever the user enters something the loop will break.....

This is my code..........

public class Stop {
    public static void stopwatch() {
        Scanner sc = new Scanner(System.in);       

        System.out.println("Enter anything when you want to stop");
        for(double i = 0; true; i+=0.5) {
            System.out.println(i);
          
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } 
            
        }        
    }
}

So, I want to take an input but it should be optional whether the user enters or not if he enters the loop will break else it will continue...

My output should be like

Enter anything when you want to stop
0.0
0.5
1.0
1.5
stop
You stopped

How do I do that?

答案1

得分: 1

你可以这样做:

public static void stopwatch() {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("输入任何内容停止计时");

    for (double i = 0; true; i += 0.5) {
        try {
            if (!br.ready()) {
                System.out.println(i);
                Thread.sleep(500);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

如果你在IDE的控制台中运行它,你需要输入一些内容然后按下回车键来停止计时。不要使用Scanner,因为它会阻塞并等待输入。

英文:

You can do it this way:

    public static void stopwatch() {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter anything when you want to stop");

		for (double i = 0; true; i += 0.5) {
			try {
				if (!br.ready()) {
					System.out.println(i);
					Thread.sleep(500);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

You have to type something and press Enter if you're running it in IDE console.
Do not use Scanner as it blocks and waits for the input.

huangapple
  • 本文由 发表于 2020年5月29日 13:43:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/62079438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定