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
- 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();
- }
- }
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.