Question: My code creates an infinite loop and I can't figure out how to get it to sum only the odd numbers without creating an infinite
My code creates an infinite loop and I can't figure out how to get it to sum only the odd numbers without creating an infinite loop. the instructions for this problem is below.
Write an application that reads integers from the user until the user enters a -1. After each valid number entered by the user, the application should calculate and display the sum and average of all numbers entered to this point. The code below is what I have thus far.
import java.util.Scanner;
public class Integers { int sum= 0; public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Please enter an integer: "); int number1 = input.nextInt(); input = new Scanner(System.in); System.out.print("Please enter another value: "); int number2 = input.nextInt(); int i = number1; if (number1 % 2 != 0) { ++number1; } while (i <= number2) { sum +=i;
System.out.println(sum); while (i <= number2) { System.out.println(i); } i++; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
