2015-09-22 20 views
0

第一個問題。任何建議幫助。溫度建議應用

這是一個班,雖然我試圖自己理解。我在編碼中遇到了語法錯誤。這個控制檯應用程序的目標是讓用戶能夠輸入溫度,並根據需要提供什麼樣的衣服建議(例如「穿上輕便的外套」)。

我在此之前完成了溫度轉換應用程序,並將我的代碼添加到了建議應用程序中。我看了其他例子,並沒有發現任何簡潔的例子,如果... 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(); 

     } 
    } 
} 
+1

我把我的水晶球留在家裏 - 你看到什麼語法錯誤? – Tim

+0

strContinue沒有定義? –

回答

0

你在這些地方得到語法錯誤。

else if 
{ 
    (temp > 40 || temp < 60) 
    Console.WriteLine("\n\nIt is cold. Put on a coat."); 
} 

的語法if(expression) { /* ... */ },所以(必須去directlyafter的if。這是正確的:

else if (temp > 40 || temp < 60) 
{ 
    Console.WriteLine("\n\nIt is cold. Put on a coat."); 
} 

而且,你忘了聲明這個變量作爲string

strContinue = Console.ReadLine(); 

而且你正確設置此布爾值,以truefalse,所以你只需要到bool boolContinue = true;的聲明移到Main()功能,套所有現有的代碼開始在while(boolContinue)表達。

0

你有一個問題是你的比較。

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."); 
} 
0

感謝馬克西米利安,我有它的工作。

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(); 
      } 

     } 
    } 
} 

^^^ 這工作,謝謝!