Ik heb een programma geschreven over de Gauss Jordan Eliminatie van matrices, daarbij heb ik de methods
try en catch geimplementeerd. Als er dus iets fout gaat doet hij alsnog wat ik wil in plaats van er mee stoppen.
Maar, in mijn code staan een aantal breuken. Wanneer er gedeeld wordt door 0 doet hij het gewoon nog... Hoe kan dit?
Dit is de code:
Anyone?
Ik ben nu ongeveer een maandje bezig met Java, ik leer het via YouTube: thenewboston/ , hij heeft
ongeveer 150 tutorials en heb er zo'n 100 gevolgd nu, vind het best leuk
Bij voorbaat dank
try en catch geimplementeerd. Als er dus iets fout gaat doet hij alsnog wat ik wil in plaats van er mee stoppen.
Maar, in mijn code staan een aantal breuken. Wanneer er gedeeld wordt door 0 doet hij het gewoon nog... Hoe kan dit?
Dit is de code:
Java: GaussJordan
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
| import java.util.Scanner; public class clean { public static void main(String[] args){ double a,b,c,d,e,f,g,h,i,j,k,l; int x=1; int counter=1; String MRX[][]={{"a","b","c","d"},{"e","f","g","h"},{"i","j","k","l"}}; System.out.println("Standard matrix layout: "); display(MRX); System.out.println(); do{ try{ System.out.println("Enter the values in alfabetical order ("+counter+")"); Scanner input = new Scanner(System.in); //Setting the values System.out.print("a = "); a = input.nextDouble(); System.out.print("b = "); b = input.nextDouble(); System.out.print("c = "); c = input.nextDouble(); System.out.print("d = "); d = input.nextDouble(); System.out.print("e = "); e = input.nextDouble(); System.out.print("f = "); f = input.nextDouble(); System.out.print("g = "); g = input.nextDouble(); System.out.print("h = "); h = input.nextDouble(); System.out.print("i = "); i = input.nextDouble(); System.out.print("j = "); j = input.nextDouble(); System.out.print("k = "); k = input.nextDouble(); System.out.print("l = "); l = input.nextDouble(); //Matrix which is going to be cleared double MRXX[][]={{a,b,c,d},{e,f,g,h},{i,j,k,l}}; System.out.println("Your matrix equals: "); display2(MRXX); //THE MATH PART (GAUSS ELIMINATION) b = b/a; c = c/a; d = d/a; a = a/a; f = f - (b*e); g = g - (c*e); h = h - (d*e); //e will now equal zero, the rest will be subtracted by e times the value above e = e - (a*e); //this is done last else all the other values will just be the same.. // The first column of the matrix now equals 1,0,0 j = j - (b*i); k = k - (c*i); l = l - (d*i); i = i-(a*i); g = g/f; h = h/f; f=f/f; c = c - (b*g); d = d - (h*b); b = 0; k = k - (g*j); l = l - (h*j); j = j - (f*j); l = l/k; k=k/k; h= h - (g*l); g = 0; d = d - (c*l); c = 0; System.out.println(); double MRX2[][]={{a,b,c,d},{e,f,g,h},{i,j,k,l}}; System.out.println("Your matrix after Gauss Jordan elimination: "); display2(MRX2); x++;} catch(Exception q){ System.out.println("You typed number.decimal, try using a comma instead."); counter++; }}while(x==1); } public static void display(String x[][]){ for(int row=0;row<x.length;row++){ for(int column=0;column<x[row].length;column++){ System.out.print(x[row][column]+"\t"); } System.out.println(); }} public static void display2(double y[][]){ for(int row=0;row<y.length;row++){ for(int column=0;column<y[row].length;column++){ System.out.print(y[row][column]+"\t"); } System.out.println(); } } } |
Anyone?
Ik ben nu ongeveer een maandje bezig met Java, ik leer het via YouTube: thenewboston/ , hij heeft
ongeveer 150 tutorials en heb er zo'n 100 gevolgd nu, vind het best leuk
Bij voorbaat dank