Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi I started working on this do while loop and I am stuck on it.. I cannot conce

ID: 3551720 • Letter: H

Question

Hi I started working on this do while loop and I am stuck on it.. I cannot conceptualize what it is I need to do. Please help!!!

Also I need help with section E as well because I am really bad at loops because I cannot think of how it works in my head quickly enough.

For part D I dont know how to ask continuously and also I am not sure if the logic is there..

THANK YOU!! This is for my study guide for my in class programming exam for BlueJ thank you


public static void sectionD()

{

/*

* Using a do/while loop, write a code segment that keeps on asking the user to enter a

* positive integer from 1 to 10 and enter a negative integer to stop. If the user

* enters a negative integer stop the loop, and if the user enters a number that

* is not in the range 1-10, output the message: "Input not in the range." and ask

* the user for a number again.

*/

int input = 0;

Scanner console= new Scanner(System.in);

do

{

System.out.println("Enter a number in the range 1-10, or enter a negative number to stop:");

input =console.nextInt();

if(input <1 || input >10)

{

System.out.println("Input not in range.");

System.out.println("Enter a number in the range 1-10, or enter a negative number to stop:");

input =console.nextInt();

}

else if(input >=1 || input <=10)

  

System.out.println("Enter a number in the range 1-10, or enter a negative number to stop:");

input =console.nextInt();

} while ( input <0 );

  

}

  

public static void sectionE()

{   

/*

* Write a code segment that asks the user for an integer in the range 3-9.

* Your code should output a pattern of numbers as shown below.

* Use two nested for loops.

* If the user enters 5, the pattern is:

* 1

* 12

* 123

* 1234

* 12345

*   

* If the user enters 7, the pattern is:

* 1

* 12

* 123

* 1234

* 12345

* 123456

* 1234567

*/

  

  

}


}

Explanation / Answer

  1. import java.util.*;
  2. import java.io.*;
  3. class Ideone
  4. {
  5. Scanner console=new Scanner(System.in);
  6. int input;
  7. public void sectionD()
  8. {
  9. do
  10. {
  11. System.out.println("Enter a number in the range 1-10, or enter a negative number to stop:");
  12. input=console.nextInt();
  13. if(input==0 || input>10)
  14. {
  15.   System.out.println("Input not in the range.");
  16. continue;
  17. }
  18. else if(input<0)
  19. {
  20. System.out.println("Input is negative. Program stopping...");
  21. break;
  22. }
  23. else continue;
  24. }while(true);
  25. }
  26. public void sectionE()
  27. {
  28.   System.out.println(" Enter a number in the range 3-9 : ");
  29. input=console.nextInt();
  30. for(int i=1; i<=input; i++)
  31. {
  32. for(int j=1; j<=i; j++) System.out.print(j);
  33.   System.out.println();
  34. }
  35. }
  36. public static void main (String[] args)
  37. {
  38. Ideone in = new Ideone();
  39. in.sectionD();
  40. in.sectionE();
  41. }
  42. }
import java.util.*; import java.io.*; class Ideone { Scanner console=new Scanner(System.in); int input; public void sectionD() { do { System.out.println("Enter a number in the range 1-10, or enter a negative number to stop:"); input=console.nextInt(); if(input==0 || input>10) {   System.out.println("Input not in the range."); continue; } else if(input<0) { System.out.println("Input is negative. Program stopping..."); break; } else continue; }while(true); } public void sectionE() {   System.out.println(" Enter a number in the range 3-9 : "); input=console.nextInt(); for(int i=1; i<=input; i++) { for(int j=1; j<=i; j++) System.out.print(j);   System.out.println(); } } public static void main (String[] args) { Ideone in = new Ideone(); in.sectionD(); in.sectionE(); } } Success 0.09s 380608KB stdin :
5
11
-1
6

stdout :
Enter a number in the range 1-10, or enter a negative number to stop:
Enter a number in the range 1-10, or enter a negative number to stop:
Input not in the range.
Enter a number in the range 1-10, or enter a negative number to stop:
Input is negative. Program stopping...
6

stdout :
Enter a number in the range 1-10, or enter a negative number to stop:
Enter a number in the range 1-10, or enter a negative number to stop:
Input not in the range.
Enter a number in the range 1-10, or enter a negative number to stop:
Input is negative. Program stopping...
Enter a number in the range 3-9 :  1 12 123 1234 12345
123456
Enter a number in the range 3-9 :  1 12 123 1234 12345
123456
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote