无法找出问题出在哪里 [英] Can't figure out where the problem is with calculation

查看:79
本文介绍了无法找出问题出在哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么我的代码没有计算.当我输入并选择每个输入后按提交按钮时,它甚至不会显示任何内容甚至错误.

I can't figure out why my code is not calculating. when i press submit button after entering and selecting each input it doesn't show up anything not even error.

下面的xaml文件内容.

xaml file content below.

<Window x:Class="MortgageCalculator.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:Mortgage Calculator"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="625" HorizontalAlignment="Center" VerticalAlignment="Center">
    <Grid x:Name="lblintratevalue" HorizontalAlignment="Center" VerticalAlignment="Center">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="162"></ColumnDefinition>
            <ColumnDefinition Width="Auto" MinWidth="416"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="41"></RowDefinition>
            <RowDefinition Height="152"></RowDefinition>
            <RowDefinition Height="62"></RowDefinition>
            <RowDefinition Height="83"></RowDefinition>
            <RowDefinition Height="50"></RowDefinition>
        </Grid.RowDefinitions>
        <Label x:Name="lblprincipal" Grid.Column="0" Grid.Row="0" Content="Principal Amount:" FontFamily="Arial" FontSize="14.667" FontWeight="Bold" Margin="0,6,6,9" Width="136" VerticalAlignment="Center" HorizontalAlignment="Right" Height="26"/>
        <TextBox x:Name="txtprincipal" Grid.Column="1" HorizontalAlignment="Left" Height="28" TextWrapping="Wrap" Text="" VerticalAlignment="Center" Width="184" FontFamily="Gadugi" FontSize="14.667" BorderThickness="1.5" Margin="10,6,0,7" ToolTip="Pricipal" UndoLimit="95"/>
        <Label x:Name="lblyears" Content="Number of Years:"  Grid.Row="1" FontFamily="Arial" FontSize="14.667" FontWeight="Bold" Margin="0,45,10,81" Width="132" VerticalAlignment="Center" HorizontalAlignment="Right" Height="26" />
        <StackPanel x:Name="stkrdobtns" Grid.Column="1" HorizontalAlignment="Left" Height="99" Margin="10,32,0,21" Grid.Row="1" VerticalAlignment="Center" Width="184">
            <RadioButton x:Name="rad10years" Content="10" FontSize="14.667" HorizontalAlignment="Left" VerticalAlignment="Center" BorderThickness="1.5" FontFamily="Arial"/>
            <RadioButton x:Name="rad20years" Content="20" FontSize="14.667" HorizontalAlignment="Left" VerticalAlignment="Center" BorderThickness="1.5" FontFamily="Arial"/>
            <RadioButton x:Name="rad30years" Content="30" FontSize="14.667" HorizontalAlignment="Left" VerticalAlignment="Center" BorderThickness="1.5" FontFamily="Arial"/>
            <RadioButton x:Name="radother" Content="Other" FontSize="14.667" HorizontalAlignment="Left" VerticalAlignment="Center" BorderThickness="1.5" FontFamily="Arial" Width="76"/>
            <TextBox x:Name="txtother" HorizontalAlignment="Left" Height="23" Margin="20,0,0,0" TextWrapping="Wrap" Text="Enter Year" Width="128" FontFamily="Arial" FontSize="14.667"/>
        </StackPanel>
        <Label x:Name="lblinterestrate" Content="Interest Rate:"  Grid.Row="2" FontFamily="Arial" FontSize="14.667" FontWeight="Bold" Margin="0,15,10,21" Width="132" VerticalAlignment="Center" HorizontalAlignment="Right" Height="26" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" />
        <Slider x:Name="sldinterest" Grid.Column="1" HorizontalAlignment="Left" Height="27" Margin="10,10,0,0" Grid.Row="2" VerticalAlignment="Top" Width="276" Background="{x:Null}" Ticks="0.5, 1, 1.5, 2, 2.5,  3, 3.5, 4, 4.5, 5" AutoToolTipPlacement="BottomRight" Minimum="0.5" Maximum="5" IsHitTestVisible="True" FontSize="13.333" FontFamily="Arial" Cursor="Arrow" TickPlacement="BottomRight" SelectionEnd="1" UseLayoutRounding="False" IsSnapToTickEnabled="True" ValueChanged="sldvalue_changed" Foreground="Black" TickFrequency="0.5" LargeChange="0.5">
            <Slider.Effect>
                <DropShadowEffect BlurRadius="2" Color="#FF8F8C8C" ShadowDepth="3"/>
            </Slider.Effect>
        </Slider>
        <Label x:Name="lblintratedisplay" Content="0.5" Grid.Column="1" HorizontalAlignment="Left" Height="31" Margin="306,6,0,0" Grid.Row="2" VerticalAlignment="Top" Width="53" FontFamily="Arial" FontSize="13.333"/>
        <Button x:Name="btnsubmit" Content="Submit" Grid.Column="1" HorizontalAlignment="Left" Height="58" Margin="10,15,0,0" Grid.Row="3" VerticalAlignment="Top" Width="165" FontFamily="Arial" FontSize="16" FontWeight="Bold" Background="{x:Null}" Click="btnsubmit_Click">
            <Button.Effect>
                <DropShadowEffect BlurRadius="2" Color="#FF7E7979" ShadowDepth="2"/>
            </Button.Effect>
        </Button>

    </Grid>
