带有 GUI + 和 * 的计算器不起作用



我正在用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)

如果您运行脚本,则首先要求您提供第一个数字,然后索要操作员,然后要求第二个数字。如果您在数字字段中放置字母或其他字符,会有一个错误告诉您,您在其中一个字段中放了一个错误的字符。如果输入数字,并且选择一个操作员,则窗口将弹出并告诉您结果。

您的问题从这里开始:

[string]$zahl1 = $objTextBoxfenster1.Text
[string]$zahl2 = $objTextBoxfenster2.Text

此时,您已经强烈键入$zahl1$zahl2。现在发生的事情是,对这些变量的每个分配都会自动转换为字符串。您可以确认这是变量属性 System.Management.Automation.ArgumentTypeConverterAttribute

的主题
[string]$var = "5"
Get-Variable var | ft *

因此,即使您尝试在以后的代码中将它们键入[int]S,它们仍然是字符串。

[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."
}

我看到您正在尝试将字符串乘以字符串,如果您在控制台中进行简单测试:

"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

最新更新