我对MATLAB中的结构有点麻烦。我需要创建自己的小周期表,我认为到目前为止我已经做好了。但我的问题是当我尝试添加第二个元素时,它会在第一个元素上写入。我怎样才能解决这个问题?请看一下我的代码,我知道它有点长:D 如果你看到任何其他错误,我会很感激。谢谢你的帮助
fprintf('Periodic table program - Main menu:\n (1) Add an element; (2) Search for an element; (3) Exit\n');
choose=input('Enter your choice: ');
while choose~=3
if choose==1
element.name=input('Enter the name of your element: ','s');
element.symbol=input('Enter the symbol of your element: ','s');
element.Anumber=input('Enter the atomic number of your element: ');
while element.Anumber<=0 || mod(element.Anumber,1)~=0
disp('This is not a positive integer, please enter a valid input!');
eAnumber=input('Enter the atomic number of your element: ');
end
elseif choose==2
fprintf('Search menu:\n (1) By name; (2) By symbol; (3) By atomic number; (4) Main menu\n');
S_choice=input('Enter your choice: ');
while S_choice ~=4
if S_choice==1
alex=input('Enter the name of the element: ','s');
if alex~=element.name
disp('No such element exists!');
else
fprintf('Operations menu:\n (1) Show properties; (2) Edit; (3) Main menu\n');
Op_choice=input('Enter your choice: ');
while Op_choice~=3
if Op_choice==1
element
elseif Op_choice==2
fprintf('Edit menu:\n (1) Edit name; (2) Edit symbol; (3) Edit atomic number; (4) Main menu\n');
E_choice=input('Enter your choice: ');
while E_choice~=4
if E_choice==1
new_name=input('Enter the new name of the element: ','s');
element.name=new_name;
elseif E_choice==2
new_sym=input('Enter the new symbol of the element: ','s');
element.symbol=new_sym;
elseif E_choice==3
new_anumber=input('Enter the new atomic number: ');
while new_anumber<=0 || mod(new_anumber,1)~=0
disp('This is not a positive integer, please enter a valid input!');
new_anumber=input('Enter the new atomic number: ');
end
element.Anumber=new_anumber;
end
fprintf('Edit menu:\n (1) Edit name; (2) Edit symbol; (3) Edit atomic number; (4) Main menu\n');
E_choice=input('Enter your choice: ');
end
end
fprintf('Operations menu:\n (1) Show properties; (2) Edit; (3) Main menu\n');
Op_choice=input('Enter your choice: ');
end
end
elseif S_choice==2
lugano=input('Enter the symbol of the element: ','s');
if lugano~=element.symbol
disp('No such element exists!');
else
fprintf('Operations menu:\n (1) Show properties; (2) Edit; (3) Main menu\n');
Op_choice=input('Enter your choice: ');
while Op_choice~=3
if Op_choice==1
element
elseif Op_choice==2
fprintf('Edit menu:\n (1) Edit name; (2) Edit symbol; (3) Edit atomic number; (4) Main menu\n');
E_choice=input('Enter your choice: ');
while E_choice~=4
if E_choice==1
new_name=input('Enter the new name of the element: ','s');
element.name=new_name;
elseif E_choice==2
new_sym=input('Enter the new symbol of the element: ','s');
element.symbol=new_sym;
elseif E_choice==3
new_anumber=input('Enter the new atomic number: ');
while new_anumber<=0 || mod(new_anumber,1)~=0
disp('This is not a positive integer, please enter a valid input!');
new_anumber=input('Enter the new atomic number: ');
end
element.Anumber=new_anumber;
end
fprintf('Edit menu:\n (1) Edit name; (2) Edit symbol; (3) Edit atomic number; (4) Main menu\n');
E_choice=input('Enter your choice: ');
end
end
fprintf('Operations menu:\n (1) Show properties; (2) Edit; (3) Main menu\n');
Op_choice=input('Enter your choice: ');
end
end
elseif S_choice==3
niang=input('Enter the atomic number of the element: ');
if niang~=element.Anumber
disp('No such element exists!');
else
fprintf('Operations menu:\n (1) Show properties; (2) Edit; (3) Main menu\n');
Op_choice=input('Enter your choice: ');
while Op_choice~=3
if Op_choice==1
element
elseif Op_choice==2
fprintf('Edit menu:\n (1) Edit name; (2) Edit symbol; (3) Edit atomic number; (4) Main menu\n');
E_choice=input('Enter your choice: ');
while E_choice~=3
if E_choice==1
new_name=input('Enter the new name of the element: ','s');
element.name=new_name;
elseif E_choice==2
new_sym=input('Enter the new symbol of the element: ','s');
element.symbol=new_sym;
elseif E_choice==3
new_anumber=input('Enter the new atomic number: ');
while new_anumber<=0 || mod(new_anumber,1)~=0
disp('This is not a positive integer, please enter a valid input!');
new_anumber=input('Enter the new atomic number: ');
end
element.Anumber=new_anumber;
end
fprintf('Edit menu:\n (1) Edit name; (2) Edit symbol; (3) Edit atomic number; (4) Main menu\n');
E_choice=input('Enter your choice: ');
end
end
fprintf('Operations menu:\n (1) Show properties; (2) Edit; (3) Main menu\n');
Op_choice=input('Enter your choice: ');
end
end
end
fprintf('Search menu:\n (1) By name; (2) By symbol; (3) By atomic number; (4) Main menu\n');
S_choice=input('Enter your choice: ');
end
end
fprintf('Periodic table program - Main menu:\n (1) Add an element; (2) Search for an element; (3) Exit\n');
choose=input('Enter your choice: ');
end
答案 0 :(得分:0)
您需要一个结构数组来保存不同的元素。尝试以下内容:
element = struct
i = 1;
element(i).name = 'oxygen'
element(i).symbol = 'O'
i = i + 1
element(i).name = 'carbon'
element(i).symbol = 'C'