简体   繁体   English

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

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

I am having trouble with layout, I know I am close. 布局有问题,我知道我很接近。 /// I have two errors that are ( 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' 31,18): error CS1002: ; expected & (31,13): error CS1525: Invalid expression term 'else' /// I have tried to do what was said and it made more errors, so I am totally don't know how to make this work now. 31,18): error CS1002: ; expected & (31,13): error CS1525: Invalid expression term 'else' ///我试图做了所说的内容并且它犯了更多错误,所以我完全不知道如何让这个工作现在。

At the moment I have this 目前我有这个

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

I am trying to do this 我想这样做

**Write a program, for the local swimming pool that displays the admission cost for a group of people based on their age. **为当地游泳池写一个节目,根据他们的年龄显示一群人的入场费。 The program should continue to prompt the user to enter an age until –1 is entered, and then display the total number of people in the group and the total cost for that group. 程序应继续提示用户输入年龄,直到输入-1,然后显示该组中的总人数以及该组的总成本。 Admission fees are as follows: - under 16's £2.50 over 65 -£3 and all other swimmers - £5 入场费如下: - 65岁以下的16英镑+2.50英镑和所有其他游泳运动员 - 5英镑

A 20% discount should be applied to groups of more than 6 people.** 超过6人的团体应享受20%的折扣。**

This is connected to another cs called Program.cs, laid out like this and works, because I have tested with other cs files, this displays the program 这是连接到另一个名为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();
        }
    }
}

Thanks again, if anyone can help 再次感谢,如果有人可以提供帮助

A few syntax issues: 一些语法问题:

  • the opening line of an if statement should not end in a semi colon if语句的开头行不应以半冒号结尾
  • you need another set of parentheses around your compound comparison 你的化合物比较需要另一组括号
  • you almost always want to use && instead of & as the former will short circuit 你几乎总是想用&&而不是&因为前者会短路

So 所以

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

Should become 应该成为

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

Or, as @Servy mentioned in the comments 或者,正如@Servy在评论中提到的那样

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

You have semi-colons at the end of a few of your if/else statements, and you're using the wrong logical operator: 在一些if / else语句的末尾有分号,并且你使用了错误的逻辑运算符:

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

should be 应该

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

and

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

should be 应该

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

You want to use the && operator instead of the & operator, and @Servy gives a nice explanation of why in the comments. 你想使用&&运算符而不是&运算符,@ Servvy在注释中给出了原因的一个很好的解释。

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

Three problems: 三个问题:

  • First, you shouldn't add a ; 首先,你不应该添加; after the if statement, that would cause nothing (empty statement) to get executed. 在if语句之后,这将导致无法执行任何操作(空语句)。

  • The second problem are the brackets. 第二个问题是括号。 You are closing the breakets in your if condition. 您正在关闭if条件中的breakets。 You can't close them and reopen them, but you could use brackets to seperate parts in the outer brackets. 您无法关闭它们并重新打开它们,但您可以使用括号分隔外部括号中的部件。

  • The last problem is the AND operator. 最后一个问题是AND运算符。 You use the binary AND & and not the logical AND &&. 您使用二进制AND&而不是逻辑AND &&。

The line should look like: 该行应如下所示:

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

The same thing applies to the other line. 同样的事情适用于另一条线。

Try this: 尝试这个:

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

For starters this: 对于初学者来说:

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

Should be && . 应该是&& You want a logical and not a bitwise and. 你想要一个逻辑而不是一个按位和。

You can try with 你可以试试

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

Syntax 句法

if(condition)
{
  //Treatment
} 

Your if statement are badly formed, you are using the binary & not the logical && and both conditions only need to be in a single () , and most importantly you don't need to semi colon after a if statment, that signifys the end 你的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