</Window>


.CS文件的内容


.CS file content below

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 MortgageCalculator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        double principal;
        double years;
        double rate = .5;


        //Slider text box value display
        private void sldvalue_changed(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (lblintratedisplay != null)
            {
                lblintratedisplay.Content = sldinterest.Value.ToString();
                rate = sldinterest.Value;
            }
        }


        private void btnsubmit_Click(object sender, RoutedEventArgs e)
        {
            double monthlypayment;

            {

                //Tryparse for principal amount with error Red texbox
                if (String.IsNullOrEmpty(txtprincipal.Text) || !double.TryParse(txtprincipal.Text, out principal))
                {
                    txtprincipal.BorderBrush = new SolidColorBrush(Colors.Red);
                    return;
                }
                else
                    txtprincipal.BorderBrush = new SolidColorBrush(Colors.Black);



                //Validate radio button
                if (rad10years.IsChecked.Value)
                {
                    years = 10;
                    return;
                }
                else if (rad20years.IsChecked.Value)
                {
                    years = 20;
                    return;
                }
                else if (rad30years.IsChecked.Value)
                {
                    years = 30;
                    return;
                }
                else if (radother.IsChecked == true)
                {
                    if (!double.TryParse(txtother.Text, out years))
                    {
                        MessageBox.Show("Not a Valid year.");
                    }
                    return;
                }
                else
                {
                    MessageBox.Show("Please select number of years.");
                    stkrdobtns.Background = new SolidColorBrush(Colors.Red);
                    return;
                }

               

               double monthlypayment = ((principal * rate) / 1200) / Math.Pow(1 - (1.0 + rate / 1200), (-12.0 * years));
                    string MonthPay = string.Format("The amount of the monthly payment is: {0:c}", monthlypayment);
                MessageBox.Show(MonthPay);


            }
        }
    }
}

推荐答案

正如我在 btnsubmit_Click 中看到的那样,您有一个if语句来验证单选按钮,但是有一个 在所有情况下都返回,这意味着if语句之后的所有内容均无法访问;因为return语句终止了该方法的执行.

As I can see in btnsubmit_Click you have an if statement to validate radio buttons however there is a return in all cases which means that every thing after the if statement will not be reachable; because return statement terminates execution of the method.

		//Validate radio button
                if (rad10years.IsChecked.Value)
                {
                    years = 10;
                }
                else if (rad20years.IsChecked.Value)
                {
                    years = 20;
                }
                else if (rad30years.IsChecked.Value)
                {
                    years = 30;
                }
                else if (radother.IsChecked == true)
                {
                    if (!double.TryParse(txtother.Text, out years))
                    {
                        MessageBox.Show("Not a Valid year.");
                    }
                    return;
                }
                else
                {
                    MessageBox.Show("Please select number of years.");
                    stkrdobtns.Background = new SolidColorBrush(Colors.Red);
                    return;
                }

如果此答案有帮助,请记住通过将有用的帖子标记为答案来关闭话题

If this answer was helpful please remember to close your threads by marking helpful posts as answer

票价


这篇关于无法找出问题出在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