Question
This is java programming. I want to design a program which ask user to input some integers. However, user can even type alphabets and special
This is java programming. I want to design a program which ask user to input some integers. However, user can even type alphabets and special characters. Program will keep asking for inputs while input is not 'q' or 'Q'. When user input is 'q' or 'Q', then system should print the sum of even numbers, sum of odd numbers and product of even and odd numbers. All letters and special characters should be ignored. Modify the following code to meet all the requirements.
import java.util.Scanner;
public class Loop {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter numbers n letters, then q or Q to stop");
int input = sc.nextInt();
int odd, even;
while (input != 'q' || input != 'Q'){
if (isDigit(input)){
if (input% 2 == 0 ){
even += input;
}else{ odd += input;}
}
}
System.out.println("sum of odds is " + odd);
System.out.println("sum of evens is " + even);
System.out.println("their product is " + odd * even);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started