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

  • Context: Java 
  • Thread starter Thread starter aldofo
  • Start date Start date
  • Tags Tags
    Java Loop Program
Click For Summary
SUMMARY

The discussion centers around a Java program that utilizes a do-while loop to print odd numbers between two user-defined integers. The initial code provided contains errors, such as incorrect variable names and logic flaws. The corrected approach requires the program to ask for two numbers, print all odd numbers within that range, and then prompt the user to decide whether to repeat the operation. Key corrections include proper handling of user input and ensuring the loop continues based on user confirmation.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of loops in Java, specifically do-while loops
  • Basic knowledge of user input handling in Java using Scanner
  • Familiarity with conditional statements in Java
NEXT STEPS
  • Review Java do-while loop syntax and usage
  • Learn about input validation techniques in Java
  • Explore methods for generating odd numbers within a range in Java
  • Investigate best practices for user interaction in console applications
USEFUL FOR

Java developers, programming students, and anyone interested in improving their skills in user input handling and loop constructs in Java applications.

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
 

Similar threads

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