【发布时间】:2014-11-26 17:03:25
【问题描述】:
我创建了一个 for 循环,我希望当用户输入负值并给出消息时循环中断。我不希望程序计算负值。
public class test1
{
public static void main(String[] args)
{
PrintStream output = new PrintStream(System.out);
Scanner input = new Scanner(System.in);
List<Integer> yolo = new ArrayList<Integer>();
double sum = 0;
output.println("Enter your integers\n" + "(Negative=sentinel)");
// add the values to your empty Array except the negative entries
for (int entry = input.nextInt(); entry < 0 ; entry = input.nextInt())
{
yolo.add(entry);
if (entry >= 0 )
{
sum += entry;
}
else {
output.println("Your list is empty");
}
}
我尝试使用 Outerloop:, 和 break outerloop;但即使是正整数,它也会打破循环。
【问题讨论】:
标签: java list if-statement for-loop break