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

質問編集履歴

2

文章の訂正

2019/11/02 09:18

投稿

nubba
nubba

スコア16

title CHANGED
File without changes
body CHANGED
@@ -9,9 +9,10 @@
9
9
  ### 発生している問題・エラーメッセージ
10
10
 
11
11
  ```
12
+ 3が入力されるまで何度も聞かれるようにするにはどうすれば良いですか?
13
+ 今の状態だと一度1~3の数字を押すとspeedShow()が一度起動後終了してしまいます。
12
- 修正中で
14
+ いません............
13
15
 
14
-
15
16
  修正点:クラスをCarとTestのみへ変更。
16
17
  ```
17
18
  ```java

1

文章の訂正

2019/11/02 09:18

投稿

nubba
nubba

スコア16

title CHANGED
File without changes
body CHANGED
@@ -9,38 +9,44 @@
9
9
  ### 発生している問題・エラーメッセージ
10
10
 
11
11
  ```
12
- 今の状態ではvoid speedUp(){とvoid speedDown(){でこのメソッドには
13
- コンストラクター名がありますと出てしまいます...
14
- なのでvoid speedUp(){とvoid speedDown(){のvoidを取るとコンストラクター名が
15
- ありますというのはなくなるのですが代わりにTestの方でメソッドspeedUp()は型speedUpで未定義です。となってしまいます(speedDownもです)。
16
- ```
12
+ 修正中です
17
13
 
18
14
 
15
+ 修正点:クラスをCarとTestのみへ変更。
16
+ ```
19
17
  ```java
18
+ import java.util.Scanner;
20
- public class speedDown {
19
+ public class Test{
21
- private int speed;
22
20
 
23
- void speedDown() {
21
+ public static void main(String[] args) {
24
- if(this.speed>0) {
25
- this.speed--;
26
- }
27
- }
28
22
 
29
- }
30
- ```
31
- ```java
32
- public class speedUp {
33
- private int speed;
23
+ Car no01=new Car();
24
+
25
+ System.out.println("スピードを上げるなら[1]、"
26
+ + "下げるなら[2]を入力([3]は終了)");
34
27
 
35
- void speedUp() {
28
+ Scanner sc=new Scanner(System.in);
36
- if(this.speed<80) {
29
+ int n=sc.nextInt();
37
- this.speed++;
38
- }
39
- }
40
30
 
31
+ if(n==1) {
32
+ //スピードup
33
+ no01.speedUp();
34
+ no01.speedShow();
41
35
 
36
+ }
37
+ if(n==2) {
38
+ //スピードDown
39
+ no01.speedDown();
40
+ no01.speedShow();
42
41
 
42
+ }
43
+ if(n==3) {
44
+ //終了
45
+ System.out.println("終了します");
46
+ }
47
+ sc.close();
43
48
  }
49
+ }
44
50
  ```
45
51
  ```java
46
52
  public class Car {
@@ -51,64 +57,35 @@
51
57
  Car(){
52
58
  this.speed=0;
53
59
  }
60
+
61
+ void speedDown() {
62
+ if(this.speed>0) {
63
+ this.speed--;
64
+ }
65
+ }
66
+
67
+ void speedUp() {
68
+ if(this.speed<80) {
69
+ this.speed++;
70
+ }
71
+ }
72
+
54
73
  void speedShow() {
55
74
  if(LIMIT>this.speed) {
56
75
  System.out.println("制限速度"+LIMIT+"km/h");
57
- System.out.println("現在速度"+speed);
76
+ System.out.println("現在速度"+this.speed+"km/h");
58
- System.out.println("制限速度内です(あと"+(LIMIT-this.speed)+"です)");
77
+ System.out.println("制限速度内です(あと"+(LIMIT-this.speed)+"km/hです)");
59
78
  }
60
79
  if(LIMIT==this.speed) {
61
80
  System.out.println("制限速度"+LIMIT+"km/h");
62
- System.out.println("現在速度"+speed);
81
+ System.out.println("現在速度"+this.speed+"km/h");
63
82
  System.out.println("制限速度限界");
64
83
  }
65
84
  if(LIMIT<this.speed) {
66
85
  System.out.println("制限速度"+LIMIT+"km/h");
67
- System.out.println("現在速度"+speed);
86
+ System.out.println("現在速度"+this.speed+"km/h");
68
87
  System.out.println("制限速度を"+(this.speed-LIMIT)+"km/hオーバーしています");
69
88
  }
70
89
  }
71
90
  }
72
-
73
- ```
74
-
75
-
76
-
77
-
78
-
79
-
80
- ```java
81
- import java.util.Scanner;
82
- public class Test{
83
-
84
- public static void main(String[] args) {
85
- speedUp s1=new speedUp();
86
- speedDown d1=new speedDown();
87
- Car no01=new Car();
88
-
89
- System.out.println("スピードを上げるなら[1]、"
90
- + "下げるなら[2]を入力([3]は終了)");
91
-
92
- Scanner sc=new Scanner(System.in);
93
- int n=sc.nextInt();
94
-
95
- if(n==1) {
96
- //スピードup
97
- s1.speedUp();
98
- no01.speedShow();
99
-
100
- }
101
- if(n==2) {
102
- //スピードDown
103
- d1.speedDown();
104
- no01.speedShow();
105
-
106
- }
107
- if(n==3) {
108
- //終了
109
- System.out.println("終了します");
110
- }
111
- sc.close();
112
- }
113
- }
114
91
  ```