Question: I need help with formatting with #1, 6, and 7. Here's my code: import java.util.Scanner; public class LoopConceptReview { public static void main(String[] args) {
I need help with formatting with #1, 6, and 7.
Here's my code:
import java.util.Scanner;
public class LoopConceptReview {
public static void main(String[] args)
{
int value = 0;
String evenOrOdd = "***";
String word = "***";
Scanner scan = new Scanner(System.in);
System.out.println("Loop Concept Review");
//#1 Using a while loop, write input validation loop that asks users to enter a positive value
System.out.println(" #1. Input validation: ");
while (value <=0) //Value is equal or less than zero
{
System.out.println("Positive values only: ");
value = scan.nextInt();
}
//#2 write for loop that displays all of the odd numbers, 1 through 19
System.out.print(" #2. Odd numbers: ");
for (int i = 1; i <= 20; i++) { // Displays only odd numbers between 1 and 19
if (i % 2 != 0){
System.out.print(i + " ");
}
}
//#3 write for loop that displays every fifth number, 0 through 50
System.out.print(" #3. Every fifth number: ");
for (int i = 5; i<51; i +=5) { // displays every number between 0 and 50
System.out.print(i + " ");
}
//#4 write for loop that counts down from 15 to 0
System.out.print(" #4. Count down: ");
for (int i = 15; i > -1; i--) { // displays a count down starting at 15
System.out.print(i + " ");
}
/*#5 write nested for loop that creates a triangle that looks like this
A
AA
AAA
AAAA
AAAAA
*/
System.out.println(" #5. Cool Triangle:");
for (int i=0; i<6; i++) {
for (int j=0; j
System.out.print("A");
}
System.out.println("");
}
/*#6 write while loop that asks the user for value
* and displays whether the value is even or odd until
* the user enters a 0. (called a sentinel value)
*/
System.out.println(" #6. Even or Odd: ");
while (value > 0)
{
System.out.println("Enter a number (0 to quit): ");
value = scan.nextInt();
if (value % 2 == 0 && value > 0)
{
System.out.println("The value is even");
}
else if (value > 0)
{
System.out.println("The value is odd");
}
}
/*#7 Using a do..while loop, ask the user for a word and
* display the word until the user enters either "stop"
* or "STOP" or "Stop" or "sTop" or "stoP".
* (hint: use the String method equalsIgnoreCase)
*/
System.out.println(" #7. Words!!");
do
{
System.out.println("Enter a word: ");
word = scan.next();
System.out.println("Your word is: " + word);
}
while (! word.equalsIgnoreCase("stop"));
System.out.println(" Completed the Loop Concept Review");
}
}
For the last line, do I include it inside the while loop or outside?
Also would the line be something to similar to this, System.out.println ("Correct." + value + "is a positive number.");
#1
This is my output -
#1. Input validation:
Positive values only:
-7
Positive values only:
0
Positive values only:
2
Expected output:
#1. Input validation: Positive values only. Please enter a value: -3 Positive values only. Please enter a value: 0 Positive values only. Please enter a value: 5 Correct. 5 is a positive number.
My issue with #6 and #7 are the same. How do I get the user input to stay on the same line and not type out into the next one?
#6
My Output
#6. Even or Odd:
Enter a number (0 to quit)
2
The value is even
Enter a number (0 to quit)
7
The value is odd
Enter a number (0 to quit)
0
Expected Output
#6. Even or Odd: Enter a value (0 to quit):13 13 is odd Enter a value (0 to quit):6 6 is even Enter a value (0 to quit):0
#7
My output
#7. Words!!
Enter a word:
hello
Your word is: hello
Enter a word:
hello
Your word is: hello
Enter a word:
stop
Your word is: stop
Expected output
#7. Words!! Enter a word:Hello The word is: Hello Enter a word:students The word is: students Enter a word:stop The word is: stop
Thanks
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
