Консультация № 188394
10.12.2015, 00:28
0.00 руб.
0 1 1
Здравствуйте! Прошу помощи в следующем вопросе:
нужно перевести программу с паскаль на java. Благодарю
uses crt;
const n=7; m=7;
var i,j: integer;
U: array [0..m,0..n] of real;
x: array [0..m] of real;
t: array [0..n] of real;
ht,hx,t0,tk,10,1k: real;
begin clrscr;
10:=0;
1k:=1;
hx:=1k/m;
write (' t0= ' ) ; readln(t0);
write (' tk= ' ) ; readln(tk);
ht := (tk-t0) / n;
for i:=0 to m do
begin x [i]:= 10+hx*i;
writeln ('x[' ,i,' ]=',x [i]:2:3);
end;
for j:=0 to n do
begin
t [j]:= t0+ht*j;
writeln ('t[' ,j,' ]=',t [j]:2:3);
end;
for i:=0 to m-1 do
begin
for j:=1 to n-1 do
begin
U[i,j+1] :=U[i,j] +ht* ((1-sqr(x[i+1/2]) * (U[i+1,j]-U[i,j])-(1-sqr(x[i-1/2]) * (U[i,j]- U(i-1,j]))/sqr(hx) ;
write ('U[',i,',',j,'])= ',U[i,j]:2:3,' ');
end;
writeln;
end;
readln;
end.

Обсуждение

давно
Профессионал
848
1596
10.12.2015, 12:07
общий
10.12.2015, 19:43
это ответ
Здравствуйте, prostolenaya!

[code lang=js]import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
final int n=7;
final int m=7;
int i,j;
double[][] U = new double [m][n];
double[] x = new double [m];
double[] t = new double [n];
double ht,hx,t0,tk,l0,lk;

l0=0;
lk=1;
hx=lk/m;
System.out.println(" t0= ");
Scanner scanIn = new Scanner(System.in);
t0 = Double.parseDouble(scanIn.nextLine());
System.out.println(" tk= ");
tk = Double.parseDouble(scanIn.nextLine());
ht = (tk-t0) / n;
for (i = 0; i < m; i = i + 1)
{
x[i]= l0+hx*i;
System.out.println("x[" + i +"]="+x[i]);
}
for (j = 0; j < n; j = j + 1)
{
t[j]= t0+ht*j;
System.out.println("t[" + j +"]="+t[j]);
}

for (i = 0; i < m-1; i = i + 1)
{
for (j = 1; j < n-1; j = j + 1)
{
U[i][j+1] = U[i][j] + ht * ((1-Math.sqrt(x[i+1/2]) * (U[i+1][j]-U[i][j])-(1-Math.sqrt(x[j-1/2]) * (U[i][j]- U[j-1][j]))/Math.sqrt(hx)));
System.out.println("U[" + i + "," + j + "'])= " + U[i][j] + " ");
}
}
scanIn.nextLine();
}
}[/code]
Были ошибки в исходном задании.
ht,hx,t0,tk,10,1k: real;

Переменные не могут начинаться с цифр. В кода Java я заменил 1 на букву l.
U[i,j+1] :=U[i,j] +ht* ((1-sqr(x[i+1/2]) * (U[i+1,j]-U[i,j])-(1-sqr(x[i-1/2]) * (U[i,j]- U(i-1,j]))/sqr(hx) ;

перепутаны скобки,количество открытых не совпадает с количеством закрытых, во второй части формулы заменил i на j, т.к. возникала ошибка индекса при обращении к массиву.
Форма ответа