Multiplication table in Swift ios

2019-09-21 11:33发布

问题:

I am learning how to make a multiplication table in swift and used

override func viewDidLoad() {
    let n = Int(str)!

    while (i<=10) {


       let st = "\(n) * \(i) = \(n * i)"
       lbl.text = st

        i += 1

    }

this code. i have a label in which i want to show the table, but the problem is that only the last result which is say 2*10 = 20 is showing and not all the other value. i am confused what to do, please help what to do so that all the values are displayed.

回答1:

Glad you've decided to learn Swift. You're on the right track, but as others have said, your final iteration of the loop is replacing the contents of lbl.text.

There are many ways to achieve what you want, but for problems like this I'd suggest starting off working in a playground rather than worrying about labels, viewDidLoad and suchlike.

Here's a nice Swift-y way to do what you want

let n = 12
let table = Array(0...10).map({"\(n) * \($0) = \(n * $0)"}).joinWithSeparator("\n")
print("\(table)")

Gives…

12 * 0 = 0
12 * 1 = 12
12 * 2 = 24
12 * 3 = 36
12 * 4 = 48
12 * 5 = 60
12 * 6 = 72
12 * 7 = 84
12 * 8 = 96
12 * 9 = 108
12 * 10 = 120

To break that down…

      // Take numbers 0 to 10 and make an array 
Array(0...10).

      // use the map function to convert each member of the array to a string
      // $0 represents each value in turn. 
      // The result is an array of strings
map({"\(n) * \($0) = \(n * $0)"}).

      // Join all the members of your `String` array with a newline character
joinWithSeparator("\n")

Try it for yourself. In Xcode, File -> New -> Playground, and just paste in that code. Good luck!



回答2:

That's because every time the loop iterates, it overwrites the previous value in label.text. You need to append the new value to existing string value in label, as suggested by RichardG.

let n = Int(str)!

    while (i<=10) {


       let st = "\(n) * \(i) = \(n * i)"
       lbl.text = lbl.text + " " +st //Append new value to already existing value in label.text

        i += 1

    }

There is also a possibility of UI issue. You have to provide number of lines to the label or it will mess up the display. It also needs to be of size enough to hold your data. A better option would be UITextView which is scrollable, if you are unwilling to handle cases for label height and width. But if you want to stick with UILabel, the following code will resize the label depending on text for you:

lbl.numberOfLines = 0; //You only need to call this once. Maybe in `viewDidLoad` or Storyboard itself.
lbl.text = @"Some long long long text"; //here you set the text to label
[lbl sizeToFit]; //You must call this method after setting text to label

You can also handle that by Autolayout constraints.



回答3:

Easy way to do it with SWIFT 2.0

var tableOf = 2 //Change the table you want
for index in 1...10 {

    print("\(tableOf) X \(index) = \(index * tableOf)")

}

OUTPUT



回答4:

repeat-while loop, performs a single pass through the loop block first before considering the loop's condition (exactly what do-while loop does).

1) repeat...while Loop

var i = Int()
repeat {

    print("\(i) * \(i) = \(i * 11)")
    i += 1

} while i <= 11

2) While Loop

var i = Int()
while i <= 11
{

  print("\(i) * \(i) = \(i * 11)")
  i += 1

}

3) For Loop

for n in 1..<11
{
    print("\(n) * \(n) = \(n * 10)")

}