【问题标题】:Adding colours and posting in rgb添加颜色并以 rgb 发布
【发布时间】:2014-09-24 11:33:44
【问题描述】:

我试图从一种颜色中获得红色,从另一种颜色中获得绿色,从另一种颜色中获得蓝色,然后将它们添加到全新的颜色中。但它一直说找不到第 2 行符号,现在它说我需要一个返回类型。我该怎么办?

 public static four (String[] args) {
       Color colour4 = new Color();
       int red = colour1.getRed();
       int green = colour2.getGreen();
       int blue = colour3.getBlue();


       System.out.println("The RGB is: ("+red+","+green+","+blue+")");
}

其他颜色的代码一般是这样的

public static void second (String[] args) {
      Scanner b = new Scanner (System.in);

      System.out.println("What's the decimal?");
      int value = b.nextInt();
      System.out.println("Your decimal is"+value);

      Color colour2 = new Color(value);
      int red = colour2.getRed();
      int green = colour2.getGreen();
      int blue = colour2.getBlue();

      System.out.println("The RGB is: ("+red+","+green+","+blue+")");
}

【问题讨论】:

  • 第 2 行是什么? colour1 是什么? colour2? colour3?
  • 这是我的全部代码
  • 哇,看起来好可怕
  • 这怎么不正确?
  • 考虑提供一个runnable example 来证明您的问题。这将导致更少的混乱和更好的响应

标签: java rgb


【解决方案1】:

你的代码完全错误:

  • 应该执行的方法始终命名为main,并且应该是void(您没有指定返回类型)。
  • 您的编译器出现问题的原因是您使用了一个变量 (colour1,您一开始没有声明。
  • 颜色实际上没有“十进制”。红色通道是 24 位整数的最高 8 位,绿色通道是中间的 8 位,蓝色通道是最低 8 位。

可能的解决方案:

public static void main (String[] args) {
    Scanner b = new Scanner (System.in);

    System.out.println("What's the first color decimal?");
    int value = b.nextInt();
    Color colour1 = new Color(value);

    System.out.println("What's the second color decimal?");
    value = b.nextInt();
    Color colour2 = new Color(value);

    System.out.println("What's the third color decimal?");
    value = b.nextInt();
    Color colour3 = new Color(value);

    int red = colour1.getRed();
    int green = colour2.getGreen();
    int blue = colour3.getBlue();

    Color colour4 = new Color(red,green,blue);

    //do something with colour4

    System.out.println("The RGB is: ("+red+","+green+","+blue+")"); 
}

【讨论】:

  • 好的,如果我想将前三种颜色显示为 RGB,我是否只需输入 system.out.print("The RGB is")?
  • 没有。 System.out.print(String.format("The RGB is: (%s,%s,%s)",colour1.getRed(),colour1.getGreen(),colour1.getBlue()));
猜你喜欢
  • 2021-10-06
  • 2022-11-13
  • 2022-12-19
  • 1970-01-01
  • 2012-04-07
  • 1970-01-01
  • 2020-08-25
  • 2012-12-13
  • 1970-01-01
相关资源
最近更新 更多