繁体   English   中英

错误CS1002 :; 预期&(31,13):错误CS1525:无效的表达式术语'else'

[英]error CS1002: ; expected & (31,13): error CS1525: Invalid expression term 'else'

布局有问题,我知道我很接近。 ///我有两个错误是( 31,18): error CS1002: ; expected & (31,13): error CS1525: Invalid expression term 'else' 31,18): error CS1002: ; expected & (31,13): error CS1525: Invalid expression term 'else' ///我试图做了所说的内容并且它犯了更多错误,所以我完全不知道如何让这个工作现在。

目前我有这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CE0721a
{
    class Tut4_7
    {
        public void run()
          {
            double under16_Free = 2.5;
            double oap = 3.0;
            double other = 5.0;
            int groupSize = 0;
            double totalCost = 0;
            int age = 0;

        while (age!=-1)
        {
            Console.WriteLine("Enter an Age or -1 to quit");
            age=int.Parse(Console.ReadLine());

            if(age<16)&(age>-1);
            {
                groupSize++;
                totalCost = totalCost + under16_Free;
            }
            else if(age>16)
            {
                groupSize++;
                totalCost = totalCost + oap;
            }
            else if(age>16)&(age<=65);
            {
                groupSize++;
                totalCost = totalCost + other;
            }
        }
        if (groupSize>6)
        {
            totalCost = totalCost - (totalCost/5);
        }
        Console.WriteLine("Total Cost = "(totalCost));
        }
    }
}

我想这样做

**为当地游泳池写一个节目,根据他们的年龄显示一群人的入场费。 程序应继续提示用户输入年龄,直到输入-1,然后显示该组中的总人数以及该组的总成本。 入场费如下: - 65岁以下的16英镑+2.50英镑和所有其他游泳运动员 - 5英镑

超过6人的团体应享受20%的折扣。**

这是连接到另一个名为Program.cs的cs,这样的布局和工作,因为我已经测试了其他cs文件,这显示程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CE0721a
{
    class Program
    {
        static void Main(string[] args)
        {
            Tut4_7 myProgram = new Tut4_7();
            myProgram.run();
        }
    }
}

再次感谢,如果有人可以提供帮助

一些语法问题:

  • if语句的开头行不应以半冒号结尾
  • 你的化合物比较需要另一组括号
  • 你几乎总是想用&&而不是&因为前者会短路

所以

if(age<16)&(age>-1);

应该成为

if ((age<16) && (age>-1))

或者,正如@Servy在评论中提到的那样

if (age<16 && age>-1)

在一些if / else语句的末尾有分号,并且你使用了错误的逻辑运算符:

if(age<16)&(age>-1);

应该

if(age < 16 && age >= 1)
{
    //Code here
}

else if(age>16)&(age<=65);

应该

else if(age > 16 && age <= 65)
{
    //Code here
}

你想使用&&运算符而不是&运算符,@ Servvy在注释中给出了原因的一个很好的解释。

if(age<16)&(age>-1);

三个问题:

  • 首先,你不应该添加; 在if语句之后,这将导致无法执行任何操作(空语句)。

  • 第二个问题是括号。 您正在关闭if条件中的breakets。 您无法关闭它们并重新打开它们,但您可以使用括号分隔外部括号中的部件。

  • 最后一个问题是AND运算符。 您使用二进制AND&而不是逻辑AND &&。

该行应如下所示:

if(age<16 && age>-1)

同样的事情适用于另一条线。

尝试这个:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CE0721a
{
    class Tut4_7
    {
        public void run() 
          { 
            double under16_Free = 2.5; 
            double oap = 3.0; 
            double other = 5.0; 
            int groupSize = 0; 
            double totalCost = 0; 
            int age = 0; 

        while (age!=-1) 
        { 
            Console.WriteLine("Enter an Age or -1 to quit"); 
            age=int.Parse(Console.ReadLine()); 

            if((age<16)&&(age>-1))
            { 
                groupSize++; 
                totalCost = totalCost + under16_Free; 
            } 
            else if(age>16) 
            { 
                groupSize++; 
                totalCost = totalCost + oap; 
            } 
            else if((age>16)&&(age<=65))
            { 
                groupSize++; 
                totalCost = totalCost + other; 
            } 
        } 
        if (groupSize>6) 
        { 
            totalCost = totalCost - (totalCost/5); 
        } 
        Console.WriteLine("Total Cost = " + totalCost); 
        }
    }
}

对于初学者来说:

 if(age<16)&(age>-1);

应该是&& 你想要一个逻辑而不是一个按位和。

你可以试试

if ( (age<16) & (age>-1))

句法

if(condition)
{
  //Treatment
} 

你的if语句形成错误,你使用的是二进制而不是逻辑&&而且两个条件只需要在一个单独的()中,最重要的是你不需要在if语句之后进行半冒号,这表示结束

namespace CE0721a
{
    class Tut4_7
    {
        public void run()
          {
            double under16_Free = 2.5;
            double oap = 3.0;
            double other = 5.0;
            int groupSize = 0;
            double totalCost = 0;
            int age = 0;

        while (age!=-1)
        {
            Console.WriteLine("Enter an Age or -1 to quit");
            age=int.Parse(Console.ReadLine());

            if(age<16 && age>-1)
            {
                groupSize++;
                totalCost = totalCost + under16_Free;
            }
            else if(age>16)
            {
                groupSize++;
                totalCost = totalCost + oap;
            }
            else if(age>16 && age<=65)
            {
                groupSize++;
                totalCost = totalCost + other;
            }
        }
        if (groupSize>6)
        {
            totalCost = totalCost - (totalCost/5);
        }
        Console.WriteLine("Total Cost = "(totalCost));
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM