質問編集履歴

1

TN8001さんの修正内容で変更したところ次のようなエラーが出ました。TN8001さんのコードでも同じエラーが出ました。

2022/09/15 08:08

投稿

Biginner
Biginner

score0

test CHANGED
File without changes
test CHANGED
@@ -14,51 +14,73 @@
14
14
  ### 発生している問題・エラーメッセージ
15
15
 
16
16
  ```
17
- エラーメッセージ
18
- ```Syntax error, insert "}" to complete MethodBody
19
- Syntax error on token ".", @ expected after this token
20
- Syntax error, insert "SimpleName" to complete QualifiedName
21
- Syntax error, insert "Identifier (" to complete MethodHeaderName
22
- Syntax error, insert ")" to complete MethodDeclaration
17
+ Exception in thread "main" java.lang.NoSuchMethodError: 'void DrawCanvas.<init>(int)'
23
- Syntax error on token "}", delete this token
18
+ at Test2.main(Test1.java:29)
24
- Syntax error on token "}", delete this token
19
+ ```
25
20
 
26
21
  ### 該当のソースコード
27
22
 
28
23
  該当の言語
29
- Java
24
+ ```Java
30
-
31
- import javax.swing.JFrame;
25
+ import javax.swing.*;
32
- import javax.swing.JPanel;
33
26
  import java.awt.Color;
34
27
  import java.awt.Graphics;
35
28
  import java.util.Scanner;
36
29
 
37
- public class Test2 {
30
+ public class Test1 {
31
+
38
32
  public static void main(String[] args){
33
+ Scanner stdIn = new Scanner(System.in);
34
+ int n;
35
+
36
+ while (true) {
37
+ System.out.println("正何角形?(3-999):");
38
+ try {
39
+ n = stdIn.nextInt();
40
+ if (3 <= n && n <= 999) break;
41
+ } catch (Exception ignored) { stdIn.next(); }
42
+ }
43
+
44
+
45
+ double a = n;
46
+ double b = 180/a;
47
+ double m = Math.toRadians(b);
48
+ double l = a*(Math.sin(m));
49
+ System.out.println("正"+n+"角形の円周率の近似:"+l);
50
+
51
+
39
- GameWindow gw = new GameWindow("円と正多角形",1000,1000);
52
+ GameWindow gw = new GameWindow("円と正多角形",1000,1000);
40
- gw.add(new DrawCanvas());
53
+ gw.add(new DrawCanvas(n));
41
54
  gw.setVisible(true);
55
+
42
56
  }
43
57
  }
44
58
 
45
59
  class DrawCanvas extends JPanel{
60
+ private int n;
46
- public static void main(String[] args){
61
+ public DrawCanvas(int n) {
62
+ this.n = n;
63
+ SpinnerNumberModel model = new SpinnerNumberModel(n, 3,999, 1);
47
- static Scanner stdIn = new Scanner(System.in);
64
+ JSpinner spinner = new JSpinner(model);
48
- System.out.println("正何角形?:");
65
+ spinner.addChangeListener(e -> {
49
- static int n = stdIn.nextInt();
66
+ this.n = (int) spinner.getValue();
67
+ repaint();
68
+ });
69
+ add(spinner);
50
- }
70
+ }
71
+
72
+
73
+
51
- public void paintComponent(Graphics g) {
74
+ @Override public void paintComponent(Graphics g) {
52
75
  super.paintComponent(g);
53
-
54
-
76
+
55
77
  int x0 = 500;
56
78
  int y0 = 500;
57
79
  int r = 400;
58
80
  int x[] = new int[n];
59
81
  int y[] = new int[n];
60
82
 
61
- double delta = (360.0 / (double)n ) * ( Math.PI / 180.0);
83
+ double delta = 2 * ( Math.PI /(double)n );
62
84
 
63
85
  for(int i= 0; i<n; i++) {
64
86
  x[i] = (int)(Math.cos(-Math.PI/2.0 + delta * i)*r) + x0;
@@ -68,14 +90,11 @@
68
90
  g.drawPolygon(x, y, n);
69
91
 
70
92
  }
71
- }
72
-
73
- public void paint(Graphics g) {
93
+ public void paint(Graphics g) {
74
- super.paint(g);
94
+ super.paint(g);
75
- g.setColor(Color.blue);
95
+ g.setColor(Color.blue);
76
- g.drawOval(100, 100, 800, 800);
96
+ g.drawOval(100, 100, 800, 800);
77
97
  }
78
-
79
98
  }
80
99
 
81
100
  class GameWindow extends JFrame{
@@ -88,5 +107,8 @@
88
107
 
89
108
  }
90
109
  }
110
+ ```
111
+
112
+
91
113
 
92
114