java 在屏幕上打印 arraylist 元素但等待用户按 enter 键打印下一个元素
java print arraylist element on screen but wait user press enter to print next one
我正在编写一个 java 程序并尝试在屏幕上打印一个 arraylist 元素。但我想一次打印一个,然后等待用户按 Enter 键打印下一个。我应该如何修改以下代码?
import java.util.*
public class printonscreen{
public static void main(String args[]){
ArrayList<Integer> test = new ArrayList<Integer>();
test.add(0);
test.add(1);
test.add(2);
test.add(4);
for(int i=0; i<test.size(); i++){
System.out.print(test.get(i));
// wait user press enter
}
}
}
尝试:
System.in.read()
在 for 循环中的打印行之后
您可以像这样使用 JOptionPane:
import java.util.*
public class printonscreen{
public static void main(String args[]){
ArrayList<Integer> test = new ArrayList<Integer>();
test.add(0);
test.add(1);
test.add(2);
test.add(4);
for(int i=0; i<test.size(); i++){
System.out.print(test.get(i));
JOptionPane.showMessageDialog(null, "Press Ok to continue", "Alert", JOptionPane.ERROR_MESSAGE);
}
}
}
您可以通过更改属性来更改框中的图像JOptionPane.ERROR_MESSAGE
有五种风格:
- ERROR_MESSAGE
- INFORMATION_MESSAGE
- WARNING_MESSAGE
- QUESTION_MESSAGE
- PLAIN_MESSAGE
您通常会做什么来等待用户输入?如果你不知道,那就是 Scanner
。 Scanner
class的nextLine()
(实例)方法可以暂时阻塞运行线程,等待用户在控制台输入。
所以你应该这样做:
import java.util.*
public class printonscreen{
public static void main(String args[]){
ArrayList<Integer> test = new ArrayList<Integer>();
test.add(0);
test.add(1);
test.add(2);
test.add(4);
Scanner s = new Scanner (System.in);
for(int i=0; i<test.size(); i++){
System.out.print(test.get(i));
// wait user press enter
s.nextLine();
}
}
}
看到 nextLine()
部分了吗?
看看这是否有效:
import java.util.*;
public class printonscreen{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
ArrayList<Integer> test = new ArrayList<Integer>();
test.add(0);
test.add(1);
test.add(2);
test.add(4);
for(int i:test ){
System.out.print(i);
sc.nextLine(); // wait user to press enter
}
}
我正在编写一个 java 程序并尝试在屏幕上打印一个 arraylist 元素。但我想一次打印一个,然后等待用户按 Enter 键打印下一个。我应该如何修改以下代码?
import java.util.*
public class printonscreen{
public static void main(String args[]){
ArrayList<Integer> test = new ArrayList<Integer>();
test.add(0);
test.add(1);
test.add(2);
test.add(4);
for(int i=0; i<test.size(); i++){
System.out.print(test.get(i));
// wait user press enter
}
}
}
尝试:
System.in.read()
在 for 循环中的打印行之后
您可以像这样使用 JOptionPane:
import java.util.*
public class printonscreen{
public static void main(String args[]){
ArrayList<Integer> test = new ArrayList<Integer>();
test.add(0);
test.add(1);
test.add(2);
test.add(4);
for(int i=0; i<test.size(); i++){
System.out.print(test.get(i));
JOptionPane.showMessageDialog(null, "Press Ok to continue", "Alert", JOptionPane.ERROR_MESSAGE);
}
}
}
您可以通过更改属性来更改框中的图像JOptionPane.ERROR_MESSAGE
有五种风格:
- ERROR_MESSAGE
- INFORMATION_MESSAGE
- WARNING_MESSAGE
- QUESTION_MESSAGE
- PLAIN_MESSAGE
您通常会做什么来等待用户输入?如果你不知道,那就是 Scanner
。 Scanner
class的nextLine()
(实例)方法可以暂时阻塞运行线程,等待用户在控制台输入。
所以你应该这样做:
import java.util.*
public class printonscreen{
public static void main(String args[]){
ArrayList<Integer> test = new ArrayList<Integer>();
test.add(0);
test.add(1);
test.add(2);
test.add(4);
Scanner s = new Scanner (System.in);
for(int i=0; i<test.size(); i++){
System.out.print(test.get(i));
// wait user press enter
s.nextLine();
}
}
}
看到 nextLine()
部分了吗?
看看这是否有效:
import java.util.*;
public class printonscreen{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
ArrayList<Integer> test = new ArrayList<Integer>();
test.add(0);
test.add(1);
test.add(2);
test.add(4);
for(int i:test ){
System.out.print(i);
sc.nextLine(); // wait user to press enter
}
}