126k views
0 votes
Create a class named Yourlastname_Yourfirstname_midterm. In this class, import java.util.Scanner;

1. Write a static method called problem 1 that keeps prompting the user for integer values. Calculate the sum of only two digit numbers. Return the sum when the user enters 0 Test case: if the input stream is 1, 20, 300, 40, 5, 600, and 0, problem 1() returns 60 (20+40=60)

User Bill Bonar
by
8.7k points

1 Answer

4 votes

Final Answer:

java

import java.util.Scanner;

public class Yourlastname_Yourfirstname_midterm {

public static void main(String[] args) {

problem1();

}

public static void problem1() {

Scanner scanner = new Scanner(System.in);

int sum = 0;

int num;

System.out.println("Enter integers (0 to stop):");

while (true) {

num = scanner.nextInt();

if (num == 0) break;

if (num >= 10 && num <= 99) {

sum += num;

}

}

System.out.println("Sum of two-digit numbers: " + sum);

}

}

Step-by-step explanation:

The class "Yourlastname_Yourfirstname_midterm" contains a static method "problem1()" that uses a Scanner to continuously prompt the user for integer inputs until 0 is entered. Within the method, it accumulates the sum of only two-digit numbers entered by the user by checking if the input falls within the range of 10 to 99.

If a two-digit number is provided, it adds it to the running total. Once the user enters 0, indicating the end of input, the method returns the sum of all two-digit numbers entered. In the given test case scenario (1, 20, 300, 40, 5, 600, 0), the method correctly computes and returns the sum of the two-digit numbers (20 and 40), which equals 60.