Java I with this java program that has to use do while loop?

Click For Summary
The discussion revolves around a Java program designed to print all odd numbers between two user-inputted integers. The initial code contains errors and does not fulfill the requirement of displaying all odd numbers before prompting the user for further action. Key points include the need for a loop that continuously asks the user for two numbers, checks for valid input, and correctly identifies and prints all odd numbers within the specified range. Additionally, the program should prompt the user after displaying the odd numbers to determine if they wish to repeat the operation. The structure of the program should include a do-while loop to facilitate this functionality. Overall, the focus is on correcting the existing code to meet the outlined requirements effectively.
aldofo
Messages
1
Reaction score
0
here is
Code:
import java.util.*;

public class oddNumbers


{
public static void main(String[] args){
System.out.println("enter two number");
Scanner kb=new Scanner(System .in);


int number=kb.nextInt();
int number2=kb.nextInt();
int s=number2/2;
String decision; 





while(number>number2)
{
System.out.println("Error enter input again");
number=kb.nextInt();
number2=kb.nextInt();
s=number2/2;

}

do{ 

System.out.println(number*2);
number++;



System.out.println("do you want to continue");

ddecisionkb.nextLine();





}while (decisionequals("yes")||decisionquals("Y…



}
}


this code asks the user every time that this program prints a odd number, but I want to print all the odd numbers and them ask the user.

this is what i have to do.
Write a program that asks the user to enter two numbers. The program should display all the odd numbers between the two numbers. The program will ask the user if he wishes to perform the operation again. If so, it should repeat, otherwise it should terminate
 
Technology news on Phys.org
Well, structurally your program should then look like
Code:
do {
  ask_for_interval_bound
  print_the_odd_numbers_within_this_interval
} while user_want_to_continue
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
1K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
3K
  • · Replies 14 ·
Replies
14
Views
6K