1

我是编程新手,正在尝试学习 c#。我的控制台应用程序遇到了我认为是一个简单的问题(代码如下)。该错误似乎与我的 if 语句有关,内容如下:

//evaluate subtotals to results
        if (subTotalOne == subTotalTwo)
        {
            Console.WriteLine("=");
        }
        else if (subTotalOne < subTotalTwo)
        {
            Console.WriteLine("<");
        }
        else (subTotalOne > subTotalTwo);
        { 
            Console.WriteLine(">"); 
        }            

我得到的错误是:只有赋值、调用、递增、递减和新对象表达式可以用作语句。

任何帮助,将不胜感激。我已经阅读了这里的论坛并看到了可能类似的问题,但我的理解还不足以将我看到的解决方案映射到我的问题。

完整应用代码:

using System;

命名空间 itc110_a02_GroceryComparison { 类程序 { 静态无效 Main(string[] args) {

        Console.WriteLine("Compare Grocery Stores "); //Alert user to purpose of program with title
        Console.WriteLine("\n");//line break



        // Store 1
        Console.WriteLine("Enter the name of the first store for comparison: ");//storeName 1
        String storeOne = Console.ReadLine();//ToLower for eval

        //store 1, first item
        Console.WriteLine("Name of First product purchased at " + storeOne + ": ");//ask item
        String purchaseOne = Console.ReadLine();//collect item
        Console.WriteLine("Price paid for first purchased at " + storeOne + ": ");//ask 1st price
        Double price1A = Double.Parse(Console.ReadLine());//collect 1st price

        //store 1, second item, repeat process -- this ought to be a method or a function
        Console.WriteLine("Name of second product purchased at " + storeOne + ": ");//ask item
        String purchaseTwo = Console.ReadLine();//collect Item
        Console.WriteLine("Price paid for second purchased at " + storeOne + ": ");//Ask Item Price
        Double price1B = Double.Parse(Console.ReadLine());//Collect Item Price
        Console.WriteLine("\n");



        // Store 2, repeat process -- this ought to be a method or a function
        Console.WriteLine("Enter the name of the second store for comparison: ");//Store name 1
        String storeTwo = Console.ReadLine();// To Evals entry, we  ToLower to set to lower case

        //store 2
        Console.WriteLine("Price paid for " + purchaseOne + " at " + storeTwo + ": ");//ask 1st price
        Double price2A = Double.Parse(Console.ReadLine());//collect 1st price

        //store 2, second item
        Console.WriteLine("Price paid for " + purchaseTwo + " at " + storeTwo + ": ");//Ask Item Price
        Double price2B = Double.Parse(Console.ReadLine());//Collect Item Price
        Console.WriteLine("\n");



        // Results go here
        //Store one totals
        Console.WriteLine("************  " + storeOne + "  ************");
        Console.WriteLine(purchaseOne + ": $" + price1A);
        Console.WriteLine(purchaseTwo + ": $" + price1B);
        Console.WriteLine("\n \n");
        // store two totals
        Console.WriteLine("************  " + storeTwo + "  ************");
        // Result A: Where to shop
        Console.WriteLine(purchaseOne + ": $" + price2A);
        Console.WriteLine(purchaseTwo + ": $" + price2B);
        Console.WriteLine("\n \n");

        Console.WriteLine("************  After Price Comparison  ************");



        //merge subtotals
        Double subTotalOne = (price1A + price1B);
        Double subTotalTwo = (price2A + price2B);



        //evaluate subtotals to results
        if (subTotalOne == subTotalTwo)
        {
            Console.WriteLine("=");
        }
        else if (subTotalOne < subTotalTwo)
        {
            Console.WriteLine("<");
        }
        else (subTotalOne > subTotalTwo);
        { 
            Console.WriteLine(">"); 
        }            



        //keeps the console open
        Console.Read();
    }
}

}

4

2 回答 2

3

else条线是问题所在。

    else (subTotalOne > subTotalTwo);
    { 
        Console.WriteLine(">"); 
    }  

固定的:

    else
    { 
        Console.WriteLine(">"); 
    }  

解释:

您的代码相当于:

else
   (subTotalOne > subTotalTwo);//else block

{ 
    Console.WriteLine(">"); 
}  

分号使条件表达式成为语句,因此错误。但是,下一个控制台语句也不会是 else 块的一部分,因为 else 块在分号之后结束。因此,即使条件表达式是一个有效的语句,“>”也总是会被打印出来,这是不希望的。

于 2013-10-06T18:25:09.440 回答
0

You are probably going for:

else
{
    Console.WriteLine(">");
}

since you have eliminated all other possibilities.

On a formatting front I have never been a fan of else if all one line. For beginners especially, it can obscure program flow. There are others who completely disagree with me though...

于 2013-10-06T18:32:07.230 回答