Question

Java: makes sure program compiles before posting. Write a program that uses nested for loops to...

Java: makes sure program compiles before posting.

Write a program that uses nested for loops to create the pattern of Xs and Os, in which on every line each letter is displayed one additional space to the right. Use a toggle variable ( a boolean flag) to alternate between X and O to produce output as shown in the sample run below:

Enter the number of lines: 7

X
O
X
O
X
O
X

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;
class Main{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
  
// taking user input
System.out.print("Enter the number of lines: ");
int n = sc.nextInt();
  
// looping for n times
for(int i=0;i<n;i++)
{
// printing spaces
for(int j=0;j<=i;j++)
{
System.out.print(" ");
}
  
// condition to print either x or 0
if(i%2==0)
System.out.println("X");
else
System.out.println("0");
}
}
}
/*SAMPLE OUTPUT
Enter the number of lines: 9
X
0
X
0
X
0
X
0
X

*/

Add a comment
Know the answer?
Add Answer to:
Java: makes sure program compiles before posting. Write a program that uses nested for loops to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program (name it SandClock) that uses nested loops to print out the following...

    Write a Java program (name it SandClock) that uses nested loops to print out the following shape (sand clock). * *** ***** ******* *********

  • Write a Java program that uses nested for loops to print a multiplication table as shown...

    Write a Java program that uses nested for loops to print a multiplication table as shown below. Make sure to include the table headings and separators as shown. The values in the body of the table should be computed using the values in the heading e.g. row 1 column 3 is 1 times 3. * | 1 2 3 4 5 6 7 8 9 4 | 1| 1 2 3 456 7 8 9 2 | 2 4 6.8...

  • Write a program that uses nested loops to collect data and calculate the average rainfall over...

    Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should prompt the user for the number of years. Be sure to ask for each months average rainfall for each year. The outer loop will iterate once for each year while the inner loop will iterate 12 times(one time per month) prompting for the months average rainfall on each iteration. Display the number of months, the total inches...

  • use  JOptionPane to display output Can someone please help write a program in Java using loops, not...

    use  JOptionPane to display output Can someone please help write a program in Java using loops, not HashMap Test 1 Grades After the class complete the first exam, I saved all of the grades in a text file called test1.txt. Your job is to do some analysis on the grades for me. Input: There will not be any input for this program. This is called a batch program because there will be no user interaction other than to run the program....

  • Java Programming. Please make sure program compiles, the code is copyable, and screenshots of the output...

    Java Programming. Please make sure program compiles, the code is copyable, and screenshots of the output are provided for 5 stars :-) Calculation for speed is just supposed to be google searched 1) 18 points] You've been hired by Pedal Punchers to write a Java console application that estimates bicycle speed in miles per hour. Use a validation loop to prompt for and get from the user the wheel diameter of a bicycle in inches in the range 10-50. Then...

  • c++ help please! Create a 2D character array in your main function and use nested for...

    c++ help please! Create a 2D character array in your main function and use nested for loops to fill the array with the letter ‘e’ to represent empty spaces. Create a function to print the board on the screen using a nested for loop. The function header is: void printBoard (char board [][3]) Create a function that checks whether a particular space has already been filled. If the space is filled it returns a boolean value of true, otherwise false....

  • .Need code for my java class !! Write a simple java program that uses loops. You...

    .Need code for my java class !! Write a simple java program that uses loops. You may use ‘WHILE’ or ‘FOR’ or ‘DO-WHILE’ – you decide. The program should use a loop to compute the semester average for up to 5 students. In the loop the user will ask for a student name and their 3 test scores and add them up and divide the total by 3 giving the semester average. You will print out the student name and...

  • Today you are to write a Java program that will prompt for and read 2 words...

    Today you are to write a Java program that will prompt for and read 2 words of equal length entered by the user, and create a new word which contains the last letter of the 1st word, the last letter of the 2nd word, followed by the second to last character of the 1st word, followed by the second to last character of the 2nd word and so on. Be sure to use the same format and wording as in...

  • Need help with implementing a While, Do-while and for loops in a program. UML: - scnr...

    Need help with implementing a While, Do-while and for loops in a program. UML: - scnr : Scanner - rows : int - MAX_ASCII : int ------------------------------------------------------------------------------- + CharacterTables() : + CharacterTables(rows : int) : - getStartingValue() : int -displayTable(startValue : int) : void - userContinue() : boolean + main(args : String[]) : void In Section 5.10 we saw that there is a close relationship between char and int. Variables of type char store numbers. (Actually, variables of all types...

  • write a java program that does the following Part one Use a For loop to compute...

    write a java program that does the following Part one Use a For loop to compute the sum of all the odd numbers from 1 through 99. Your result should be labeled, and the value should be 2500. Print your name 7 times using a While loop. String dogNames[ ] = {"Sam","Buster","Fido","Patches","Gromit","Flicka"}; Using the array defined here, print the values of the array vertically and horizontally using one For-Each loop. Reverse the logic (Nested loops: Indent text) so that the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT