java - 如何修改程序运行直到输入某个键

标签 java

通过输入特定关键字或字母来终止此行为的最佳方法是什么?理想情况下希望程序通过输入一个键然后回车来终止

class NestedLoopTable {
    private int start;
    private int end;

    public NestedLoopTable() {
    }

    public NestedLoopTable(int aStart, int aEnd) {
        super();
        start = aStart;
        end = aEnd;
    }

    public void printTable() {
        // looping through the number of rows to print the table
        for (int i = start; i <= end - start + 1; i++) {
            for (int j = start; j <= i; j++)
                System.out.print(j + " ");
            System.out.println();
        }
    }
}
import java.util.Scanner;

public class NestedLoopTableApp {
    public static void main(String[] args) {

        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter start and end");
        int start = sc.nextInt();
        int end = sc.nextInt();

        NestedLoopTable np = new NestedLoopTable(start, end);
        np.printTable();
        sc.close();
    }
}

最佳答案

在输入特定键时终止程序的最佳方法是检查输入的键,如果它与程序终止时将输入的键匹配,则运行 System.exit(0 )

关于java - 如何修改程序运行直到输入某个键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60312995/

相关文章:

java - Spring :找不到资源

java - LibGDX:如何实现 Google Play 游戏服务?

java - 使用 .cer 文件签署 .jar 文件

使用异常的 Java 类

java - 我应该重写 Object.equals(Object) 方法吗?

java - 是否可以从 layout.xml android 中的 Activity 中获取变量的值?

java - Flying Saucer (xhtmlrenderer) 请求图像 4 次

java - Android与json解析

java - 如何设置谷歌驱动器下载文件的位置?

java - 下载后过期的临时 URL