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

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

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

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

Javaアプレット

Javaアプレットは、Webページに組み込まれて実行されるJavaアプリケーションの形式です。Java SE 11で廃止となりました。

Q&A

解決済

1回答

472閲覧

『実引数リストと仮引数リストが異なります』javaのコンパイルエラーを解決してほしい

wealthy_plonker

総合スコア6

Java

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

Javaアプレット

Javaアプレットは、Webページに組み込まれて実行されるJavaアプリケーションの形式です。Java SE 11で廃止となりました。

0グッド

0クリップ

投稿2019/11/28 11:28

###困っていること
ビュフォンの針という実験を描画するプログラムを書こうと思ったのですが、動作テストをしようにもエラーによりコンパイルできません。
該当ファイルは2つ目のbuffon_applet.javaで、与えている配列の長さが同じなのに『実引数リストと仮引数リストの長さが異なります』と出てしまいます。
どなたかご助力のほどお願いいたします。
お見苦しいコードで申し訳ありません。。。

該当コード

buffon_graphic.java

java

1import java.awt.*; 2 3class buffon_graphic extends Canvas{ 4 5 int each_margins = 5; 6 7 /* 0. needle_x 8 1. needle_y 9 2. cos_theta 10 3. sin_theta 11 4. touch 12 5. c 13 6. ever_N 14 7. which_d 15 8. calculated_pi 16 9. difference */ 17 double[] needle_info = new double[10]; 18 19 public void buffon_graphic(double[] needle_info){ 20 this.needle_info[0] = needle_info[0]; 21 this.needle_info[1] = needle_info[1]; 22 this.needle_info[2] = needle_info[2]; 23 this.needle_info[3] = needle_info[3]; 24 this.needle_info[4] = needle_info[4]; 25 this.needle_info[5] = needle_info[5]; 26 this.needle_info[6] = needle_info[6]; 27 this.needle_info[7] = needle_info[7]; 28 this.needle_info[8] = needle_info[8]; 29 this.needle_info[9] = needle_info[9]; 30 } 31 32 public void paint(Graphics g){ 33 int line_distance = (this.getHeight() - (2 * each_margins))/4; 34 int horizon = this.getWidth(); 35 36 for (int i=0; i<5; i++){ 37 g.setColor(Color.LIGHT_GRAY); 38 g.drawLine(0, i * line_distance + each_margins, horizon, i * line_distance + each_margins); 39 } 40 41 double needle_middle_x = this.needle_info[0] * this.getWidth(); 42 double needle_middle_y = this.needle_info[1] * line_distance + this.needle_info[7] * line_distance; 43 int needle_length = this.getWidth() * 20; 44 45 if (this.needle_info[4] == 0){ 46 g.setColor(Color.BLACK); 47 g.drawLine((int)needle_middle_x, (int)needle_middle_y, (int)needle_middle_x + (int)(needle_length * this.needle_info[2]), (int)needle_middle_y + (int)(needle_length * this.needle_info[3])); 48 49 g.setColor(Color.BLACK); 50 g.drawLine((int)needle_middle_x - (int)(needle_length * this.needle_info[2]), (int)needle_middle_y - (int)(needle_length * this.needle_info[3]), (int)needle_middle_x, (int)needle_middle_y); 51 } else { 52 g.setColor(Color.RED); 53 g.drawLine((int)needle_middle_x, (int)needle_middle_y, (int)needle_middle_x + (int)(needle_length * this.needle_info[2]), (int)needle_middle_y + (int)(needle_length * this.needle_info[3])); 54 55 g.setColor(Color.RED); 56 g.drawLine((int)needle_middle_x - (int)(needle_length * this.needle_info[2]), (int)needle_middle_y - (int)(needle_length * this.needle_info[3]), (int)needle_middle_x, (int)needle_middle_y); 57 } 58 } 59 60 public void update(Graphics g){ 61 paint(g); 62 } 63}

buffon_applet.java

java

