質問編集履歴
2
質問の1部分が解決したため修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,54 +1,60 @@
|
|
1
1
|
電卓の1部分をつくっています。
|
2
2
|
現在、23,36,39,56行目でシンボルが見つからないというエラーが発生しています。(エラー箇所に★をつけています)
|
3
|
-
スコープの問題と感じているのですが、例えば23行目、数字と判定したのちに配列であるw_expression_fieldに追加するにはどのようにすればいいのでしょうか。
|
3
|
+
~~スコープの問題と感じているのですが、例えば23行目、数字と判定したのちに配列であるw_expression_fieldに追加するにはどのようにすればいいのでしょうか。~~
|
4
|
+
上記、if文の外で宣言することで解決しました。
|
5
|
+
56行目のエラーに取り組みたいと思います。
|
6
|
+
|
7
|
+
|
4
8
|
ご教授のほどよろしくお願いいたします。
|
5
9
|
|
10
|
+
|
6
11
|
import java.io.*;
|
7
12
|
import java.util.*;
|
8
13
|
|
9
14
|
class Test {
|
10
|
-
public static void main(String[] args){
|
11
|
-
ArrayList<String> w_expression_field = new ArrayList<String>();
|
12
|
-
|
13
|
-
while(true){
|
14
|
-
|
15
|
-
|
15
|
+
int w_judge_count = 0; // 奇数回、偶数回の判定に使う
|
16
|
-
boolean w_number_judge = true; //数字かどうかの判定に使う
|
17
|
-
boolean w_operand_judge = true; //演算子かどうかの判定に使う
|
18
16
|
|
19
|
-
|
17
|
+
public static void main(String[] args) {
|
20
|
-
while(w_number_judge){ //数字が入力されない限りループ
|
21
|
-
System.out.println("数字を入力してください");
|
22
|
-
|
18
|
+
ArrayList<String> w_expression_field = new ArrayList<String>();
|
23
|
-
String w_changed_input_number = w_input_number.readLine();//入力された文字の型をStringに変換
|
24
19
|
|
25
|
-
w_number_judge = isJudgeNumber(w_changed_input_number ); //falseが返ってこれば抜けれる
|
26
|
-
}
|
27
|
-
|
28
|
-
}
|
29
|
-
|
20
|
+
while (true) {
|
30
21
|
|
22
|
+
int w_judge_count = 0; // 奇数回、偶数回の判定に使う
|
23
|
+
boolean w_number_judge = true; // 数字かどうかの判定に使う
|
24
|
+
boolean w_operand_judge = true; // 演算子かどうかの判定
|
25
|
+
String w_changed_input_number = " ";
|
26
|
+
String w_changed_input_operand = " ";
|
31
27
|
|
32
|
-
|
28
|
+
if (w_judge_count % 2 == 0) { // 偶数回なら数字か判定
|
33
|
-
|
29
|
+
while (w_number_judge) { // 数字が入力されない限りループ
|
34
|
-
|
30
|
+
System.out.println("数字を入力してください");
|
35
|
-
|
31
|
+
BufferedReader w_input_number = new BufferedReader(new InputStreamReader(System.in));
|
36
|
-
|
32
|
+
w_changed_input_number = w_input_number.readLine();// 入力された文字の型をStringに変換
|
37
|
-
|
38
33
|
|
39
|
-
|
34
|
+
w_number_judge = isJudgeNumber(w_changed_input_number); // falseが返ってこれば抜けれる
|
40
|
-
|
35
|
+
}
|
41
|
-
}
|
42
|
-
if(w_changed_input_operand.equals("=")){//★
|
43
|
-
break;
|
44
|
-
}
|
45
|
-
w_expression_field.add(w_changed_input_operand);//入力された値を代入 ★
|
46
|
-
|
47
|
-
|
48
|
-
}
|
49
|
-
//System.out.println("ループから抜け出しました");
|
50
|
-
}
|
51
36
|
|
37
|
+
}
|
38
|
+
w_expression_field.add(w_changed_input_number);// 入力された値を代入
|
39
|
+
|
40
|
+
if (w_judge_count % 2 != 0) { // 奇数回なら演算子か判定 //else ifでいけるか要検討
|
41
|
+
while (w_operand_judge) {
|
42
|
+
System.out.println("演算子を入力してください");
|
43
|
+
BufferedReader w_input_operand = new BufferedReader(new InputStreamReader(System.in));
|
44
|
+
w_changed_input_operand = w_input_operand.readLine();// 入力された文字の型をStringに変換
|
45
|
+
|
46
|
+
w_operand_judge = isJudgeOperand(w_changed_input_operand); // falseが返ってこれば抜けれる
|
47
|
+
}
|
48
|
+
}
|
49
|
+
if (w_changed_input_operand.equals("=")) {
|
50
|
+
break;
|
51
|
+
}
|
52
|
+
w_expression_field.add(w_changed_input_operand);// 入力された値を代入
|
53
|
+
|
54
|
+
}
|
55
|
+
// System.out.println("ループから抜け出しました");
|
56
|
+
}
|
57
|
+
|
52
58
|
public static boolean isJudgeNumber(String p_number) {
|
53
59
|
try {
|
54
60
|
Integer.parseInt(p_number);
|
1
インデントの整形
title
CHANGED
File without changes
|
body
CHANGED
@@ -5,8 +5,9 @@
|
|
5
5
|
|
6
6
|
import java.io.*;
|
7
7
|
import java.util.*;
|
8
|
+
|
8
|
-
class Test{
|
9
|
+
class Test {
|
9
|
-
|
10
|
+
public static void main(String[] args){
|
10
11
|
ArrayList<String> w_expression_field = new ArrayList<String>();
|
11
12
|
|
12
13
|
while(true){
|
@@ -38,7 +39,7 @@
|
|
38
39
|
w_operand_judge = isJudgeOperand(w_changed_input_operand ); //falseが返ってこれば抜けれる
|
39
40
|
}
|
40
41
|
}
|
41
|
-
if(w_changed_input_operand.equals("=")){
|
42
|
+
if(w_changed_input_operand.equals("=")){//★
|
42
43
|
break;
|
43
44
|
}
|
44
45
|
w_expression_field.add(w_changed_input_operand);//入力された値を代入 ★
|
@@ -47,26 +48,25 @@
|
|
47
48
|
}
|
48
49
|
//System.out.println("ループから抜け出しました");
|
49
50
|
}
|
50
|
-
public static boolean isJudgeNumber(String p_number){
|
51
|
-
try{
|
52
|
-
Integer.parseInt(p_number);
|
53
|
-
return false;
|
54
|
-
}catch(NumberFormatException e){
|
55
|
-
return true;
|
56
|
-
}
|
57
|
-
}
|
58
51
|
|
52
|
+
public static boolean isJudgeNumber(String p_number) {
|
53
|
+
try {
|
54
|
+
Integer.parseInt(p_number);
|
55
|
+
return false;
|
56
|
+
} catch (NumberFormatException e) {
|
57
|
+
return true;
|
58
|
+
}
|
59
|
+
}
|
59
60
|
|
60
|
-
|
61
|
+
public static boolean isJudgeOperand(String p_operand) {
|
61
|
-
|
62
|
+
if (w_judge_count < 3 && p_operand.equals("=")) { // 最初に=を入力されないように ★
|
62
|
-
|
63
|
+
return true;
|
63
|
-
|
64
|
+
} else if (p_operand.equals("+") || p_operand.equals("-") || p_operand.equals("*") || p_operand.equals("/")) { // 演算子ならfalseを返し、ループを抜ける
|
64
|
-
|
65
|
+
return false;
|
65
|
-
|
66
|
+
} else {
|
66
|
-
|
67
|
+
return true;
|
67
|
-
|
68
|
+
}
|
68
|
-
|
69
|
+
}
|
69
|
-
|
70
|
-
|
70
|
+
|
71
71
|
}```
|
72
72
|
```
|