-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab1.java
More file actions
39 lines (36 loc) · 1.31 KB
/
Lab1.java
File metadata and controls
39 lines (36 loc) · 1.31 KB
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
import java.util.Scanner;
public class Lab1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter number a:");
ensureShort(scan);
final short a = scan.nextShort();
System.out.println("Enter number b:");
ensureShort(scan);
final short b = scan.nextShort();
System.out.println("Enter number n:");
ensureShort(scan);
final short n = scan.nextShort();
System.out.println("Enter number m:");
ensureShort(scan);
final short m = scan.nextShort();
final short C = 1;
double s = 0;
if (a <= C && C <= n) {
System.out.println("Error! Division by zero.");
} else {
for (short i = a; i <= n; i++) {
for (short j = b; j <= m; j++) {
s += (double) (i - j) / (i - C);
}
}
System.out.println("Result: " + s);
}
}
private static void ensureShort(Scanner scan) {
while (!scan.hasNextShort()) {
System.out.println("was in input buffer: \"" + scan.next() + "\"");
System.out.println("Please enter a number in the range -32768 to 32767 (not other symbol(s)).");
}
}
}