在下面的代碼中, string.Format  總是不能輸出結果,  注:選用的是控制臺應用程序.
using System;
using System.Collections.Generic;
using System.Text;namespace C31._
{
    class Program
    {
        static void Main(string[] args)
        {
            float num,price;
            Console.WriteLine("請錄入商品的原價:");
            num = float.Parse(Console.ReadLine());
            price = pdd(num );
            //Console.WriteLine("原價為:{0}\t實價為{1}", num, price);    //輸出正常
            string tt = string.Format ("原價為:{0}\t實價為{1}", num, price);   //為什么Format格式不起作用?
        }        //價格大於100 優惠50
        private static float pdd(float num)
        {
            float price;
            if (num < 100)
            {
                price = num;
            }
            else
            {
                price = num - 50;
            }
            return price;
        }
    }
}

解决方案 »

  1.   

    老大!你更本就没有输出啊!    //Console.WriteLine("原價為:{0}\t實價為{1}", num, price);    //輸出正常 
                string tt = string.Format("原價為:{0}\t實價為{1}", num, price);  //為什么Format格式不起作用? 
                Console.WriteLine(tt);
      

  2.   

    你只获取了string.Format的结果..没有输出结果而已加上  Console.WriteLine(tt);把
      

  3.   

    資料上是這樣寫的呀:
     string price="小強";
     console.writeLine("他的名字是{0}",price);
     等同於  
     string tt = string.Format("他的名字是{0}",price); Format 格式的語法是:
     string mystring = string.Format("格式字符串",參數列表); 與我下面的語句, 好像沒有不同的地方, 但是就是找不到原因,程序也不報錯. price = pdd(num ); 
     Console.WriteLine("原價為:{0}\t實價為{1}", num, price);    //輸出正常 
     string tt = string.Format ("原價為:{0}\t實價為{1}", num, price);  //為什么Format格式就是不顯示, 請各位再指教!!!
      

  4.   

    明白了,  string.Format 的功能衹是賦值給tt,  如果要輸出的話,還是必須用到Console.WriteLine()  
    謝謝各位!!
      

  5.   

    晕啊~
    Format只有格式转换的作用,不负责输出