P
P
Ptichka0072021-08-23 11:03:53
C++ / C#
Ptichka007, 2021-08-23 11:03:53

How to display multiplication table in unity?

I'm trying to display the multiplication table on the program interface in unity. I tried , but I get an error: Cannot implicitly convert type string, string y to string. What can be done here?
text.text = ("{0}\t", y);

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;

public class task_2 : MonoBehaviour
{
    [SerializeField] Text text;

    public void Main()
    {
        for (int i = 1; i <= 10; i++)
        {
            for (int j = 1; j <= 10; j++)
            {
                int x = i * j;
                string y = x.ToString();
                text.text = ("{0}\t", y);
            }
        }    
            
                

    }

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pashara, 2021-08-24
@Ptichka007

Text output can also be done like this:
text.text = $"{y}\t";

string multiplyTable = string.Empty;
for (int i = 1; i <= 10; i++)
{
    for (int j = 1; j <= 10; j++)
    {
        multiplyTable += $"{i} * {j} = {i * j}\n";
    }
    multiplyTable += "\n";
}
text.text = multiplyTable;

A
AndromedaStar, 2021-08-23
@AndromedaStar

Wrong syntax, on the right side of the expression you are creating a Tuple.
This is such a thing:
https://docs.microsoft.com/ru-ru/dotnet/api/system...
Apparently they forgot String.Format.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question