2017-03-26 2 views
-5

私はエラーを取得しています:悪いオペランドの型「*」

error: bad operand types for binary operator '*' 
if ((monthD * dayD).equals("yearD"))  
      ^
first type: String 
second type: String 

コードのポイントを掛けたときに月と日付が年等しいかどうかを確認することです。

// Month 
String monthD; 
System.out.print("What is the month? (For example, August would inserted as '08') "); 
monthD = userInput.nextLine(); 

// Day 
String dayD; 
System.out.print("What is the day? (For example, the 10th of the month is inserted as '10') "); 
dayD = userInput.nextLine(); 

// Year 
String yearD; 
System.out.print("What is the year? (For example, 1998 is inserted as '98') "); 
yearD = userInput.nextLine(); 

// Is this date magic? 
if ((monthD * dayD).equals("yearD")) 
{ 
    System.out.print("This date is magic!"); 
} 
else 
{ 
    System.out.print("This date is not magic!"); 
} 
+0

あなたはintをしたいときにあなたの入力をなぜ文字列として読むのですか? – Tom

+0

論理が間違っている場合は、最初に比較したいことを確認してください。 – Omore

+0

私はintとdoubleを試してみましたが、うまくいきませんでした。これは私が試した最後のオプションです。他に何をすべきですか? – MadCodez

答えて

0

あなたをカントは '*'(乗算)の目的のためにStringデータ型を持っている:

は、ここに私のコードです。 代わりにintタイプを使用してください。

+0

私は多くの異なるバリエーションを試しました。ストリングは私が最後に試したものでした。何が使えますか? – MadCodez

+0

使用 "int型 とあなたの 'もし' ループで、 '.euals(yearD')の周りに二重引用符を削除 – Nish

+0

または単に(monthD * dayD == yearD)場合{ 「 を使用.. .your code ...} else {... your code ...} " – Nish

0

文字列を整数に変換するには、Integer.parseInt(String)を使用する必要があります。真の整数でないかどうかを調べるtry/catchでサラウンド。そして何をでもしようとしています。ちょうどから日付を取得するためにjava.util.Dateから

+0

私はクラス内でこれほど遠くないとは思っていません。それは複雑で、犯罪でなければならないとは思いません。月x日=年であるかどうかを確認するだけです – MadCodez

+0

[これは私が作成した要点です](https://gist.github.com/ramidzkh/5c5a278941fbebe7d1221de878857ef4) – ramidzkh

関連する問題