0

私はプログラミング初心者ですので、ご容赦ください。この質問に答えることができる既存のスレッドを検索しましたが見つかりませんでした。次のコードを記述しました。これは、セキュリティオブジェクトがユーザーによって株式または債券として識別されたかどうかに基づいて、stock.toString()またはbond.toString()の定型句を吐き出すことになっています。ただし、「セキュリティを解決できません」というコンパイラエラーが発生します。コンパイル時にセキュリティオブジェクトのクラスが定義されていないため、これが問題になると思います。本当?もしそうなら、反射法に頼らずにそれを回避する方法はありますか?ありがとうございました!

public static void main(String[] args) {

    double thePrice;
    double theShares;
    double theEarnings;
    double theRate;
    String securityType;

    Scanner in = new Scanner(System.in);

    System.out.println("Is it a stock or a bond?");
    securityType = in.nextLine();

    if (securityType.compareToIgnoreCase("stock") == 0) {
        System.out.println("Successfully set to STOCK");
        System.out.println("What are the earnings?");
        theEarnings = in.nextDouble();
        Stock security = new Stock();
        security.setEarnings(theEarnings);
    }

    else if (securityType.compareToIgnoreCase("bond") == 0) {
        System.out.println("Successfully set to BOND");
        System.out.println("What is the rate?");
        theRate = in.nextDouble();
        Bond security = new Bond();
        security.setRate(theRate);
    }

    System.out.println("What is the price");
    thePrice = in.nextDouble();     

    System.out.println("How many shares are there?");
    theShares = in.nextDouble();

    security.setPrice(thePrice);
    security.setShares(theShares);

    System.out.println(security);
}

@ Jigur Joshi、@ penartur、その他に感謝します。これが私たちが思いついた解決策ですが、より良い代替案があるかどうか私に知らせてください。そして、securityTypeが「stock」でも「bond」でもない場合にクリーンアップするelseステートメントを追加しています:)

public static void main(String[] args) {

                ...
    Security security = null;
    String securityType;

    Scanner in = new Scanner(System.in);

    System.out.println("Is it a stock or a bond?");
    securityType = in.nextLine();

    System.out.println("What is the price"); 
    thePrice = in.nextDouble();      

    System.out.println("How many shares are there?"); 
    theShares = in.nextDouble(); 

    if (securityType.compareToIgnoreCase("stock") == 0) {
        System.out.println("Successfully registered STOCK");
        security = new Stock();
        System.out.println("What are the earnings?"); 
        theEarnings = in.nextDouble(); 
        ((Stock) security).setEarnings(theEarnings); 
    }

    if (securityType.compareToIgnoreCase("bond") == 0) {
        System.out.println("Successfully registered BOND");
        security = new Bond();
        System.out.println("What is the rate?"); 
        theRate = in.nextDouble(); 
        ((Bond) security).setRate(theRate);
    }

    security.setPrice(thePrice); 
    security.setShares(theShares); 

        System.out.println(security); 

}
4

3 に答える 3

6

if else の後に使用できるように、if else の外側で宣言します。

Stockのスーパークラスであると仮定すると、次のようBondに宣言されていない場合security

 Object security = null;

成功する

 Stock security = null;
 if (securityType.compareToIgnoreCase("stock") == 0) {
        System.out.println("Successfully set to STOCK");
        System.out.println("What are the earnings?");
        theEarnings = in.nextDouble();
        security = new Stock();
        security.setEarnings(theEarnings);
    }

    else if (securityType.compareToIgnoreCase("bond") == 0) {
        System.out.println("Successfully set to BOND");
        System.out.println("What is the rate?");
        theRate = in.nextDouble();
        security = new Bond();
        security.setRate(theRate);
    }

見る

于 2012-06-29T05:03:43.683 に答える
4

問題は型ではありません。

あなたはsecurity内側のスコープで定義しましたが、それは単に外側には存在しません(security.setPrice.が「債券」でも「株式」でもないsecurity場合。securityType

しかし、あなたがやりたかったことはこれだと思います:

public static void main(String[] args) {

    double thePrice;
    double theShares;
    double theEarnings;
    double theRate;
    String securityType;

    Scanner in = new Scanner(System.in);

    System.out.println("Is it a stock or a bond?");
    securityType = in.nextLine();

    System.out.println("What is the price");
    thePrice = in.nextDouble();     

    System.out.println("How many shares are there?");
    theShares = in.nextDouble();

    if (securityType.compareToIgnoreCase("stock") == 0) {
        System.out.println("Successfully set to STOCK");
        System.out.println("What are the earnings?");
        theEarnings = in.nextDouble();
        Stock security = new Stock();
        security.setEarnings(theEarnings);
        security.setPrice(thePrice);
        security.setShares(theShares);
        System.out.println(security);
    }

    else if (securityType.compareToIgnoreCase("bond") == 0) {
        System.out.println("Successfully set to BOND");
        System.out.println("What is the rate?");
        theRate = in.nextDouble();
        Bond security = new Bond();
        security.setRate(theRate);
        security.setPrice(thePrice);
        security.setShares(theShares);
        System.out.println(security);
    }

}

もちろん、これはひどい解決策ですが、最初にタスクを明確にする必要があります。

于 2012-06-29T05:03:32.763 に答える
1

問題は変数の範囲にあります。securityif/elseブロック内でのみ定義されます。

コードを次のように変更できます。

Object security; //you can use common superclass of Bond and Stock (maybe Security?)

if (securityType.compareToIgnoreCase("stock") == 0) {
    /* ... */
    Stock stock = new Stock();
    stock.setEarnings(theEarnings);
    security = stock;
}

else if (securityType.compareToIgnoreCase("bond") == 0) {
    /* ... */
    Bond bond = new Bond();
    bond.setRate(theRate);
    security = bond;
}

/* ... */
System.out.println(security);
于 2012-06-29T05:08:20.827 に答える