簡體   English   中英

成員函數在JAVA中顯示錯誤(intelliJ IDEA)

[英]Member Functions showing error in JAVA (intelliJ IDEA)

我編寫了一個使用intelliJ IDEA編寫的程序。但是,我不知道所有功能(對象)都顯示錯誤提示。。1.無法解析方法“ MulCom(complex1)” 2.無法解析方法“ SumCom(complex1)” 3 。無法解析方法“ SubCom(complex1)”

代碼如下:

import java.util.Scanner;
public class complex1 {
    public static void main(String[] args) {
        complex1 arg1 = new complex1();
        complex1 arg2 = new complex1();
        arg1.input();
        arg1.show();
//  complex arg1;
        arg2.input();
        arg2.show();
        complex1 c = new complex1();
        System.out.println("Sum:");
        c.SumCom(arg2);
        c.show();
        System.out.println("Product:");
        c.MulCom(arg2);
        c.show1();
        System.out.println("difference:");
        c.SubCom(arg1);
        c.show();
    }
}

    class complex
    {
        double re, img;
        double a, b;

        complex() {
            re = 0;
            img = 0;
            a = 0;
            b = 0;
        }

        public void input() {
            System.out.println("Real:");
            Scanner re = new Scanner(System.in);
            System.out.println("Imagnary:");
            Scanner img = new Scanner(System.in);
        }

        public complex SumCom(complex arg1) {
            complex temp = new complex();
            temp.re = arg1.re + arg1.re;
            temp.img = arg1.img + arg1.img;
            return temp;
        }

        public complex SubCom(complex arg1) {
            complex temp = new complex();
            temp.re = arg1.re - arg1.re;
            temp.img = arg1.img - arg1.img;
            return temp;
        }

        public complex MulCom(complex arg1) {
            complex temp = new complex();
            temp.a = ((arg1.re) * (arg1.re)) - ((arg1.img) * (arg1.img));
            temp.b = ((arg1.re) * (arg1.img)) + ((arg1.re) * (arg1.img));
            return temp;

        }

        public void show() {
            System.out.println(re + "," + img + "i");
        }

        public void show1() {
            System.out.println(a + "," + b + "i");
        }
    }

我是JAVA的新手,所以我需要分配幫助。

您正在創建complex1實例,但是正在嘗試調用complex類中定義的方法。 因此,由於complex1沒有input()show()方法,您會遇到錯誤。

因此,如果您更改此設置:

complex1 arg1 = new complex1();
complex1 arg2 = new complex1();
. . .
complex1 c = new complex1();

對此:

complex arg1 = new complex();
complex arg2 = new complex();
. . .
complex c = new complex();

它會工作。

如@JFPicard所建議的,在這里使用更有意義和獨特的名稱會有所幫助。 您不會混淆。

另外,請注意,Java中的約定是類名以大寫字母開頭。 因此,這些類應命名為ComplexComplex1 但同樣,您應該給它們起更多不同的名稱。

SumCom采用一個復雜參數,該參數不能轉換為complex1。

同樣,arg1.input調用無效。

只需將arg1參數從complex1更改為complex。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM