开关在do-while循环中不起作用(arraylist) [英] switch doesnt work in a do-while loop (arraylist)
本文介绍了开关在do-while循环中不起作用(arraylist)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用系统;
使用 System.Collections;
使用 System.Linq;
使用 System.Text;
命名空间 arraylist_exe
{
class 计划
{
静态 void Main( string [] args)
{
ArrayList arr = new ArrayList( 4 < /跨度>);
string choice;
执行
{
开关(选择) // 使用未分配的本地变量'choice'
{
case a:
Console.WriteLine( a-add,p-print,e-exit);
员工e = 新 employee();
e.id = int .Parse(Console.ReadLine());
e.name = Console.ReadLine();
arr.Add(e);
break ;
case p :
foreach (员工i in arr)
{
Console.WriteLine(i.name + 你的id是 + i.id);
}
break ;
默认:
Console.WriteLine( 无效选择);
break ;
}
} while (选择!= E跨度>);
}
}
class employee
{
public int id { get ; set ; }
public string name { get 跨度>; set ; }
}
}
解决方案
你没有指定变量选择
使用前。它甚至不应该编译。
-SA
字符串选择;
什么是没有分配的选择因此导致错误,
尝试这个,看看会发生什么,
string choice =p;
使用系统;
使用 System.Collections;
使用 System.Linq;
使用 System.Text;
命名空间 arraylist_exe
{
class 计划
{
静态 void Main( string [] args)
{
ArrayList arr = new ArrayList( 2 < /跨度>);
Console.WriteLine( a-add,p-print,e-exit) ;
string choice;
do
{
choice = Console.ReadLine();
switch (选择)
{
case :
员工e = new 雇员();
Console.WriteLine( 输入id);
e.id = int .Parse(Console.ReadLine());
Console.WriteLine( 输入名称);
e.name = Console.ReadLine();
arr.Add(e);
break ;
case p :
foreach (员工i in arr)
{
Console.WriteLine(i.name + 你的id是 + i.id);
}
break ;
默认:
Console.WriteLine( 无效选择);
break ;
}
} while (选择!= E跨度>);
}
}
class employee
{
public int id { get ; set ; }
public string name { get 跨度>; set ; }
}
}
//感谢所有前来帮忙的人,特别是richcb !
using System;
using System.Collections;
using System.Linq;
using System.Text;
namespace arraylist_exe
{
class Program
{
static void Main(string[] args)
{
ArrayList arr = new ArrayList(4);
string choice;
do
{
switch (choice) //use of unassigned local variable 'choice'
{
case "a":
Console.WriteLine("a-add,p-print,e-exit");
employee e = new employee();
e.id = int.Parse(Console.ReadLine());
e.name = Console.ReadLine();
arr.Add(e);
break;
case "p":
foreach (employee i in arr)
{
Console.WriteLine(i.name +"your id is"+i.id);
}
break;
default:
Console.WriteLine("invalid choice");
break;
}
} while (choice!="e");
}
}
class employee
{
public int id { get; set; }
public string name { get; set; }
}
}
解决方案
You don''t assign the variablechoice
before use. It should not even compile.
—SA
string choice;
What is choice assigned nothing so it causes the error,
try this and see what happens,
string choice = "p";
using System; using System.Collections; using System.Linq; using System.Text; namespace arraylist_exe { class Program { static void Main(string[] args) { ArrayList arr = new ArrayList(2); Console.WriteLine("a-add,p-print,e-exit"); string choice; do { choice = Console.ReadLine(); switch (choice) { case "a": employee e = new employee(); Console.WriteLine("enter id"); e.id = int.Parse(Console.ReadLine()); Console.WriteLine("enter name"); e.name = Console.ReadLine(); arr.Add(e); break; case "p": foreach (employee i in arr) { Console.WriteLine(i.name +"your id is"+i.id); } break; default: Console.WriteLine("invalid choice"); break; } } while (choice!="e"); } } class employee { public int id { get; set; } public string name { get; set; } } }
//thanks to all those who came to help, especialy to "richcb"!
这篇关于开关在do-while循环中不起作用(arraylist)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文