温度建议应用程序
Temperature Advice Application
第一个问题。任何建议都有帮助。
这是针对 class 的,尽管我正在尝试自己理解。我在编码时遇到语法错误的问题。此控制台应用程序的目标是让用户能够输入温度并就需要穿什么衣服提出建议(即 "Put on a light jacket")。
我之前已经完成了温度转换应用程序,并将我的代码添加到建议应用程序中。我查看了其他示例,但没有找到类似这样的 if...else 语句的简洁示例。
我认为错误是因为变量不是布尔值,但我不知道如何将它转换为布尔值,仅用于 if else 语句。
这是我目前拥有的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleF_to_C_App
{
class Program
{
static void Main(string[] args)
{
//declare a char variable to store the degree symbol
char chrDegree = (char)176;
//display program info
Console.WriteLine("Temperature Conversions with Advice (v.1) Sept 17, 2015");
Console.WriteLine("-------------------------------------------------------\n\n");
//prompt user to enter the temperature in F
Console.Write("Enter today's temperature in {0} F (eg 60): ", chrDegree);
//read in the user input
string strF = Console.ReadLine();
//declare two doubles to store F and C temperature
double dblF, dblC;
//convert input from string to double
dblF = Convert.ToDouble(strF);
//calculate celsius using fahrenheit
dblC = (dblF - 32) * 5 / 9;
Console.WriteLine("\n\nToday's Temperature: {0:F2}{1} F = {2:F2}{1} C \n\n",
dblF, chrDegree, dblC);
double temp = double.Parse(Console.ReadLine());
//if the user enters < 40
if (temp < 40)
{
Console.WriteLine("\n\nIt is very cold. Put on a heavy coat.");
}
else if
{
(temp > 40 || temp < 60)
Console.WriteLine("\n\nIt is cold. Put on a coat.");
}
else if
{
(temp >= 60 || temp < 70)
Console.WriteLine("\n\nThe temperature is cool. Put on a light jacket.");
}
else if
{
(temp >= 70 || temp < 80)
Console.WriteLine("\n\nThe temperature is pleasant. Wear anything you like.");
}
else if
{
(temp >= 80 || temp < 90)
Console.WriteLine("\n\nThe temperature is warm. Wear short sleeves.");
}
else if
{
(temp >= 90)
Console.WriteLine("\n\nIt is hot. Wear shorts today.");
}
Console.WriteLine("Thank you for using the Temperature Conversion Application.\n\n");
//ask if the user wants to continue
Console.Write("Do you want to continue Y/N ? ");
//reads in the user input
strContinue = Console.ReadLine();
Console.WriteLine("\n\n");
//if the user enters N or n
if (strContinue == "N" || strContinue == "n")
{
//set the bool variable to false
boolContinue = false;
}
//otherwise
else
{
//set the boolean variable to true
boolContinue = true;
}
Console.ReadKey();
}
}
}
你在这些地方遇到语法错误。
else if
{
(temp > 40 || temp < 60)
Console.WriteLine("\n\nIt is cold. Put on a coat.");
}
语法是 if( expression) { /* ... */ }
,所以 (
必须紧跟在 if
之后。这是正确的:
else if (temp > 40 || temp < 60)
{
Console.WriteLine("\n\nIt is cold. Put on a coat.");
}
此外,您忘记将此变量声明为 string
。
strContinue = Console.ReadLine();
并且您确实正确地将此布尔值设置为 true
或 false
,因此您只需将 bool boolContinue = true;
的声明移动到 [= 的开头20=] 函数,将所有现有代码包装在 while(boolContinue)
表达式中。
你的一个问题是你的比较。
else if
{
(temp >= 90)
Console.WriteLine("\n\nIt is hot. Wear shorts today.");
}
必须...
else if (temp >= 90)
{
Console.WriteLine("\n\nIt is hot. Wear shorts today.");
}
感谢 Maximilian,我已经解决了。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//declare a char variable to store the degree symbol
char chrDegree = (char)176;
Boolean boolContinue = true;
string strContinue;
//declare two doubles to store F and C temperature
double dblF, dblC;
while (boolContinue == true)
{
//display program info
Console.WriteLine("Temperature Conversions with Advice (v.1) Sept 17, 2015");
Console.WriteLine("-------------------------------------------------------\n\n");
//prompt user to enter the temperature in F
Console.Write("Enter today's temperature in {0} F (eg 60): ", chrDegree);
//read in the user input
string strF = Console.ReadLine();
//convert input from string to double
dblF = Convert.ToDouble(strF);
//calculate celsius using fahrenheit
dblC = (dblF - 32) * 5 / 9;
Console.WriteLine("\n\nToday's Temperature: {0:F2}{1} F = {2:F2}{1} C \n\n",
dblF, chrDegree, dblC);
//if the user enters < 40
if (dblF < 40)
{
Console.WriteLine("\n\nIt is very cold. Put on a heavy coat.");
}
else if (dblF > 40 && dblF < 60)
{
Console.WriteLine("\n\nIt is cold. Put on a coat.");
}
else if (dblF >= 60 && dblF < 70)
{
Console.WriteLine("\n\nThe temperature is cool. Put on a light jacket.");
}
else if (dblF >= 70 && dblF < 80)
{
Console.WriteLine("\n\nThe temperature is pleasant. Wear anything you like.");
}
else if (dblF >= 80 && dblF < 90)
{
Console.WriteLine("\n\nThe temperature is warm. Wear short sleeves.");
}
else if (dblF >= 90)
{
Console.WriteLine("\n\nIt is hot. Wear shorts today.");
}
Console.WriteLine("Thank you for using the Temperature Conversion Application.\n\n");
//ask if the user wants to continue
Console.Write("Do you want to continue Y/N ? ");
//reads in the user input
strContinue = Console.ReadLine();
Console.WriteLine("\n\n");
//if the user enters N or n
if (strContinue == "N" || strContinue == "n")
{
//set the bool variable to false
boolContinue = false;
}
//otherwise
else
{
//set the boolean variable to true
boolContinue = true;
}
Console.ReadKey();
}
}
}
}
^^^
这个有用,谢谢!
第一个问题。任何建议都有帮助。
这是针对 class 的,尽管我正在尝试自己理解。我在编码时遇到语法错误的问题。此控制台应用程序的目标是让用户能够输入温度并就需要穿什么衣服提出建议(即 "Put on a light jacket")。
我之前已经完成了温度转换应用程序,并将我的代码添加到建议应用程序中。我查看了其他示例,但没有找到类似这样的 if...else 语句的简洁示例。
我认为错误是因为变量不是布尔值,但我不知道如何将它转换为布尔值,仅用于 if else 语句。
这是我目前拥有的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleF_to_C_App
{
class Program
{
static void Main(string[] args)
{
//declare a char variable to store the degree symbol
char chrDegree = (char)176;
//display program info
Console.WriteLine("Temperature Conversions with Advice (v.1) Sept 17, 2015");
Console.WriteLine("-------------------------------------------------------\n\n");
//prompt user to enter the temperature in F
Console.Write("Enter today's temperature in {0} F (eg 60): ", chrDegree);
//read in the user input
string strF = Console.ReadLine();
//declare two doubles to store F and C temperature
double dblF, dblC;
//convert input from string to double
dblF = Convert.ToDouble(strF);
//calculate celsius using fahrenheit
dblC = (dblF - 32) * 5 / 9;
Console.WriteLine("\n\nToday's Temperature: {0:F2}{1} F = {2:F2}{1} C \n\n",
dblF, chrDegree, dblC);
double temp = double.Parse(Console.ReadLine());
//if the user enters < 40
if (temp < 40)
{
Console.WriteLine("\n\nIt is very cold. Put on a heavy coat.");
}
else if
{
(temp > 40 || temp < 60)
Console.WriteLine("\n\nIt is cold. Put on a coat.");
}
else if
{
(temp >= 60 || temp < 70)
Console.WriteLine("\n\nThe temperature is cool. Put on a light jacket.");
}
else if
{
(temp >= 70 || temp < 80)
Console.WriteLine("\n\nThe temperature is pleasant. Wear anything you like.");
}
else if
{
(temp >= 80 || temp < 90)
Console.WriteLine("\n\nThe temperature is warm. Wear short sleeves.");
}
else if
{
(temp >= 90)
Console.WriteLine("\n\nIt is hot. Wear shorts today.");
}
Console.WriteLine("Thank you for using the Temperature Conversion Application.\n\n");
//ask if the user wants to continue
Console.Write("Do you want to continue Y/N ? ");
//reads in the user input
strContinue = Console.ReadLine();
Console.WriteLine("\n\n");
//if the user enters N or n
if (strContinue == "N" || strContinue == "n")
{
//set the bool variable to false
boolContinue = false;
}
//otherwise
else
{
//set the boolean variable to true
boolContinue = true;
}
Console.ReadKey();
}
}
}
你在这些地方遇到语法错误。
else if
{
(temp > 40 || temp < 60)
Console.WriteLine("\n\nIt is cold. Put on a coat.");
}
语法是 if( expression) { /* ... */ }
,所以 (
必须紧跟在 if
之后。这是正确的:
else if (temp > 40 || temp < 60)
{
Console.WriteLine("\n\nIt is cold. Put on a coat.");
}
此外,您忘记将此变量声明为 string
。
strContinue = Console.ReadLine();
并且您确实正确地将此布尔值设置为 true
或 false
,因此您只需将 bool boolContinue = true;
的声明移动到 [= 的开头20=] 函数,将所有现有代码包装在 while(boolContinue)
表达式中。
你的一个问题是你的比较。
else if
{
(temp >= 90)
Console.WriteLine("\n\nIt is hot. Wear shorts today.");
}
必须...
else if (temp >= 90)
{
Console.WriteLine("\n\nIt is hot. Wear shorts today.");
}
感谢 Maximilian,我已经解决了。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//declare a char variable to store the degree symbol
char chrDegree = (char)176;
Boolean boolContinue = true;
string strContinue;
//declare two doubles to store F and C temperature
double dblF, dblC;
while (boolContinue == true)
{
//display program info
Console.WriteLine("Temperature Conversions with Advice (v.1) Sept 17, 2015");
Console.WriteLine("-------------------------------------------------------\n\n");
//prompt user to enter the temperature in F
Console.Write("Enter today's temperature in {0} F (eg 60): ", chrDegree);
//read in the user input
string strF = Console.ReadLine();
//convert input from string to double
dblF = Convert.ToDouble(strF);
//calculate celsius using fahrenheit
dblC = (dblF - 32) * 5 / 9;
Console.WriteLine("\n\nToday's Temperature: {0:F2}{1} F = {2:F2}{1} C \n\n",
dblF, chrDegree, dblC);
//if the user enters < 40
if (dblF < 40)
{
Console.WriteLine("\n\nIt is very cold. Put on a heavy coat.");
}
else if (dblF > 40 && dblF < 60)
{
Console.WriteLine("\n\nIt is cold. Put on a coat.");
}
else if (dblF >= 60 && dblF < 70)
{
Console.WriteLine("\n\nThe temperature is cool. Put on a light jacket.");
}
else if (dblF >= 70 && dblF < 80)
{
Console.WriteLine("\n\nThe temperature is pleasant. Wear anything you like.");
}
else if (dblF >= 80 && dblF < 90)
{
Console.WriteLine("\n\nThe temperature is warm. Wear short sleeves.");
}
else if (dblF >= 90)
{
Console.WriteLine("\n\nIt is hot. Wear shorts today.");
}
Console.WriteLine("Thank you for using the Temperature Conversion Application.\n\n");
//ask if the user wants to continue
Console.Write("Do you want to continue Y/N ? ");
//reads in the user input
strContinue = Console.ReadLine();
Console.WriteLine("\n\n");
//if the user enters N or n
if (strContinue == "N" || strContinue == "n")
{
//set the bool variable to false
boolContinue = false;
}
//otherwise
else
{
//set the boolean variable to true
boolContinue = true;
}
Console.ReadKey();
}
}
}
}
^^^ 这个有用,谢谢!