Original: Okay so I have to make a simple number pyramid but the catch is that it has to use two methods. My problem is that return keeps giving me "Incompatible types" and I have no clue as to why. Okay so I have to make a simple number pyramid but the catch is that it has to use two methods. My problem is that return keeps giving me "Incompatible types" and I have no clue as to why.
public static void main(String[] args)
{
System.out.println(NumPyramid(1,1));
}
public static int NumPyramid(int i, int j)
{
for (;i <= 7; i++)
{
for (; j <= i; j++)
{
{
return System.out.print(j + " ");
}
}
}
Edit: okay so now my new code has the problem of not being a pyramid
public static void main(String[] args)
{
NumPyramid(1,1);
}
public static void NumPyramid(int i, int j)
{
for (;i <= 7; i++)
{
for (; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
}
this prints out
1 2 3 4 5 6 7
removing the Println gives 1 2 3 4 5 6 7
The output should be 1 12 123
etc,
System.out.print
is a void
method; that is, it returns nothing.
You can't return something from a void
method.
Simply remove the return
keyword from that line, change the signature of your method from int
to void
.
Then, change the call in your main method to remove the System.out.println
from it.
well, as @makoto points out cleverly, System.out.print
being a void
method, it returns nothing so:
public static void main(String[] args) {
System.out.println(NumPyramid(1,1));
}
should be changed as well. So you shall make:
public static void NumPyramid(int i, int j) {
for (;i <= 7; i++) {
for (; j <= i; j++) {
System.out.print(j + " ");
}
}
}
a void method, and :
public static void main(String[] args) {
NumPyramid(1,1);
}
not getting printed.
Edit
When you got a new question, you shall not edit your question, removing stuff in the question's post to make it into a new one… But instead accept the best answer, and make a new post. Here we are not answering only to you , but we are building a knowledge base. If you have a new question, make it a new post!
That said, for your new question, what your algorithm is off, it should instead be:
public static void NumPyramid(int max) {
for (int i=1; i<=max; ++i) {
for (int j=1; j<=i; ++j)
System.out.println(j + " ");
System.out.println();
}
}
max
to specify the number of lines, and the width of the "base" of the pyramid ; i
for max
carriage return output ; j
for i
numbers 0 1 2
for max = 3
but 1 2 3
which should output, with max = 3
1
1 2
1 2 3
HTH, again. And please, please, restore your original question.
You want to print this?
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
You need one argument, and it's the method:
public static void NumPyramid(int number)
{
for (int i = 1; i <= number; ++i)
{
for (int x = 1; x <= i; ++x)
{
System.out.print(x + " ");
}
System.out.println();
}
}
I think it's self-explanatory
I guess you're not asking about incompatible return types anymore? Well, I can answer the question you have now, I think.
If you want the code to be in a pyramid, you can't do this:
for (;i <= 7; i++)
{
for (; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
What that code is doing is printing the value of j, then a space, and then printing a new line. A solution is to create a String that you use to store the numbers after each iteration of the for loops.
for (;i <= 7; i++)
{
for (; j <= i; j++)
{
//System.out.print(j + " ");
//The string would take the place of this line
}
//Since println always prints on a new line, you
//could just print the string in this System.out
System.out.println();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.