質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

解決済

3回答

2538閲覧

class,interfeceまたはenumがありません

shou_hitotose

総合スコア66

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2018/11/08 06:46

編集2018/11/08 06:50

また質問を投稿させていただきます。なかなかプログラムが完成しないものですのですみません。例の如く自己紹介プログラムを書いているのですがまたエラーが出てしまいました。おそらくクラスを配置すればいいのでしょうがどこにどう配置すればよいのかがわかりませんでした。ご意見などをいただけると幸いです。

Java

1class DateOfBirth { 2 3 int birth_year; 4 int birth_month; 5 int birth_date; 6 boolean known; 7 8} 9class Myfriend { 10 11 String name; 12 DateOfBirth the_date_of_birth; 13 String phone_number; 14 15} 16class Person { 17 String name; 18 19 DateOfBirth the_date_of_birth; 20 String phone_number; 21 int n_of_friends; 22 Myfriend[] friend; 23 24} 25 26 public int year; 27 public int month; 28 public int day; 29 30 31 32class ex6_2 { 33 34 public static void main(String args[]) { 35 Person person_one; 36 Person person_two; 37 person_one = new Person(); 38 person_two = new Person(); 39 40 /* for protection of personal data, pseudo values are used */ 41 person_one.name = "A"; 42 person_one.the_date_of_birth = new DateOfBirth(); 43 person_one.the_date_of_birth.known = true; 44 Birth birth_year1=new Birth(); 45 46 birth_year1.birthday(2004,3,3); 47 48 49 person_one.phone_number = "080-1234-5678"; 50 51 person_two.name = "Adolf Brain Charles David Frederic John"; 52 person_two.the_date_of_birth = new DateOfBirth(); 53 person_two.the_date_of_birth.known = true; 54 Birth birth_year2=new Birth(); 55 birth_year2.birthday(2004,1,23); 56 person_two.phone_number = "090-8765-4321"; 57 58 /* person_one believes that the 'person_two' is a friend */ 59 person_one.n_of_friends = 1; 60 person_one.friend = new Myfriend[1]; 61 person_one.friend[0] = new Myfriend(); 62 person_one.friend[0].name = person_two.name; 63 person_one.friend[0].the_date_of_birth = new DateOfBirth(); 64 person_one.friend[0].the_date_of_birth.birth_year 65 = person_two.the_date_of_birth.birth_year; 66 person_one.friend[0].the_date_of_birth.birth_month 67 = person_two.the_date_of_birth.birth_month; 68 person_one.friend[0].the_date_of_birth.birth_date 69 = person_two.the_date_of_birth.birth_date; 70 person_one.friend[0].the_date_of_birth.known 71 = person_two.the_date_of_birth.known; 72 person_one.friend[0].phone_number = person_two.phone_number; 73 74 /* however, person_two does not think so. */ 75 person_two.n_of_friends = 0; 76 77 /* print the input data */ 78 print_personal_info(person_one); 79 print_personal_info(person_two); 80 } 81 static void print_personal_info(Person p) { 82 int i; 83 84 System.out.print("NAME: " + p.name + "\n"); 85 86 System.out.print("Number of friends = " + p.n_of_friends + "\n"); 87 for( i = 0; i < p.n_of_friends; i++ ) { 88 System.out.print("Name of friend " + i 89 + ": " + p.friend[i].name + "\n"); 90 if( p.friend[i].the_date_of_birth.known == true ) { 91 System.out.print("Date of Birth: " 92 + p.friend[i].the_date_of_birth.birth_year 93 + " " 94 + p.friend[i].the_date_of_birth.birth_month 95 + " " 96 + p.friend[i].the_date_of_birth.birth_date 97 + "\n"); 98 } else { 99 System.out.print(" Date of birth is unknown.\n"); 100 } 101 102} 103} 104 105 static void birthday(int year,int month,int day){ 106 System.out.print("Date of Birth: " + this.year 107 + " " + this.month 108 + " " + this.day + "\n"); 109} 110 111 112 113 114} 115 116

(実行結果)
ex6_2.java:26: エラー: class、interfaceまたはenumがありません
public int year;
^
ex6_2.java:27: エラー: class、interfaceまたはenumがありません
public int month;
^
ex6_2.java:28: エラー: class、interfaceまたはenumがありません
public int day;
^
エラー3個

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2018/11/08 06:47

エラーが出るなら、エラーメッセージを省略せずそのまま提示してください
shou_hitotose

2018/11/08 06:48

ああ、すみません。また忘れてしまいました。
madoka9393

2018/11/08 06:51

ひとつ前の質問でも指摘ありましたが、インデント揃えませんか?
shou_hitotose

2018/11/08 06:53

すみません。少しいじくってたらまた乱れてしまいました。すぐに修正します。
madoka9393

2018/11/08 06:54

IDEかエディタ使いましょう。 今回及び前回指摘いただいている括弧の対応なんかもハイライトで明示的になるため、ミスが少なくなります。
shou_hitotose

2018/11/08 06:56

テキストエディタというのを使っているのですがそれではダメでしょうか
shou_hitotose

2018/11/08 06:57

すみません。インデントについてのルールもよくわからないほど初心者なんです。
madoka9393

2018/11/08 07:06 編集

そういった面倒な部分をカバーしてくれるのがIDEやプログラミング向けのエディタのいいところです。 https://toiroha.jp/article/detail/54510 この他にも比較やまとめは色々ありますが、今後の学習や習熟のためにも、何かしら導入しておいた方が良いかと思われます。 (エディタ おすすめ プログラミング 等で検索すると色々出てきます)
shou_hitotose

2018/11/08 07:08

世の中便利ですね。だからプログラマーの方はなんのエディタを使っているのかを話したりするのですね。
shou_hitotose

