用户输入矩阵的维数,程序应计算行,列的总和以及矩阵的总和。如果矩阵的尺寸相同,例如2x2 3x3 7x7,那么一切都很好。但是,如果是3x2 5x4 7x5矩阵,则会发生异常:createElement_pr_4.exe中出现了'System.NullReferenceException'类型的未处理异常
其他信息:对象引用不表示对象实例。
<Window x:Class="createElement_pr_4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:createElement_pr_4"
mc:Ignorable="d"
Title="" Height="600" Width="1000">
<Grid x:Name="Container">
<TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="24" x:Name="HeightEl"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="40,0,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="28" x:Name="WidthEl"/>
<Button Content="Создать таблицу" HorizontalAlignment="Left" Margin="92,0,0,0" VerticalAlignment="Top" Width="120" x:Name="Create" Click="Create_Click"/>
<Button Content="Вычислить" HorizontalAlignment="Left" Margin="61,178,0,0" VerticalAlignment="Top" Width="75" x:Name="Add" Click="Add_Click"/>
<Label x:Name="test" Content="" HorizontalAlignment="Left" Margin="10,533,0,-7" VerticalAlignment="Top" Height="43" Width="120"/>
<Label x:Name="row" Content="" HorizontalAlignment="Left" Margin="158,533,0,0" VerticalAlignment="Top" Width="377"/>
<Label x:Name="col" Content="" HorizontalAlignment="Left" Margin="580,533,0,0" VerticalAlignment="Top" Width="377"/>
<Label x:Name="label" Content="Общая сумма" HorizontalAlignment="Left" Margin="0,482,0,0" VerticalAlignment="Top"/>
<Label x:Name="label1" Content="Сумма строк" HorizontalAlignment="Left" Margin="158,482,0,0" VerticalAlignment="Top"/>
<Label x:Name="label2" Content="Сумма колонок" HorizontalAlignment="Left" Margin="580,482,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace createElement_pr_4
{
public partial class MainWindow : Window
{
int w, h;
TextBox[,] textBoxes = new TextBox[10, 10];
public MainWindow()
{
InitializeComponent();
}
private void Create_Click(object sender, RoutedEventArgs e)
{
foreach (var TextBoxItem in textBoxes)
{
Container.Children.Remove(TextBoxItem);
}
try
{
h = int.Parse(HeightEl.Text);
if (h > 10)
{
h = 0;
MessageBox.Show("error");
}
w = int.Parse(WidthEl.Text);
if (w > 10)
{
w = 0;
MessageBox.Show("error");
}
}
catch (System.FormatException)
{
MessageBox.Show("error");
}
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
TextBox textBox = new TextBox()
{
Height = 23,
Width = 23,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = new Thickness(250.0 + (double)(j * 40), 100 + (double)(i * 40), 0, 0)
};
try
{
textBoxes[i, j] = textBox;
Container.Children.Add(textBoxes[i, j]);
}
catch (System.IndexOutOfRangeException)
{
MessageBox.Show("error");
}
}
}
//Add.Visibility = Visibility.Visible;
//Result.Visibility = Visibility.Visible;
}
private void Add_Click(object sender, RoutedEventArgs e)
{
try
{
double sumCol;
double sumRow;
double sumAll = 0;
for (int j = 0; j < w; j++)
{
sumCol = 0;
sumRow = 0;
for (int i = 0; i < h; i++)
{
sumAll += Convert.ToDouble((textBoxes[i, j]).Text);
sumCol += Convert.ToDouble((textBoxes[i, j]).Text);
sumRow += Convert.ToDouble((textBoxes[j, i]).Text);
}
col.Content += Convert.ToString(sumCol + ", ");
row.Content += Convert.ToString(sumRow + ", ");
}
test.Content = sumAll;
}
catch (System.FormatException)
{
MessageBox.Show("erroe");
}
catch (System.OverflowException)
{
MessageBox.Show("error");
}
}
}
}