c# - TryParse 循环中的两个变量

标签 c# algorithm tryparse

我是编程新手,我正在尝试创建一个循环,其中 while 条件基于变量,但我不知道它的正确语法。 这就是我想要做的,即使它可能不被允许......我认为。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            float totalArea, areaUnbuilt;

            while (!float.TryParse(Console.ReadLine(), out totalArea, out areaUnbuilt)) 
            {
                Console.Clear();
                Console.WriteLine("Insert total area in square meters:");
                float.TryParse(Console.ReadLine(), out totalArea);
                Console.WriteLine("Insira are not built in square meters:");
                float.TryParse(Console.ReadLine(), out areaUnbuilt);

            }
        }
    }
}

最佳答案

您不能使用 TryParse 取回两个输出值。而是将您的代码更改为 do..while 循环

bool isArea = true;
bool isUnbuilt = true;
do
{
    Console.Clear();
    Console.WriteLine("Insert total area in square meters:");
    isArea = float.TryParse(Console.ReadLine(), out totalArea);
    Console.WriteLine("Insert area not built in square meters:");
    isUnbuilt = float.TryParse(Console.ReadLine(), out areaUnbuilt);
} while( !isArea || !isUnbuilt);

关于c# - TryParse 循环中的两个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50777969/

相关文章:

Android: Ints.tryParse 始终返回 "null",即使字符串中有数字

c# - 在iOS 5上使用MonoTouch不能使用OpenGL ES 1.1深度缓冲区

c# - 对于以下情况,什么是好的设计模式?

c# - 忽略毫秒的 LINQ DateTime 查询

c++ - 在 C++ 中迭代具有可变维度大小的 N 维矩阵

c++ - 为什么 MCARDS 中需要前突变

algorithm - MAX-HEAPIFY : "the worst case occurs when the bottom level of the tree is exactly half full" 中的最坏情况

c# - 什么更好 : int. TryParse 或 try { int.Parse() } catch

c++ - 从文本文件中读取,存储数据的最佳方式 C++

c# - EF7 beta5 : Foreign Key returns null value