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

In summary, this conversation is discussing how to create a program that asks the user to enter two numbers and then displays all the odd numbers between the two numbers. The program will also ask the user if they want to perform the operation again. If yes, the program will repeat, otherwise it will terminate.
  • #1
aldofo
1
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
  • #2
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
 

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

1. What is a do while loop in Java?

A do while loop is a control flow statement in Java that executes a block of code repeatedly until a specified condition is no longer true. Unlike a while loop, the code inside a do while loop will always be executed at least once, regardless of the initial condition.

2. How do I use a do while loop in my Java program?

To use a do while loop in your Java program, you will need to first declare and initialize any necessary variables, then write the code that you want to execute repeatedly inside the loop. The syntax for a do while loop in Java is: do { // code to be executed } while (condition);

3. What is the difference between a do while loop and a while loop?

The main difference between a do while loop and a while loop is that a do while loop will always execute the code inside the loop at least once, while a while loop may not execute at all if the initial condition is not met. Additionally, the condition for a do while loop is checked at the end of each iteration, while the condition for a while loop is checked at the beginning.

4. Can I use a do while loop for any type of task in my Java program?

Yes, a do while loop can be used for a variety of tasks in a Java program, such as iterating through an array or processing user input. However, it is important to ensure that the condition for the loop will eventually become false, otherwise the loop will continue to execute infinitely.

5. Are there any common mistakes to avoid when using a do while loop in Java?

One common mistake when using a do while loop is forgetting to update the variable(s) inside the loop, which can result in an infinite loop. It is also important to ensure that the condition for the loop will eventually become false, otherwise the loop will continue to execute indefinitely. Additionally, make sure to properly format and close the loop to avoid syntax errors.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
804
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
14
Views
991
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
2
Views
716
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top