c# - 如何在循环外重用 int。C#
问题描述
我创建了读取用户号码并将其更改为 int 的代码。基本上我的代码看起来像这样
while (e != 1)
{
int num = Convert.ToInt32(Console.ReadLine());
e += 1;
}
如何在循环外重用“int num”?
解决方案
您可以num
在循环外声明,但在循环内分配给它:
int num = 0; // Or some other default value
while (e != 1)
{
num = Convert.ToInt32(Console.ReadLine()); // Note that num is NOT declared here
e += 1;
}
// Use num here
推荐阅读
- python - Firebase ML Kit 使用自定义 TFLITE 为 Android 上的不同输出产生相同的推理
- php - 每小时最大查询数限制为 36 000。我每次浏览量有 100 个查询。每小时浏览 360 页,我崩溃了?
- bootstrap-4 - 清晰设计网格
- c# - 那么在实体框架核心中包含显式加载?
- ruby-on-rails - Rails 5.2:如何创建自引用重复记录
- java - AVRO Java 生成器 - 生成为 int 的日期
- r - 如何添加带有条件行的传单弹出窗口?
- javascript - 如何使用 Ava 创建参数测试
- ios - iOS - 在呈现另一个视图控制器时堆栈全屏视图控制器
- mysql - 无法以 root 身份使用 sudo 登录 MySQL