我最近被介绍到一个大型代码库,并注意到所有的字符串比较都使用String.Equals()而不是==
你觉得这是什么原因?
我有这个代码:
private double Price;
private bool Food;
private int count;
private decimal finalprice;
public void Readinput()
{
Console.Write("Unit price: ");
Price = Console.ReadLine();
Console.Write("Food item y/n: ");
Food = Console.ReadLine();
Console.Write("Count: ");
count = Console.ReadLine();
}
private void calculateValues()
{
finalprice = Price * count;
}
Run Code Online (Sandbox Code Playgroud)
并得到以下错误:
无法将类型'string'隐式转换为'bool'
无法将类型'string'隐式转换为'double'
无法将类型'string'隐式转换为'int'
无法将类型'double'隐式转换为'decimal'.存在显式转换(您是否错过了演员?)
我知道这意味着什么,但我不知道如何解决它.
我对语言很新,我不是一个优秀的程序员.这段代码给了我错误:
不能将int类型隐式转换为bool.
我不确定我做错了什么.有些人可以告诉我我做错了什么.任何建议也会有所帮助.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class mysteryVal
{
public const int limitOfGuess = 5;
// Data member
public int mystVal;
private int numOfGuess ;
private randomNumberMagnifier mag = new randomNumberMagnifier();
public int randomMag(int num)
{
return num + mystVal;
}
// Instance Constructor
public mysteryVal()
{
mystVal = 0;
numOfGuess = 0;
}
public void game(int user)
{
int userInput = user;
if (numOfGuess < limitOfGuess)
{ …Run Code Online (Sandbox Code Playgroud) 我是C#的新手,我正在使用微软的Visual Studio Express 2013 Windows桌面版本,我正在尝试进行一个测验,我问这个问题,用户必须回答它,这里是代码,我得到的错误是"无法将类型'string'隐式转换为'bool'",这发生在2 if语句中,我知道bool的值为true或false但是它是一个字符串,为什么它会给我这个错误?任何帮助应该被赞赏. PS:我只包含了我遇到问题的代码部分,这是主类中唯一的代码
下面是代码:
Start:
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Question 1: Test? type yes or no: ");
String answer1 = Console.ReadLine();
if (answer1 = "yes") {
Console.WriteLine();
Console.WriteLine("Question 2: Test? type Yes or no");
}
else if (answer1 = "no")
{
Console.WriteLine();
Console.WriteLine("Wrong, restarting program");
goto Start;
}
else {
Console.WriteLine();
Console.WriteLine("Error");
goto Start;
}
Run Code Online (Sandbox Code Playgroud)