我正在制作一个带有PowerShell GUI的计算器作为培训。 我从其他脚本复制/粘贴计算器,并且id正常工作。 在新脚本上, - ,/,^,√工作但+,*不起作用。
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void]
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objFormfenster1 = New-Object System.Windows.Forms.Form
$objFormfenster1.Backcolor="white"
$objFormfenster1.StartPosition = "CenterScreen"
$objFormfenster1.Size = New-Object System.Drawing.Size(500,500)
$objFormfenster1.Text = "Calculator with GUI"
$objLabelfenster1 = New-Object System.Windows.Forms.Label
$objLabelfenster1.Location = New-Object System.Drawing.Size(30,30)
$objLabelfenster1.Size = New-Object System.Drawing.Size(200,40)
$objLabelfenster1.Text = "Geben Sie die erste Zahl ein (wenn Sie keine zahl eingeben wird automatisch 0 genommen):"
$objFormfenster1.Controls.Add($objLabelfenster1)
$objTextBoxfenster1 = New-Object System.Windows.Forms.TextBox
$objTextBoxfenster1.Location = New-Object System.Drawing.Size(30,70)
$objTextBoxfenster1.Size = New-Object System.Drawing.Size(300,30)
$objTextBoxfenster1.Text
$objFormfenster1.Controls.Add($objTextBoxfenster1)
$objLabelfenster1 = New-Object System.Windows.Forms.Label
$objLabelfenster1.Location = New-Object System.Drawing.Size(30,110)
$objLabelfenster1.Size = New-Object System.Drawing.Size(200,40)
$objLabelfenster1.Text = "Wählen Sie den Operator aus (bei Wurzel nur im ersten Feld zahl eingeben):"
$objFormfenster1.Controls.Add($objLabelfenster1)
$objComboboxfenster1 = New-Object System.Windows.Forms.Combobox
$objComboboxfenster1.Location = New-Object System.Drawing.Size(30,150)
$objComboboxfenster1.Size = New-Object System.Drawing.Size(300,30)
$objComboboxfenster1.Height = 70
$objFormfenster1.Controls.Add($objComboboxfenster1)
[void] $objComboboxfenster1.Items.Add("+")
[void] $objComboboxfenster1.Items.Add("-")
[void] $objComboboxfenster1.Items.Add("*")
[void] $objComboboxfenster1.Items.Add("/")
[void] $objComboboxfenster1.Items.Add("^")
[void] $objComboboxfenster1.Items.Add("√")
$objComboboxfenster1.SelectedItem
$objComboboxfenster1.Add_SelectedIndexChanged({ })
$objLabelfenster2 = New-Object System.Windows.Forms.Label
$objLabelfenster2.Location = New-Object System.Drawing.Size(30,190)
$objLabelfenster2.Size = New-Object System.Drawing.Size(200,40)
$objLabelfenster2.Text = "Geben Sie die zweite Zahl ein (wenn Sie keine zahl eingeben wird automatisch 0 genommen):"
$objFormfenster1.Controls.Add($objLabelfenster2)
$objTextBoxfenster2 = New-Object System.Windows.Forms.TextBox
$objTextBoxfenster2.Location = New-Object System.Drawing.Size(30,230)
$objTextBoxfenster2.Size = New-Object System.Drawing.Size(300,30)
$objTextBoxfenster2.Text
$objFormfenster1.Controls.Add($objTextBoxfenster2)
$GoButtonfenster1 = New-Object System.Windows.Forms.Button
$GoButtonfenster1.Location = New-Object System.Drawing.Size(120,405)
$GoButtonfenster1.Size = New-Object System.Drawing.Size(100,30)
$GoButtonfenster1.Text = "Go"
$GoButtonfenster1.Name = "Go"
$GoButtonfenster1.DialogResult = "None"
$GoButtonfenster1.Add_Click({
$zahl1 = $null
$zahl2 = $null
$operator = $null
[string]$zahl1 = $objTextBoxfenster1.Text
[string]$zahl2 = $objTextBoxfenster2.Text
$operator = $objComboboxfenster1.SelectedItem
$iserror = "False"
$zahl1 = $zahl1 -as [int]
if($zahl1 -like $null) {
$iserror = "True"
$result = $null
}
$zahl2 = $zahl2 -as [int]
if($zahl2 -like $null) {
$iserror = "True"
$result = $null
}
if ($iserror -eq "True"){
[System.Windows.Forms.MessageBox]::Show("Error: Eine Zahl wurde falsch eingegeben","Error")
}
switch ($operator) {
"+" {$result = $zahl1 + $zahl2}
"-" {$result = $zahl1 - $zahl2}
"*" {$result = $zahl1 * $zahl2}
"/" {$result = $zahl1 / $zahl2}
"^" {$result = [math]::pow( $zahl1, $zahl2 )}
"√" {$result = [math]::sqrt( $zahl1 ) }
default {$iserror = "True"}
}
if ($iserror -eq "False"){
[System.Windows.Forms.MessageBox]::Show("$result","Das Ergebniss")
}
})
$objFormfenster1.Controls.Add($GoButtonfenster1)
$CancelButtonfenster1 = New-Object System.Windows.Forms.Button
$CancelButtonfenster1.Location = New-Object System.Drawing.Size(30,405)
$CancelButtonfenster1.Size = New-Object System.Drawing.Size(100,30)
$CancelButtonfenster1.Text = "Exit"
$CancelButtonfenster1.Name = "Exit"
$CancelButtonfenster1.DialogResult = "Cancel"
$CancelButtonfenster1.Add_Click({$objFormfenster1.Close()})
$objFormfenster1.Controls.Add($CancelButtonfenster1)
[void] $objFormfenster1.ShowDialog()
Scriptinputtranslation(德语 - > Englisch)
如果您运行脚本,它首先会要求您输入第一个号码,然后是运营商,然后是第二个号码。如果您在数字字段中添加字母或其他字符,则会在向其中一个字段中输入错误字符时出错。如果输入数字并选择一个操作符,将弹出一个窗口并告诉您结果。
答案 0 :(得分:0)
如果您在控制台中进行简单测试,我发现您正在尝试逐字符串地增加字符串:
"2" * 2"
你将获得22而不是4
2 * 2
结果将是正确的:4
所以只需将结果转换为$ zahl1 / $ zahl2变量到int类型,你就会得到正确的结果:
所以,更改此部分(我刚刚修改了' *'和' +',您更改了其余部分):
switch ($operator) {
"+" {$result = [int]$zahl1 + [int]$zahl2}
"-" {$result = $zahl1 - $zahl2}
"*" {$result = [int]$zahl1 * [int]$zahl2}
"/" {$result = $zahl1 / $zahl2}
"^" {$result = [math]::pow( $zahl1, $zahl2 )}
"√" {$result = [math]::sqrt( $zahl1 ) }
default {$iserror = "True"}
或更改此部分:
[int]$zahl1 = $objTextBoxfenster1.Text
[int]$zahl2 = $objTextBoxfenster2.Text
答案 1 :(得分:0)
你的问题从这里开始:
[string]$zahl1 = $objTextBoxfenster1.Text [string]$zahl2 = $objTextBoxfenster2.Text
此时您已强烈输入$zahl1
和$zahl2
。现在发生的是,对这些变量的每个赋值都是自动转换为字符串。您可以确认这是变量属性System.Management.Automation.ArgumentTypeConverterAttribute
[string]$var = "5"
Get-Variable var | ft *
因此,即使您尝试在后面的代码中将它们键入[int]
,它们仍然是字符串。
[string]$var = "5"
write-host "`$var: $var is $($var.GetType().Fullname)"
$var = $var -as [int]
write-host "`$var: $var is $($var.GetType().Fullname)"
<强>输出强>
$var: 5 is System.String
$var: 5 is System.String
因此,如果我删除变量和强类型声明,我会得到您可能期望的结果。
remove-variable var
$var = "5"
write-host "`$var: $var is $($var.GetType().Fullname)"
$var = $var -as [int]
write-host "`$var: $var is $($var.GetType().Fullname)"
<强>输出强>
$var: 5 is System.String
$var: 5 is System.Int32
您可能只是简化了一些逻辑
$zahl1 = $objTextBoxfenster1.Text -as [int]
$zahl2 = $objTextBoxfenster2.Text -as [int]
if($zahl1 -and $zahl2){
# Do your switch based math now
} else {
# something is wrong with one of them
Write-Warning "try again.... with real numbers this time."
}