简体   繁体   English

返回不兼容的类型(java)

[英]return incompatible types (java)

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 删除Println会得到1 2 3 4 5 6 7

The output should be 1 12 123 etc, 输出应为1 12 123等,

System.out.print is a void method; System.out.print是一个void方法; that is, it returns nothing. 也就是说,它什么也不返回。

You can't return something from a void method. 您不能从void方法返回某些内容。

Simply remove the return keyword from that line, change the signature of your method from int to void . 只需从该行中删除return关键字,即可将方法的签名从int更改为void

Then, change the call in your main method to remove the System.out.println from it. 然后,在您的main方法中更改调用以从中删除System.out.println

well, as @makoto points out cleverly, System.out.print being a void method, it returns nothing so: 好吧,正如@makoto巧妙地指出的那样, System.out.print是一个void方法,它什么也不返回:

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();
    }
}
  • having a single argument max to specify the number of lines, and the width of the "base" of the pyramid ; 仅具有一个参数max来指定行数和金字塔“底”的宽度;
  • iterate using i for max carriage return output ; 使用i迭代以获取max回车输出;
  • iterate using j for i numbers 使用j代表i编号进行迭代
  • start iterations at 1, so we don't output 0 1 2 for max = 3 but 1 2 3 从1开始迭代,因此对于max = 3 ,我们不输出0 1 2 ,而是1 2 3

which should output, with max = 3 应该输出,最大= 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. 该代码正在执行的操作是先打印j的值,然后打印一个空格,然后打印新行。 A solution is to create a String that you use to store the numbers after each iteration of the for loops. 一种解决方案是创建一个String,用于在for循环的每次迭代之后存储数字。

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();
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM