Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am currently puzzled on making a star pyramid.
My product needs to be able to create a pyramid with a user-inputted # of rows. So if I say 3, the pyramid would look like:
*
***
*****
When I enter 3, my pyramid (based on my code below) looks like:
*
***
*****
I'm having trouble making the spaces get deducted when going down a row. I seem to have other parts down accurately, so my question is, how do I deduct spaces for each following row by 1 after the initial amount of spaces? Could I get help on how to fix my code? (while using a while loop). Feel free to comment on any other parts of my code if it seems inaccurate though.
At the moment this is what my code looks like:
import java.util.Scanner;
public class AstPyramid{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("How tall do you want your pyramid to be? Rows: ");
int rowCounter = keyboard.nextInt();
int baseLength = 1;
int starsInRow = 1;
while (baseLength <= rowCounter){
int starCounter = 1;
int whiteSpace = rowCounter/2;
while (whiteSpace >= 0){
System.out.print(" ");
whiteSpace--;
}
while (starCounter <= starsInRow){
System.out.print("*");
starCounter++;
}
System.out.println();
baseLength++;
starsInRow=starsInRow+2;
}
}
}
Update: I updated this question many times and already have received an answer pertaining to it. Could this question be un-held now? I'm not sure if editing this post notifies anybody, but it is worth another try.
In your code, the first inner-loop that you are using to print the white space, always runs for rowCounter / 2 times. That is the problem. It should ideally start at that value, and keep decreasing for each iteration of the outer loop.
You could have a spacesPerRow variable and assign it an initial value of rowCounter / 2, outside the outer loop. Then on each outer-loop iteration, you can decrease it. And use this spacesPerRow in your first inner loop to print the spaces.
Your spacesPerRow should be decremented once for each iteration of the outer loop. An ideal place would be right after your first inner loop finishes.
To moderators & question down-voters:
I can't add comments, so I'll add my thoughts here. The first few lines of the question precisely mention the problem: "I want to do X, but my code is doing Y". And the OP is not able to figure out why.
I also believe the code doesn't include a lot of unnecessary parts either.
I am wondering why this is considered off-topic, put on hold, and has two down-votes.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Might be a simple question but, having two nested loops, if I execute a break over the outer one, will the inner one break immediately too; Or will it let it finish its execution, then break the outer one right after?
For example:
outer: for (int i=0; i<10; i++) {
for (int j=0; j<10; j++) {
if (j == 5) break outer;
System.out.println(j);
}
System.out.println("test");
}
Would this piece of code print numbers from 0 to 5 before it breaks the outer loop or would it print numbers from 0 to 10 instead?
Will the System.out.println("test"); execute too?
It will print numbers from 0 to 4.
5 not included, as the print is not executed when the condition is met (j==5). If you wish to include it, change the condition to (j>5).
Regarding your second question, no, "test" would never be printed. The labeled break finishes the outer loop (with every statement inside, including the inner loop) and ends your program's execution, by just printing:
0
1
2
3
4
Edit: tested and confirmed, above's the resultant output.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
i am trying to make java program that ask number from user and runs following calculation: 1*3*5*7*....*usergivendouble. I think for loop is best for this but not sure how to make such loop. i tried
for(double i=1;i<=n;i+=2)
{
n*= 2;
}
but it just never stops asking new number.
im new to java and all help is appreciated!
Assuming n is the user given number, increasing n inside the loop is the problem. You increase n inside the loop and also use it as the loop's end condition. This causes an infinite loop, as the loop's condition is never met.
You need to change the code to be:
double multiplyRestult=1;
for(double i=1;i<=n;i+=2)
{
multiplyRestult*= i;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
enter image description here
This is the code screenshot link.
I am edit many time but every time the output is not completely sorted.
Your problem is very simply: you are increasing k far too often within your code!
Meaning: you are already looping with k; so you got:
for (int k=0; k < a.length; k++) { // dont use hardcoded "9" here btw!
and then you have
k++ again in the loop body. You simply dont have to do that!
Meaning: your k is growing faster than it should. But as your loop stops when k reaches 9; you are not processing all elements in your array!
Plus: insertion sort doesn't work with iterating your array once!
You have to keep iterating until all elements are in their place! You really want to study/think more about this algorithm. You, not us that is!
And as said, dont use hard-coded limits for arrays. You already say once that your array should contain 10 elements. From there on, you should be using a.length only! Besides: only use one-letter names for loop counters and such things. "a" is a pretty bad name for an array; why dont you call it "numbers" for example.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
So I am making a JAVA program in eclipse which displays all the possible 3 digit positive numbers.I am using the stacking of for loops to do so.Here is the first time I tried it-
public class go {
public static void main(String[] args) {
for(int firstNum = 0;firstNum<10;firstNum++){
for (int secondNum = 0;secondNum<10;secondNum++){
for(int thirdNum = 0;thirdNum<10;thirdNum++){
System.out.println(firstNum+secondNum+thirdNum);
}}}}}
The answer to that was a mix of different 1 digit and 2 digit numbers which repeat in a loop.It was just a really weird mix of ordered 1 or 2 digit numbers which is NOT what I wanted.Try it out yourself in eclipse if you have the time.
But then I tried this code-
public class go {
public static void main(String[] args) {
for(int firstNum = 0;firstNum<10;firstNum++){
for (int secondNum = 0;secondNum<10;secondNum++){
for(int thirdNum = 0;thirdNum<10;thirdNum++){
System.out.println(firstNum+""+secondNum+""+thirdNum);
}}}}}
Notice that I added blank spaces between the variables in the println statement.
And this time it worked perfectly!!Giving me every possible 3 digit positive integer.
Why didn't the 1st block of code work and why did the 2nd work?
Is it the way println looks at variables or what?
Please Help.
When you add integers, as in firstNum+secondNum+thirdNum, you get the sum of these integers. That's what the + operator does for integers.
When you add String literals to the expression, as in firstNum+""+secondNum+""+thirdNum, the compiler performs String concatenation instead of addition.
In the first piece of code, the integers got added. Whereas in the second piece of code it is a string.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How can I make this output in Java in a for-loop? How can I make every line less "*" ?
Input:
enter number: 6
Java output:
******
*****
****
***
**
*
This seems a homework assignment, and it's nice to you to achieve this little goals to correct programming learning. As we're not here to make your homework, here you have few steps to guide you through:
Define a Scanner to ask user's input
Use Scanner to put user's input into a variable
Use this variable in a inverted for-loop (with loopCounter--)
for (int loopCounter = userInputVariable; loopCounter > 0; loopCounter --)
To repeat the * use:
StringUtils String repeated = StringUtils.repeat("*", loopCounter); or
for loop to repeat the char
for (int innerLoopCounter = 0; innerLoopCounter < loopCounter; innerLoopCounter ++)
Something like this. Please bear in mind that I don't know java.
for (int i = input; i > 0; i--) {
System.out.println(StringUtils.repeat("*", i));
}