1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
import java.lang.*; //C#に合わせる為使用。普段は省略
class Rei11_1{
public static void main(String[] args){
GoukeiKeisan gk = new GoukeiKeisan();
gk.setOperand(1, 10);
gk.sum();
System.out.println("1〜10の和:" + gk.ans);
int ans = gk.sum(5, 10);
System.out.println("5〜10の和:" + ans);
}
}
class GoukeiKeisan{
int ope1;
int ope2;
int ans;
void setOperand(int a, int b){
ope1 = a;
ope2 = b;
}
void sum(){
ans = 0;
for(int i=ope1; i<=ope2; i++){
ans += i;
}
}
int sum(int a, int b){
ans = 0;
for(int i=a; i<=b; i++){
ans += i;
}
return ans;
}
}
|
using System;
class Rei11_1{
static void Main(){
GoukeiKeisan gk = new GoukeiKeisan();
gk.setOperand(1, 10);
gk.sum();
Console.WriteLine("1〜10の和:" + gk.ans);
int ans = gk.sum(5, 10);
Console.WriteLine("5〜10の和:" + ans);
}
}
class GoukeiKeisan{
public int ope1;
public int ope2;
public int ans;
public void setOperand(int a, int b){
ope1 = a;
ope2 = b;
}
public void sum(){
ans = 0;
for(int i=ope1; i<=ope2; i++){
ans += i;
}
}
public int sum(int a, int b){
ans = 0;
for(int i=a; i<=b; i++){
ans += i;
}
return ans;
}
}
|