Reputation: 2794
I am working on a simple JAVA question in one of my college courses. I am stumped on this one program. I will display what I have so far and give the question I have to answer. I also looked at a similar question on StackOverflow, BUT it isn't the same problem so it DIDN'T help. The program I need to write is:
Write a program that uses 'while' loops to perform the following steps:
a.) Prompt the user to input two integers: 'firstNum' and 'secondNum' (firstNum must be less than secondNum)
b.) Output all the odd numbers between 'firstNum' and 'secondNum' inclusive.
c.) Output the sum of all the even numbers between 'firstNum' and 'secondNum' inclusive.
This is what I have so far... (I still need to calculate the even numbers and sum them up)
//import classes
import java.util.*;
public class chapter5no9
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
//Part A
int firstNum;
int secondNum;
int sumEven;
System.out.println("Please enter an integer: ");
firstNum = console.nextInt();
System.out.println("Please enter another integer less than the first integer: ");
secondNum = console.nextInt();
//Part B
if (firstNum < secondNum)
{
System.out.print("Your second number is greater than the first. So Please re-enter: ");
secondNum = console.nextInt();
}
else
{
System.out.print("Odd Numbers: ");
firstNum++;
while (firstNum > secondNum)
{
if (secondNum % 2 != 0)
{
System.out.print(" " + secondNum);
}
secondNum++;
}
System.out.println();
System.out.print("Sum of Even Numbers: ");
firstNum++;
while (firstNum > secondNum)
{
if (secondNum % 2 != 0)
{
System.out.print(" " + secondNum);
}
secondNum++;
}
}
}
}
Upvotes: 2
Views: 12859
Reputation: 1
package hello.world;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number ;
int X = 0;
System.out.print("enter a number plz : ");
number = sc.nextInt();
while (X <= number){
System.out.println(X);
X ++;
}
}
}
Upvotes: 0
Reputation: 121
I created the loopCounter variable to handle the required iterations without changing the values inputted by the user. The following changes have been made to your code.
Part A: added a while loop to validate user input. Also changed logic in if statement.
Part B: used one loop to print odd numbers and total even numbers
//Part A
int firstNum;
int secondNum;
int sumEven=0;
System.out.println("Please enter an integer: ");
firstNum = input.nextInt();
System.out.println("Please enter another integer less than the first integer: ");
secondNum = input.nextInt();
//Part B
//validate input in a loop
while(true)
{
if (firstNum > secondNum)
{
System.out.print("Your second number is larger than the first. So Please re-enter: ");
secondNum = input.nextInt();
}
else
{
break;
}
}
System.out.print("Odd Numbers: ");
int loopCounter=firstNum;
while(loopCounter<secondNum)
{
if (loopCounter%2!=0)
{
System.out.print(" " + loopCounter);
}//end if
else
{
sumEven+=loopCounter;
}//end else
loopCounter++;
}
System.out.println();
System.out.print("Sum of Even Numbers: ");
System.out.print(sumEven);
}
Upvotes: 1
Reputation: 424983
I would separate the two concerns of checking input and calculating the result. Here's how I would calculate it:
int sum = IntStream.rangeClosed(firstNum, secondNum).filter(i -> i % 2 == 0).sum();
Upvotes: 1
Reputation: 4006
The issue is in your logic. You have this if statement:
if (firstNum < secondNum)
{
System.out.print("Your second number is greater than the first. So Please re-enter: ");
secondNum = console.nextInt();
}
Which checks if the second number is greater than the first (which you want it to be) and then asks them to re-enter. You want to check if (secondNum < firstNum)
. You will need to reverse all your while
and if
statements that compare secondNum
to firstNum
.
Then you have this if (secondNum % 2 != 0)
to check for odd numbers, which is correct, but you copy-pasted it to check for even numbers, which won't work, you will need to change that as well.
Then, you are outputting all the even integers inside your loop, when really you want to be adding it to an evenSum
variable, and outputting that at the end of the loop:
System.out.println("The sum of all even integers is: " + evenSum);
That should be enough to help you, I'm not going to write your homework for you, that's not what we do here.
Upvotes: 0