2015-09-22 3 views
0

Первый вопрос. Любые советы помогают.Приложение для оценки температуры

Это для класса, хотя я пытаюсь понять сам. У меня проблемы с синтаксическими ошибками в моей кодировке. Цель этого консольного приложения заключается в том, чтобы пользователь мог ввести температуру и дать рекомендации относительно того, какая одежда необходима (т. Е. «Надеть легкую куртку»).

Я сделал приложение для преобразования температуры до этого и добавил свой код в приложение для консультаций. Я посмотрел на другие примеры и не нашел каких-либо кратких примеров для 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(); 

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

И вы правильно установить это логическое значение true или false, так что вам просто нужно переместить декларацию 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(); 
      } 

     } 
    } 
} 

^^^ Это работает, спасибо!

Смежные вопросы