212k views
1 vote
CHALLENGE ACTIVITY

7.2.2: While loops.

A while loop reads integers from input. Write an expression that executes the while loop until a negative integer is read from input.

Ex: If the input is 20 19 -11, then the output is:

Integer is 20
Integer is 19
Exit


import java.util.Scanner;

public class IntegerReader {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int in;

in = scnr.nextInt();

while (/* Your code goes here */) {
System.out.println("Integer is " + in);
in = scnr.nextInt();
}

System.out.println("Exit");
}
}

User Awojo
by
7.8k points

1 Answer

4 votes

Answer:

import java.util.Scanner;

public class IntegerReader {

public static void main(String[] args) {

Scanner scnr = new Scanner(System.in);

int in;

in = scnr.nextInt();

while (in >= 0) {

System.out.println("Integer is " + in);

in = scnr.nextInt();

}

System.out.println("Exit");

}

}

User Ed Haber
by
7.4k points