質問するログイン新規登録

質問編集履歴

1

2015/05/18 14:36

投稿

qwerty123
qwerty123

スコア26

title CHANGED
File without changes
body CHANGED
@@ -12,4 +12,44 @@
12
12
 
13
13
  そこで疑問に思ったのですが、mathクラスを使わずにjavaで平方根を求めることは可能でしょうか。
14
14
 
15
- よろしくお願いします。
15
+ よろしくお願いします。
16
+
17
+
18
+
19
+
20
+ 追記:
21
+ public class Square {
22
+ public static void main(String[] args) {
23
+
24
+ double square = 1; //最終的に2の平方根になる変数
25
+
26
+ double num = 1; //二乗の値の変数
27
+ while(true) {
28
+ square = num * num;
29
+
30
+
31
+ if(square > 2) {
32
+ num -= 0.0001;
33
+
34
+
35
+ }else{
36
+ num += 0.0001;
37
+ }
38
+
39
+
40
+ if( /*条件*/ ) {
41
+ break;
42
+ }
43
+ }
44
+
45
+ //少数第3位までを求める
46
+ double thousand = square * 1000;
47
+ double cast = (int)thousand;
48
+ double answer = cast / 1000;
49
+
50
+ System.out.println(answer);
51
+
52
+ }
53
+ }
54
+
55
+ 2の平方根を小数第三位まで求めるプログラムを作成しようと思いましたが、条件の定義がどうしてもよくわかりません。