none
No clue how this math is wrong.... RRS feed

  • Question

  • Hello, I am new to working with Small Basic. However, this math seems pretty straight forward and i'm not sure how it's going wrong. I'd very much appreciate it if anyone could identify the problem, and possibly pose a solution. The answers this program  is giving are not only incorrect, but not even static. You'll enter the same information and get different answers.

    seconds = 0
    start:
    TextWindow.ForegroundColor = "Magenta"
    TextWindow.Write ("Hello Ken, my name is RSXPP. Type") 
    TextWindow.ForegroundColor = "Red" 
    TextWindow.Write (" Start ")
    TextWindow.ForegroundColor = "Magenta" 
    TextWindow.WriteLine("to begin the program.")
    TextWindow.ForegroundColor = "Gray"
    read = TextWindow.Read()
    If read = "Start" Or  read = "start" Then
      TextWindow.ForegroundColor = "Magenta"    
      TextWindow.WriteLine ("How much XP do you currently have?")
      TextWindow.ForegroundColor = "Gray"
      startxp = TextWindow.ReadNumber()  
      TextWindow.ForegroundColor = "Red" 
      time1 = Clock.Time
      TextWindow.WriteLine ( time1)
      start2:
      Timer.Interval = 1000
      Timer.Tick = TheTimer
      TextWindow.ForegroundColor = "Magenta" 
      TextWindow.Write ("Ok! RSXPP is working! Just type")
      TextWindow.ForegroundColor = "Red" 
      TextWindow.Write (" Finish ")
      TextWindow.ForegroundColor = "Magenta" 
      TextWindow.WriteLine ("when you're done.")  
      TextWindow.ForegroundColor = "Gray"
        Else
      TextWindow.WriteLine ("Unknown command")
      Goto start
    Endif
    read2 = TextWindow.Read()
    If read2 = "Finish" Or read2 = "finish" Then
       TextWindow.ForegroundColor = "Magenta"
       TextWindow.WriteLine ("You've played = " + seconds + " seconds.")
       TextWindow.ForegroundColor = "Red" 
       time2 = Clock.Time
       TextWindow.WriteLine ( time2) 
       TextWindow.ForegroundColor = "Magenta"
       TextWindow.WriteLine ("How much XP do you have now?")
       TextWindow.ForegroundColor = "Gray"
       endxp = TextWindow.ReadNumber() 
       avghour = seconds / 3600
       madexp = endxp - startxp
       prexpgain = madexp / avghour
       xpgain = Math.Round (prexpgain)
       TextWindow.ForegroundColor = "Magenta"
       TextWindow.WriteLine ("Alright then! You've made " + madexp + " XP!") 
       TextWindow.WriteLine ("That means you've averaged " + xpgain + " XP an hour! Wow!")   
     Else    
       TextWindow.WriteLine ("Unknown command")
      Goto start2 
    Endif
     Sub TheTimer
     seconds = seconds + 1
    EndSub   



    Sunday, January 12, 2014 6:46 AM

Answers

  • Your seconds counter keeps counting during the wait for the number of XP. The calculation is done with the updated time. To show this: add a TextWindows.WriteLine(seconds)  after the output of the average, and wait a little time before entering the number of XP. You will see that the number of seconds in the calculation is different from the reported duration of the play.

    You can stop the timer by: Timer.Pause

    Tip: look also at the Text.ConvertToLowerCase command to convert "Start" to "start". This makes the if Read="start" easier


    Jan [ WhTurner ] The Netherlands


    Sunday, January 12, 2014 10:51 AM
    Answerer

All replies

  • Your seconds counter keeps counting during the wait for the number of XP. The calculation is done with the updated time. To show this: add a TextWindows.WriteLine(seconds)  after the output of the average, and wait a little time before entering the number of XP. You will see that the number of seconds in the calculation is different from the reported duration of the play.

    You can stop the timer by: Timer.Pause

    Tip: look also at the Text.ConvertToLowerCase command to convert "Start" to "start". This makes the if Read="start" easier


    Jan [ WhTurner ] The Netherlands


    Sunday, January 12, 2014 10:51 AM
    Answerer
  • Wow! That fixed it. I appreciate the help, as that problem puzzled me for quite a bit of time.
    Sunday, January 12, 2014 7:24 PM