我目前在C#中做了一個非常簡單的計算器程序,我的程序工作正常,因爲在加,減,乘和除功能工作正常,雖然我遇到的問題是十進制點無法正常工作。錯誤是當我用小數添加兩個數字時,它無法正常工作。 也就是說,如果我加20和.5,結果將是20.5,但如果我加20和1.5,答案也是20.5。我相信這是因爲,由於我寫代碼的方式,計算器會忽略1.5中的數字1(請參閱我的代碼,因爲我相信你們可能知道如何解決這個問題)。任何幫助解決這個問題,將不勝感激。C#計算器程序#
(MainPage.xaml中的代碼)
<phone:PhoneApplicationPage
x:Class="Calculator.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="Jason Lynch" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Calculator" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="=" Height="116" Margin="218,485,0,0" Name="btnEquals" VerticalAlignment="Top" HorizontalAlignment="Left" Width="127" Background="#FFFF550D" FontSize="56" Click="btnEquals_Click" />
<Button Content="-" Height="116" HorizontalAlignment="Center" Margin="323,485,6,0" Name="btnSubtract" VerticalAlignment="Top" Width="127" FontSize="56" Padding="0,-25,0,0" Click="btnSubtract_Click" />
<Button Content="0" Height="116" HorizontalAlignment="Left" Margin="6,485,0,0" Name="btn0" VerticalAlignment="Top" Width="127" FontSize="56" Click="btn0_Click" />
<Button Content="Clear" Height="73" HorizontalAlignment="Left" Margin="112,485,0,0" Name="btnClear" VerticalAlignment="Top" Width="127" FontSize="28" Background="#FF0008FF" Click="btnClear_Click"></Button>
<Button Content="3" Height="116" HorizontalAlignment="Right" Margin="0,389,111,0" Name="btn3" VerticalAlignment="Top" Width="127" FontSize="56" Click="btn3_Click" />
<Button Content="+" Height="116" HorizontalAlignment="Left" Margin="323,389,0,0" Name="btnAdd" VerticalAlignment="Top" Width="127" FontSize="56" Click="btnAdd_Click" />
<Button Content="1" Height="116" HorizontalAlignment="Left" Margin="6,389,0,0" Name="btn1" VerticalAlignment="Top" Width="127" FontSize="56" Click="btn1_Click" />
<Button Content="2" Height="116" HorizontalAlignment="Left" Margin="112,389,0,0" Name="btn2" VerticalAlignment="Top" Width="127" FontSize="56" Click="btn2_Click" />
<Button Content="6" Height="116" HorizontalAlignment="Right" Margin="0,294,111,0" Name="btn6" VerticalAlignment="Top" Width="127" Click="button9_Click" FontSize="56" />
<Button Content="x" Height="116" HorizontalAlignment="Right" Margin="0,294,6,0" Name="btnMultiply" VerticalAlignment="Top" Width="127" FontSize="56" Click="btnMultiply_Click" />
<Button Content="4" Height="116" HorizontalAlignment="Left" Margin="6,294,0,0" Name="btn4" VerticalAlignment="Top" Width="127" FontSize="56" Click="btn4_Click" />
<Button Content="5" Height="116" HorizontalAlignment="Left" Margin="112,294,0,0" Name="btn5" VerticalAlignment="Top" Width="127" FontSize="56" Click="btn5_Click" />
<Button Content="9" Height="116" HorizontalAlignment="Left" Margin="218,199,0,0" Name="btn9" VerticalAlignment="Top" Width="127" FontSize="56" Click="btn9_Click" />
<Button Content="÷" Height="116" HorizontalAlignment="Left" Margin="323,199,0,0" Name="btnDivide" VerticalAlignment="Top" Width="127" FontSize="56" Click="btnDivide_Click" />
<Button Content="7" Height="116" HorizontalAlignment="Left" Margin="6,199,0,0" Name="btn7" VerticalAlignment="Top" Width="127" FontSize="56" Click="btn7_Click" />
<Button Content="8" Height="116" HorizontalAlignment="Left" Margin="112,199,0,0" Name="btn8" VerticalAlignment="Top" Width="127" FontSize="56" Click="btn8_Click" />
<TextBox Height="72" HorizontalAlignment="Left" Margin="0,65,0,0" Name="resultTxtBox" Text="" VerticalAlignment="Top" Width="460" />
<TextBlock Height="53" HorizontalAlignment="Left" Margin="169,26,0,0" Name="textBlock1" Text="Result" VerticalAlignment="Top" FontSize="36" />
<Button Content="." Height="68" HorizontalAlignment="Left" Margin="112,533,0,0" Name="btnDecimalPoint" VerticalAlignment="Top" Width="127" Click="btnDecimalPoint_Click" HorizontalContentAlignment="Center" />
</Grid>
</Grid>
<!--Sample code showing usage of ApplicationBar-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="MenuItem 1"/>
<shell:ApplicationBarMenuItem Text="MenuItem 2"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>-->
(MainPage.xaml.cs中的代碼)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
namespace Calculator
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private double total1 = 0;
private double total2 = 0;
bool plusButtonClicked = false;
bool minusButtonClicked = false;
bool multiplyButtonClicked = false;
bool divideButtonClicked = false;
private void btn0_Click(object sender, RoutedEventArgs e)
{
resultTxtBox.Text = resultTxtBox.Text + "0";
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
resultTxtBox.Text = resultTxtBox.Text + "1";
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
resultTxtBox.Text = resultTxtBox.Text + "2";
}
private void btn3_Click(object sender, RoutedEventArgs e)
{
resultTxtBox.Text = resultTxtBox.Text + "3";
}
private void btn4_Click(object sender, RoutedEventArgs e)
{
resultTxtBox.Text = resultTxtBox.Text + "4";
}
private void btn5_Click(object sender, RoutedEventArgs e)
{
resultTxtBox.Text = resultTxtBox.Text + "5";
}
//MESSED UP BUTTON *** button9 == btn6
private void button9_Click(object sender, RoutedEventArgs e)
{
resultTxtBox.Text = resultTxtBox.Text + "6";
}
private void btn7_Click(object sender, RoutedEventArgs e)
{
resultTxtBox.Text = resultTxtBox.Text + "7";
}
private void btn8_Click(object sender, RoutedEventArgs e)
{
resultTxtBox.Text = resultTxtBox.Text + "8";
}
private void btn9_Click(object sender, RoutedEventArgs e)
{
resultTxtBox.Text = resultTxtBox.Text + "9";
}
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
total1 = total1 += double.Parse(resultTxtBox.Text);
resultTxtBox.Text = "";
plusButtonClicked = true;
minusButtonClicked = false;
multiplyButtonClicked = false;
divideButtonClicked = false;
}
private void btnSubtract_Click(object sender, RoutedEventArgs e)
{
total1 = total1 + double.Parse(resultTxtBox.Text);
resultTxtBox.Text = "";
plusButtonClicked = false;
minusButtonClicked = true;
multiplyButtonClicked = false;
divideButtonClicked = false;
}
private void btnMultiply_Click(object sender, RoutedEventArgs e)
{
total1 = total1 + double.Parse(resultTxtBox.Text);
resultTxtBox.Text = "";
plusButtonClicked = false;
minusButtonClicked = false;
multiplyButtonClicked = true;
divideButtonClicked = false;
}
private void btnDivide_Click(object sender, RoutedEventArgs e)
{
total1 = total1 + double.Parse(resultTxtBox.Text);
resultTxtBox.Text = "";
plusButtonClicked = false;
minusButtonClicked = false;
multiplyButtonClicked = false;
divideButtonClicked = true;
}
private void btnEquals_Click(object sender, RoutedEventArgs e)
{
if (plusButtonClicked == true)
{
total2 = total1 + double.Parse(resultTxtBox.Text);
resultTxtBox.Text = total2.ToString();
total1 = 0;
}
else if (minusButtonClicked == true)
{
total2 = total1 - double.Parse(resultTxtBox.Text);
resultTxtBox.Text = total2.ToString();
total1 = 0;
}
else if (multiplyButtonClicked == true)
{
total2 = total1 * double.Parse(resultTxtBox.Text);
resultTxtBox.Text = total2.ToString();
total1 = 0;
}
else if (divideButtonClicked == true)
{
total2 = total1/double.Parse(resultTxtBox.Text);
resultTxtBox.Text = total2.ToString();
total1 = 0;
}
}
private void btnClear_Click(object sender, RoutedEventArgs e)
{
resultTxtBox.Text = "";
}
private void btnDecimalPoint_Click(object sender, RoutedEventArgs e)
{
resultTxtBox.Text = ".";
}
}
}
最後,這裏是我的計算器應用程序的擷取畫面>>http://gyazo.com/717b396e32b39d4a42e91ad9cf67cdef
感謝您提前提供幫助, Jason。
這似乎是有關DecimalPoint_Click方法。我會盡力回覆 – shingonati0n
漂亮的應用程序! – Kevin