2018/11/08 07:14

madoka9393さん、y_waiwaiさん初心者の私にここまで付き合ってくださりありがとうございました!
shou_hitotose

2018/11/08 07:14

最後にお礼申し上げます。
guest

回答3

0

ベストアンサー

Birthクラスなんてないのと

Java

1 2 public int year; 3 public int month; 4 public int day;

ここでは宣言できないのでエラー出てるかと思います。

※DateOfBirthがBirthであると仮定すると
下記の通り

Java

1class DateOfBirth { 2 int birth_year; 3 int birth_month; 4 int birth_date; 5 boolean known; 6 public void birthday(int year,int month,int day){ 7 System.out.print("Date of Birth: " + year 8 + " " + month 9 + " " + day + "\n"); 10 } 11} 12class Myfriend { 13 String name; 14 DateOfBirth the_date_of_birth; 15 String phone_number; 16} 17class Person { 18 String name; 19 DateOfBirth the_date_of_birth; 20 String phone_number; 21 int n_of_friends; 22 Myfriend[] friend; 23} 24 25class ex6_2 { 26 public static void main(String args[]) { 27 Person person_one; 28 Person person_two; 29 person_one = new Person(); 30 person_two = new Person(); 31 32 /* for protection of personal data, pseudo values are used */ 33 person_one.name = "A"; 34 person_one.the_date_of_birth = new DateOfBirth(); 35 person_one.the_date_of_birth.known = true; 36 DateOfBirth birth_year1=new DateOfBirth(); 37 38 birth_year1.birthday(2004,3,3); 39 40 41 person_one.phone_number = "080-1234-5678"; 42 43 person_two.name = "Adolf Brain Charles David Frederic John"; 44 person_two.the_date_of_birth = new DateOfBirth(); 45 person_two.the_date_of_birth.known = true; 46 DateOfBirth birth_year2=new DateOfBirth(); 47 birth_year2.birthday(2004,1,23); 48 person_two.phone_number = "090-8765-4321"; 49 50 /* person_one believes that the 'person_two' is a friend */ 51 person_one.n_of_friends = 1; 52 person_one.friend = new Myfriend[1]; 53 person_one.friend[0] = new Myfriend(); 54 person_one.friend[0].name = person_two.name; 55 person_one.friend[0].the_date_of_birth = new DateOfBirth(); 56 person_one.friend[0].the_date_of_birth.birth_year 57 = person_two.the_date_of_birth.birth_year; 58 person_one.friend[0].the_date_of_birth.birth_month 59 = person_two.the_date_of_birth.birth_month; 60 person_one.friend[0].the_date_of_birth.birth_date 61 = person_two.the_date_of_birth.birth_date; 62 person_one.friend[0].the_date_of_birth.known 63 = person_two.the_date_of_birth.known; 64 person_one.friend[0].phone_number = person_two.phone_number; 65 66 /* however, person_two does not think so. */ 67 person_two.n_of_friends = 0; 68 69 /* print the input data */ 70 print_personal_info(person_one); 71 print_personal_info(person_two); 72 } 73 static void print_personal_info(Person p) { 74 int i; 75 76 System.out.print("NAME: " + p.name + "\n"); 77 78 System.out.print("Number of friends = " + p.n_of_friends + "\n"); 79 for( i = 0; i < p.n_of_friends; i++ ) { 80 System.out.print("Name of friend " + i 81 + ": " + p.friend[i].name + "\n"); 82 if( p.friend[i].the_date_of_birth.known == true ) { 83 System.out.print("Date of Birth: " 84 + p.friend[i].the_date_of_birth.birth_year 85 + " " 86 + p.friend[i].the_date_of_birth.birth_month 87 + " " 88 + p.friend[i].the_date_of_birth.birth_date 89 + "\n"); 90 } else { 91 System.out.print(" Date of birth is unknown.\n"); 92 } 93 94 } 95 } 96}

投稿2018/11/08 07:00

rururu3

総合スコア5545

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

shou_hitotose

2018/11/08 07:02

ありがとうございます。課題となっていたのですが途方にくれていました。
shou_hitotose

2018/11/08 07:44

なんとか解決いたしました。ありがとうございます!
guest

0

当該箇所がどのクラスにも属していないためです。

あなたの質問を見ていると、もっと小さい規模のコードで練習するべきでないかと思います。

投稿2018/11/08 06:53

swordone

総合スコア20649

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

shou_hitotose

2018/11/08 06:58

すみません。課題となっているのです。(私自身初心者のレベルにしては高いと切実におもっているのですが・・)。やるしかないのです。
shou_hitotose

2018/11/08 07:00

どこのクラスに属させればいいかがわからないのです。
shou_hitotose

2018/11/08 07:03

自分でいうのも変ですがまったくそのとおりだと思います。
guest

0

}

static void birthday(int year,int month,int day){ System.out.print("Date of Birth: " + this.year + " " + this.month + " " + this.day + "\n");

}
}

まず、カッコの対応が取れてませんね


public int year;

public int month; public int day;

こいつがどのクラスにも入ってないためにエラーが出てますね
どこかのクラスに入れましょう

投稿2018/11/08 06:49

編集2018/11/08 06:55
y_waiwai

総合スコア87719

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

shou_hitotose

2018/11/08 06:51

わかりました。やってみます。
y_waiwai

2018/11/08 06:53

って、最後のカッコはクラスの閉じカッコ? インデントをちゃんと付けるようにしてくだされ。そうすればこういう対応がわかりやすくなります
shou_hitotose

2018/11/08 07:11

よくわからなかったのです。上の方にエディタを進められましたのでそれ入れてやりたいと思います。 最後までお付き合いいただきありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問