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