如何使用嵌套的For循环显示数组值?

时间:2019-01-28 16:38:19

标签: c# arrays loops nested

在我们开始之前,我是编程和C#语言的新手,所以请多多包涵。 我遇到的问题是像这样在彼此前面显示两个单独的数组值:

整数类型:sbyte,byte,short,ushort
实际浮点类型:浮点,双精度

我尝试过的代码是:

string[] VariableTypes = { "Integer Type", "Real Floating Point Types" };

string[] IntergerTypes = { "sbyte", "byte", "short", "ushort" };

for (int i = 0; i <= VariableTypes.Length - 1; i++)
{
    for(int j=0; j<=IntergerTypes.Length - 1; j++)
    {
        Console.Write(IntergerTypes[j] + " ");
    }
    Console.WriteLine(VariableTypes[i] + " : ");
}

我得到的输出:

sbyte byte short ushort整数类型:
sbyte byte short ushort实浮点类型:

3 个答案:

答案 0 :(得分:0)

您需要另一个数组来保存要与Real Floating Point Types相关的值,然后有条件地检查变量类型以查看与之关联的整数类型。

string[] VariableTypes = { "Integer Type", "Real Floating Point Types" };
string[] FloatingPointTypes = { "float", "double" };
string[] IntergerTypes = { "sbyte", "byte", "short", "ushort" };

for (int i = 0; i <= VariableTypes.Length - 1; i++)
{
    Console.WriteLine(VariableTypes[i] + " : ");

    if(VariableTypes[i] == "Integer Type")
    {
        for(int j=0; j<=IntergerTypes.Length - 1; j++)
        {
            Console.Write(IntergerTypes[j] + ", ");
        }
    }
    else
    {
        for(int j=0; j<=FloatingPointTypes.Length - 1; j++)
        {
            Console.Write(FloatingPointTypes[j] + ", ");
        }
    }
}

我认为,如果您想要的只是一种实现上述输出的方法,那么在没有嵌套循环的情况下这样做会更干净:

string[] FloatingPointTypes = { "float", "double" };
string[] IntergerTypes = { "sbyte", "byte", "short", "ushort" };

Console.WriteLine("Integer Type: " + string.Join(", ", IntergerTypes));
Console.WriteLine("Real Floating Point Types: " + string.Join(", ", FloatingPointTypes));

此外,您可以利用一个类使您的工作更简化。请参阅以下内容:

public class VariableType
{
    public string Name { get; set; }

    public List<string> DataTypes { get; set; }
}

并通过以下步骤使用该类来实现所需的结果:

var variableTypes = new List<VariableType>
{
    new VariableType
    {
        Name = "Integer Types",
        DataTypes = new List<string>{ "sbyte", "byte", "short", "ushort" }
    },
    new VariableType
    {
        Name = "Real Floating Point Types",
        DataTypes = new List<string>{ "float", "double" }
    }
};

foreach(var variableType in variableTypes)
{
    Console.WriteLine(variableType.Name + " : " + string.Join(", ", variableType.DataTypes));
}

答案 1 :(得分:0)

from tkinter import *

from tkinter import messagebox
root = Tk() # creates a window and initializes the interpreter
root.geometry("500x300")

name = Label(root, text = "Name")
password = Label(root, text = "Password")
entry_1 = Entry(root)
entry_2 = Entry(root)



name.grid(row = 0, column = 0, sticky = E) # for name to be at right use sticky = E (E means east)
entry_1.grid(row = 0, column =1)
x = "Taha"
password.grid(row = 1, column = 0)
entry_2.grid(row = 1, column =1)
y = "123"
c = Checkbutton(root, text = "Keep in logged in").grid(columnspan = 2 ) # mergers the two columns


def next():

    a = entry_1.get()
    b = entry_2.get()

    if a == "Taha" and b =="123":
        messagebox.showinfo("Login", "successfuly logged in ")
        root.destroy()
        print ("Proceed")
    else:
        messagebox.showerror("Error", "wrong values entered")
        print("wrong values entered")
        root.destroy()



Next = Button(root, text = "Next", command=next).grid(row = 3, column = 1)




root.mainloop() # keep runing the code

答案 2 :(得分:0)

这是实现此目的的简短方法:

string[] VariableTypes = { "Integer Types", "Real Floating Point Types" };
string[] IntegerTypes = { "sbyte", "byte", "short", "ushort" };
string[] RealFloatingPointTypes = { "float", "double" };
Console.WriteLine(String.Concat(VariableTypes[0], " : ", String.Join(", ", IntegerTypes)));
Console.WriteLine(String.Concat(VariableTypes[1], " : ", String.Join(", ", RealFloatingPointTypes)));