如何将用户输入放入C#中的队列? [英] How do I put user input into the queue in C#?

查看:42
本文介绍了如何将用户输入放入C#中的队列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<pre>using System;
using System.Collections;
using System.Collections.Generic;
class MainClass {
  public static void Main (string[] args) {
    Queue eleven = new Queue();
    Queue twelve = new Queue();
    Queue others = new Queue();
    string name;  
    string school;
    string gender;
    string year;
    int intVal;
    while(true)
    {
    Console.WriteLine("Name?");
    name = Console.ReadLine();
    Console.WriteLine("School");
    school = Console.ReadLine(); 
    Console.WriteLine("Gender");
    gender = Console.ReadLine();
    Console.WriteLine("Year");
    year= Console.ReadLine();
    intVal = Convert.ToInt32(year);
    if (intVal > 10 && intVal <= 11)
    {
    eleven.Enqueue(intVal); 
    foreach( object obj in eleven)
    Console.WriteLine("year 11: Name: {0}, School: {1}, Gender: {2}" ,name, school, gender);
    }
    if (intVal > 11 && intVal <= 12)
    {
    twelve.Enqueue(intVal);
    foreach( object objj in twelve)
    Console.WriteLine("year 12: Name: {0}, School: {1}, Gender: {2}" ,name, school, gender);
    }
    if(intVal >= 13 || intVal <= 10 )
    {
    others.Enqueue(intVal);
    foreach( object objjj in others)
    Console.WriteLine("Others: Name: {0}, School: {1}, Gender: {2}" ,name, school, gender);
    }
  }
}
}



为什么每次我放这个代码让我们说第11年stdent并放另一个11年级学生而不是打印2个不同的11年级学生,它将两次显示相同的一个????亲自试试.....我找不到任何问题。



我尝试过的事情:



我不知道问题是这样,不知道该尝试什么。


why is this code everytime i put lets say year 11 stdent and put another year 11 student rather than print 2 different year 11 student it will display the same one twice???? try it out youself..... i can't find any problems in it.

What I have tried:

I do not know theproblem is so do not know what to try.

推荐答案

foreach( object obj in eleven)
Console.WriteLine("year 11: Name: {0}, School: {1}, Gender: {2}" ,name, school, gender);



为队列中的每个obj,你打印一些与队列无关的东西。

----

学会正确缩进代码,显示其结构,有助于阅读和理解。它还有助于发现结构错误。


for each obj in the queue, you print something that is not related to the queue.
----
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.

<pre>using System;
using System.Collections;
using System.Collections.Generic;
class MainClass {
    public static void Main (string[] args) {
        Queue eleven = new Queue();
        Queue twelve = new Queue();
        Queue others = new Queue();
        string name;
        string school;
        string gender;
        string year;
        int intVal;
        while(true)
        {
            Console.WriteLine("Name?");
            name = Console.ReadLine();
            Console.WriteLine("School");
            school = Console.ReadLine();
            Console.WriteLine("Gender");
            gender = Console.ReadLine();
            Console.WriteLine("Year");
            year= Console.ReadLine();
            intVal = Convert.ToInt32(year);
            if (intVal > 10 && intVal <= 11)
            {
                eleven.Enqueue(intVal);
                foreach( object obj in eleven)
                    Console.WriteLine("year 11: Name: {0}, School: {1}, Gender: {2}" ,name, school, gender);
            }
            if (intVal > 11 && intVal <= 12)
            {
                twelve.Enqueue(intVal);
                foreach( object objj in twelve)
                    Console.WriteLine("year 12: Name: {0}, School: {1}, Gender: {2}" ,name, school, gender);
            }
            if(intVal >= 13 || intVal <= 10 )
            {
                others.Enqueue(intVal);
                foreach( object objjj in others)
                    Console.WriteLine("Others: Name: {0}, School: {1}, Gender: {2}" ,name, school, gender);
            }
        }
    }
}



缩进样式 - 维基百科 [ ^ ]



专业程序员的编辑器具有此功能,其他功能包括括号匹配和语法高亮。 />
Notepad ++ Home [ ^ ]

ultraedit [< a href =http://www.ultraedit.com/target =_ blanktitle =新窗口> ^ ]

-----

你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:在调试器上运行你的代码一步一步,检查变量。

调试器就在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道您的代码应该做什么,它不要发现错误,只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



在Visual Studio中调试C#代码 - YouTube [ ^ ]



这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。


Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于如何将用户输入放入C#中的队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