如何生成将数字标识为布尔值的函数? [英] How do I generate a function that identify a number as boolean ?

查看:80
本文介绍了如何生成将数字标识为布尔值的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 

命名空间 ADP_PG2_1_Solution
{
public class IsDualNumberExercise
{
public static bool IsDualNumber( string s){

// 用户输入
Console.WriteLine( Bitte geben Sie eine Zahl: + s);
s = Console.ReadLine();

// 我们现在需要将上面给出的数字比作1或0
for int i = 0 ; i < s.Length; i ++)
{
if (s [i] == 1 || s [i] == 0
{
Console.WriteLine( 你给出的数字是布尔值< /跨度>,S);
}
其他
{
// < span class =code-comment> if(s ist neighter of them)

Console.WriteLine( 不是布尔值);
}
}

返回 false ;
}
}
}





我尝试了什么:



它不起作用:-(有人可以告诉我为什么吗?

谢谢

解决方案

这是一些奇怪的代码:你正在将一个字符串传递给方法但是立即覆盖它。你不能使新的字符串可用于调用IsDualNumber的方法,因为字符串是不可变的(这意味着它们不能被更改 - 每次尝试时都会生成一个新字符串),因为你传递的是对字符串的引用而不是字符串内容。所以传入一个字符串是没有意义的!

相反,我' d读取调用方法中的字符串,并将其传递给IsDualNumber方法。

然后,你有下一个问题 - 这是一个字符串是 char <的集合/ code> values。所以当你使用索引时,要检索一个值,它返回一个char - 而'1'与1不同,'0'与0不同。 br />
你的方法总是返回一个 false - 所以它根本不是一个bool!

我会用不同的方式做:

  string  s = Console.ReadLine(); 
if (IsDualNumber(s))
{
Console.WriteLine( 数字\{0} \是一个布尔,s);
}
else
{
Console.WriteLine( \{0} \不是布尔值,s);
}
...
public static bool IsDualNumber( string s)
{
foreach char c in s)
{
if (c!= ' 1'&& ; c!= ' 0' // false如果不是'1'而不是'0'
{
return false ;
}
}
返回 true ;
}


执行readline时,它是一个字符串。

您可能需要考虑以下内容:



if(s [i] ==1|| s [i] ==0)







if(Convert.ToInt(s [i])== 1 || Convert.ToInt(s [i])== 0 )



希望它有所帮助。

您也可以尝试转换为布尔值但是当它不是1时你必须处理异常0

using System;

namespace ADP_PG2_1_Solution
{
	public class IsDualNumberExercise
	{
		public static bool IsDualNumber(string s) {
           
            //User imput 
            Console.WriteLine("Bitte geben Sie eine Zahl:" + s);
            s = Console.ReadLine();

            //We need now to compare the above given number to "1" or "0"
            for(int i =0; i < s.Length; i++)
            {
               if(s[i] == 1 || s[i] == 0)
                {
                    Console.WriteLine("The number you've given is a boolean",s);
                }
                else
                {
                    // if(s ist neighter of them)
                    Console.WriteLine("Is not a boolean");
                }
            }

			return false;
		}
	}
}



What I have tried:

It doesnt work :-( Can someone plse tell me why ?
Thanks

解决方案

That's some odd code: You are passing a string into the method but immediately overwriting it. You can't make the new string available to the method that calls IsDualNumber because strings are immutable (which means that they can't be changed - a new string is generated each time you try) and because what you pass is a reference to the string rather than the string content. So passing a string in is pointless!
Instead, I'd read the string in the calling method, and pass that to the IsDualNumber method.
Then, you have the next problem - which is that a string is a Collection of char values. So when you use an index, to retrieve a value it returns a single char - and '1' is not the same as 1, '0' is not the same as 0.
Your method always returns a false - so it's not really a bool at all!
I'd do it differently:

string s = Console.ReadLine();
if (IsDualNumber(s))
   {
   Console.WriteLine("The number \"{0}\" is a boolean", s);
   }
else
   {
    Console.WriteLine("\"{0}\" is not a boolean", s);
   }
...
public static bool IsDualNumber(string s) 
    {
    foreach (char c in s)
        {
        if(c != '1' && c != '0')    // false if NOT '1' and NOT '0' 
            {
            return false;
            }
        }
    return true;
    }


When you do a readline, it is a string.
You might want to consider the below:

if(s[i] == "1" || s[i] == "0")

OR

if(Convert.ToInt(s[i]) == 1 || Convert.ToInt(s[i]) == 0)

Hope it helps.
You can also try converting to boolean but you will have to handle the exception when it is not 1 or 0.


这篇关于如何生成将数字标识为布尔值的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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