locked
Adding Variable RRS feed

  • Question

  • Hello,

    Im trying to make it so that is the user press a key such as (1) it adds one to a variable (x). So far I have created

    x = 0

    y = textwindow.read

    If y = (1) then

    x = [adding the one to the variable]

    endif

    Thursday, November 28, 2013 2:56 AM

Answers

  • try this:

    (you have to press enter after you press a number)

    x = 0
    TextWindow.Write("enter a number: ")
    y = textwindow.Read()
    
    If y >=0 then
    x = x + y
    endif
    
    TextWindow.WriteLine(x)


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    • Proposed as answer by Jibba j Thursday, December 5, 2013 6:20 AM
    • Marked as answer by litdev Thursday, December 5, 2013 8:01 PM
    Thursday, November 28, 2013 3:36 AM
    Answerer
  • Coolaspie

    TextWindow.Read(), TextWindow.ReadNumber() and TextWindow.ReadKey() all cause the program to wait till the user presses enter or just a Key for TW.ReadKey().

    So just before the line y = textwindow.read(), ask the user to enter a value. Then TW.Read() will wait till the Enter key is pressed and will then read what was entered by the user.

    Also if you want the user to input numbers only, then TextWindow.ReadNumber() is a good method to consider as well.

    This will only allow the user to type numbers only. i.e. an easy way to validate the key stroke entries as numbers.

    X = 5
    takeInput = "True"
    
    While takeInput
      
      TextWindow.Write("Enter a number: ")
      Y = TextWindow.ReadNumber()
      
      If Y = 1 Then
        output = X + Y
      Else
        output = "X <> 1"
      EndIf
      
      TextWindow.WriteLine(output)
      
      TextWindow.Write("Enter another? y/n ")
      keyPressed = Text.ConvertToLowerCase(TextWindow.ReadKey())
      
      If keyPressed = "y" Then
        takeInput = "True"
      Else
        takeInput = "False"
      EndIf
      
      TextWindow.Clear()
    EndWhile

    • Marked as answer by litdev Thursday, December 5, 2013 8:01 PM
    Thursday, December 5, 2013 6:18 AM

All replies

  • try this:

    (you have to press enter after you press a number)

    x = 0
    TextWindow.Write("enter a number: ")
    y = textwindow.Read()
    
    If y >=0 then
    x = x + y
    endif
    
    TextWindow.WriteLine(x)


    It is written: "'As surely as I live,' says the Lord, 'every knee will bow before me; every tongue will acknowledge God.'" Romans 14:11

    • Proposed as answer by Jibba j Thursday, December 5, 2013 6:20 AM
    • Marked as answer by litdev Thursday, December 5, 2013 8:01 PM
    Thursday, November 28, 2013 3:36 AM
    Answerer
  • Hi!

    Please state it more clearly what you mean by "press a key". Do you mean on a console? Or do you mean inside a form?

    Saturday, November 30, 2013 12:54 PM
  • Coolaspie

    TextWindow.Read(), TextWindow.ReadNumber() and TextWindow.ReadKey() all cause the program to wait till the user presses enter or just a Key for TW.ReadKey().

    So just before the line y = textwindow.read(), ask the user to enter a value. Then TW.Read() will wait till the Enter key is pressed and will then read what was entered by the user.

    Also if you want the user to input numbers only, then TextWindow.ReadNumber() is a good method to consider as well.

    This will only allow the user to type numbers only. i.e. an easy way to validate the key stroke entries as numbers.

    X = 5
    takeInput = "True"
    
    While takeInput
      
      TextWindow.Write("Enter a number: ")
      Y = TextWindow.ReadNumber()
      
      If Y = 1 Then
        output = X + Y
      Else
        output = "X <> 1"
      EndIf
      
      TextWindow.WriteLine(output)
      
      TextWindow.Write("Enter another? y/n ")
      keyPressed = Text.ConvertToLowerCase(TextWindow.ReadKey())
      
      If keyPressed = "y" Then
        takeInput = "True"
      Else
        takeInput = "False"
      EndIf
      
      TextWindow.Clear()
    EndWhile

    • Marked as answer by litdev Thursday, December 5, 2013 8:01 PM
    Thursday, December 5, 2013 6:18 AM
  • TextWindow.Read() is used in the TextWindow so "press a key" refers to the keyboard.
    Thursday, December 5, 2013 6:24 AM