【问题标题】:C# Console Error: Only assignment, call, increment, decrement, and new object expressions can be used as a statementC#控制台报错:只有赋值、调用、自增、自减、新对象表达式可以作为语句使用
【发布时间】:2013-10-06 18:19:44
【问题描述】:

我是编程新手,正在尝试学习 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 { 课堂节目 { 静态无效主要(字符串 [] 参数) {

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

}

【问题讨论】:

  • 您可能希望使用stringdouble 而不是StringDouble 来声明您的变量。
  • @Tico - 真的..??这将如何解决错误?
  • @Rohit Vats 我并没有说两者都暗示它会解决错误。这只是好的做法。冷静点……

标签: c# compiler-construction console


【解决方案1】:

else 行是问题所在。

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

固定:

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

解释:

您的代码相当于:

else
   (subTotalOne > subTotalTwo);//else block

{ 
    Console.WriteLine(">"); 
}  

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

【讨论】:

  • 从技术上讲,如果两个比较对象之一是double.NaN,那么只有else 而不是else if (subTotalOne &gt; subTotalTwo) 就不一样了。但这是一个小细节。
  • 感谢大家的建议和指导,我保证我已经阅读了所有这些内容,并且在我走一小段路来考虑已经说过的所有内容后,我会再次阅读。我的程序现在正在运行,我感谢大家的大力帮助。我知道这是一件简单的事情,我感谢大家向我展示了如何让它变得更简单。我还有很多东西要学,谢谢大家(我说谢谢了吗?)
【解决方案2】:

你可能会去:

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

因为你已经排除了所有其他可能性。

在格式化方面,我从来都不喜欢 else if all one line。特别是对于初学者来说,它可能会使程序流程变得模糊。不过也有人完全不同意我的观点……

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-13
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多