简体   繁体   中英

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' /// 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.

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. Admission fees are as follows: - under 16's £2.50 over 65 -£3 and all other swimmers - £5

A 20% discount should be applied to groups of more than 6 people.**

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

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
  • 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

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(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.

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.

  • The second problem are the brackets. You are closing the breakets in your if condition. 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. You use the binary AND & and not the logical 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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