您好我编写了我的代码并且所有代码都工作,除了Zero用作退出方式,我需要它不要在我的数组列表的末尾打印。
import java.util.*;
public class Question1
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int user_input;
List<Integer> number_list = new ArrayList<Integer>();
do
{
System.out.println("Enter an integer (0 to exit)");
user_input = keyboard.nextInt();
number_list.add(user_input);
}
while (user_input!=0);
System.out.println("Got it. You entered:");
for (int numbers : number_list)
{
System.out.println(numbers);
}
}
}
任何帮助将不胜感激,谢谢。
答案 0 :(得分:2)
更改
do
{
System.out.println("Enter an integer (0 to exit)");
user_input = keyboard.nextInt();
number_list.add(user_input);
}
while (user_input!=0);
到
do
{
System.out.println("Enter an integer (0 to exit)");
user_input = keyboard.nextInt();
if(user_input !=0)
number_list.add(user_input);
}
while (user_input!=0);
应该做的伎俩。这样,唯一要添加的值是非零值。
答案 1 :(得分:0)
我改变了你的代码以做你想做的事情:
public class HelloWorld {
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int user_input;
List<Integer> number_list = new ArrayList<Integer>();
do
{
System.out.println("Enter an integer (0 to exit)");
user_input = keyboard.nextInt();
number_list.add(user_input);
}
while (user_input!=0);
if (number_list.size() > 1) {
System.out.println("Got it. You entered:");
for (int i =0; i< number_list.size() -1 ;i ++)
{
System.out.println(number_list.get(i));
}
}
}
}
答案 2 :(得分:0)
do { System.out.println("Enter an integer (0 to exit)");
user_input = keyboard.nextInt();
if(user_input!=0){
number_list.add(user_input); }
}
while (user_input!=0);