1/* <applet width="300" height="200" code="buffon_applet.class"></applet>*/ 2import java.applet.*; 3import java.awt.*; 4import java.awt.event.*; 5import java.util.Random; 6import java.util.Scanner; 7 8public class buffon_applet extends Applet implements Runnable, ActionListener{ 9 Thread th; 10 Random rand = new Random(); 11 Scanner stdIn = new Scanner(System.in); 12 buffon_graphic bg; 13 int dt = 1; 14 int N; 15 Button button; 16 Label c_label, ever_N_label,calculated_pi_label_1, difference_label_1, calculated_pi_label_2, difference_label_2; 17 TextField textfield; 18 19 /* 0. needle_x 20 1. needle_y 21 2. cos_theta 22 3. sin_theta 23 4. touch 24 5. c 25 6. ever_N 26 7. which_d 27 8. calculated_pi 28 9. difference */ 29 double[] needle_info = new double[10]; 30 31 /* @see java.applet.Applet#init()*/ 32 public void init(){ 33 this.bg = new buffon_graphic(this.needle_info); 34 35 this.textfield = new TextField(); 36 this.button = new Button("reset"); 37 this.c_label = new Label(this.needle_info[5] + "touched", Label.CENTER); 38 this.ever_N_label = new Label(this.needle_info[6] + "thrown", Label.CENTER); 39 this.calculated_pi_label_1 = new Label("calculated pi", Label.CENTER); 40 this.difference_label_1 = new Label("difference", Label.CENTER); 41 this.calculated_pi_label_2 = new Label(String.format("%.10f", this.needle_info[8]), Label.CENTER); 42 this.difference_label_2 = new Label(String.format("%.10f", this.needle_info[9]), Label.CENTER); 43 44 Panel p = new Panel(); 45 p.setLayout(new GridLayout(4,2)); 46 p.add(textfield); 47 p.add(button); 48 p.add(c_label); 49 p.add(ever_N_label); 50 p.add(calculated_pi_label_1); 51 p.add(difference_label_1); 52 p.add(calculated_pi_label_2); 53 p.add(difference_label_2); 54 55 this.setLayout(new BorderLayout()); 56 this.add(p, BorderLayout.NORTH); 57 this.add(bg, BorderLayout.CENTER); 58 59 this.textfield.addActionListener(this); 60 this.button.addActionListener(this); 61 } 62 63 /* @see java.applet.Applet#init()*/ 64 public void start(){ 65 if (th == null){ 66 th = new Thread(this); 67 th.start(); 68 } 69 } 70 71 /* @see java.lang.Runnable#run()*/ 72 public void run(){ 73 while(th != null){ 74 while(this.needle_info[6] <= this.N){ 75 this.needle_info[6] += 1; 76 this.needle_info[0] = rand.nextDouble(); 77 this.needle_info[1] = rand.nextDouble(); 78 this.needle_info[7] = rand.nextInt(4); 79 double x = rand.nextDouble(); 80 double y = rand.nextDouble(); 81 this.needle_info[2] = x/Math.sqrt(x*x + y*y); 82 this.needle_info[3] = y/Math.sqrt(x*x + y*y); 83 if (this.needle_info[1] <= (this.needle_info[3] / 2)){ 84 this.needle_info[4] = 1; 85 this.needle_info[5] += 1; 86 } 87 this.needle_info[8] = this.needle_info[5] / this.needle_info[6]; 88 this.needle_info[9] = Math.PI - this.needle_info[8]; 89 90 this.bg.buffon_graphic(this.needle_info); 91 92 bg.repaint(); 93 try{ 94 Thread.sleep(dt); 95 } 96 catch (InterruptedException ex){ 97 } 98 } 99 } 100 } 101 102 public void actionPerformed(ActionEvent ev){ 103 if (ev.getSource() == this.button){ 104 this.needle_info[6] = 0; 105 this.needle_info[5] = 0; 106 } 107 if (ev.getSource() == this.textfield){ 108 this.N = Integer.parseInt(this.textfield.getText()); 109 } 110 } 111 112 public void paint(Graphics g){ 113 } 114 115 /* @see java.applet.Applet#stop()*/ 116 public void stop(){ 117 th = null; 118 } 119}

発生するエラー

buffon_applet.java:33: エラー: クラス buffon_graphicのコンストラクタ buffon_graphicは指定された型に適用できません。 this.bg = new buffon_graphic(this.needle_info); ^ 期待値: 引数がありません 検出値: double[] 理由: 実引数リストと仮引数リストの長さが異なります エラー1個

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

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

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

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

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

guest

回答1

0

ベストアンサー

public void buffon_graphic(double[] needle_info){

じゃなく、
public buffon_graphic(double[] needle_info){
なんでは

コンストラクタの記述になってないので、エラーが出ますねー

投稿2019/11/28 11:33

編集2019/11/28 11:40
y_waiwai

総合スコア87774

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

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

wealthy_plonker

2019/11/28 12:44

ありがとうございます! おかげさまでコンパイルに成功しました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問