3861 Shares

Java, nested loops question?

Java, nested loops question? Topic: New pattern of writing application forms
May 26, 2019 / By Gay
Question: Write an application (i.e. java program) that displays the following patterns. * ** *** **** ***** ****** ******* Use loops to generate the patterns. You may need nested loops. All asterisks (*) should be printed by a single statement of the form System.out.print(‘*’); which causes the asterisks to print side by side. A statement of the form System.out.println(); can be used to position to the next line. The program should ask users to enter a positive integer from the keyboard, which determines the number of lines of asterisks (i.e. the size of the pattern). This is what I have so far, I think I'm on the right track but I'm not sure how to make the computer take a user-inputted value and repeat a pattern based on that number. I'm very confused. Any comments or help is appreciated import java.util.Scanner; public class Display public static void main(String[] args) { int numberOfIterations; System.out.println("Please enter a positive integer,"); System.out.println(" and this program will create a pattern."); Scanner keyboard = new Scanner(System.in); numberOfIterations = keyboard.nextInt(); if (numberOfIterations <= 0) { System.out.println("Error: Pleae enter a positive integer.") } else {
Best Answer

Best Answers: Java, nested loops question?

Debby Debby | 9 days ago
This looks like a homework question, so i'll try to give you a hint. if (numberOfIterations <= 0) { System.out.println("Error: Pleae enter a positive integer.") } else { for(int i = 0; i<=numberOfIterations; i++) { for(int j = numberOfIterations-1; j<=numberOfIterations; j++) { } } } That is actually more than just close, however there are some errors in this logic, figure it out for yourself. Remember that whenever you are given a problem break it down into smaller pieces. 1. you need to output enough lines specified by numberOfIterations the following loop will allow you to print one line per an iteration for(int i = 0; i<=numberOfIterations; i++) { } 2. You need to somehow increase the amount of stars after every line printed. This loop will allow you to print one star minus the number of times we have gone through the outer loop. Pass one numberOfItems - 1, Pass 2 numberOfItems - 2, etc. Everytime "i" will increase. for(int j = numberOfIterations-i; j<=numberOfIterations; j++) Hope that helps and good luck
👍 246 | 👎 9
Did you like the answer? Java, nested loops question? Share with your friends

We found more questions related to the topic: New pattern of writing application forms


Debby Originally Answered: Java? Enhanced for loops?
This is obviously homework :P I wouldn't use an enhanced For-loop for any of those, or anything involving an array unlike I wanted to box myself in for some reason :/

Britney Britney
I'll help you get started: for(int line = numberOfIterations; line > 0; line--) { //insert nested for loop System.out.println(); } This loop will print out a new line numberOfIternations times. Now you'll want a loop inside of that where you'll probably want to use System.out.print("*"). You should end up using the value of line in the nested for loop.
👍 100 | 👎 3

Britney Originally Answered: Help with Java program (if/while statement & loops)?
Any time you need a frequency, use an array of int[] the size of all the 'slots'. In the case of dice, that would be int[] rolls = new int[6]; Now we have 6 zeros, ready to start incrementing. import java.util.Random; public class DieFrequency2 { public static void main(String[] args) { int[] rolls = new int[6]; int count = 1000; while (count > 0) { rolls[ nextRoll() ]++; count--; } for (int i = 0; i < rolls.length; i++) { System.out.printf("%d rolls %d%n", (i+1), rolls[ i ]) ; } } private static int nextRoll() { Random r = new Random(); return r.nextInt(6); } } You do know that int[ x ] calls the slot of an array. Here, we use a random number to call that slot and ++ When we print it out, because arrays begin index at Zero, I add 1 to the slot for the human report, and call the value of the slot
Britney Originally Answered: Help with Java program (if/while statement & loops)?
While I will not solve the assignment for you, I will work through the concept of it. As you professor already said a while loop will work in this case. This is because an if statement will only check if it can run a block of code once. A while loop is like an if statement however once the code had been run, it will loop back to the top and check if it should run it again, hence "keep doing this while this is true." Note: a "for loop" would be the best choice for this, but you can look that up later. For general solution see pseudo code below... // Set up your counter counter = 0 timestorun = 1000 dicecount = new array[6] // 6 Sides while(counter < timestorun) // 0-999 = 1000 { // Choose a random number between 0 and 5. (0,1,2,3,4,5) = 6 sides side = rand 0-5 // Increment the dice side count dicecount[side]++ // Increment the loop count counter++ // At this point the code will loop back to the (counter < timestorun) // If counter is less than 1000 it will run the loop again // If counter is 1000 or greater it will skip the loop and move to the print below. } // After loop finishes print out the 6 side totals in the array // Remember that array[0] = side 1 and array[5] = side 6

If you have your own answer to the question new pattern of writing application forms, then you can write your own version, using the form below for an extended answer.