日期:2014-05-18  浏览次数:20762 次

switch case 和 level = Math.Log10(amount);有问题
using System;
using System.Collections.Generic;
using System.Text;

namespace Test1_19
{
  class Program
  {
  static void Main(string[] args)
  {
  Int32 amount, cost, level;
  Console.WriteLine("请输入购买教科书的数量:\n");
  amount = Int32.Parse(Console.ReadLine());
  cost = amount * 25;
  level = Math.Log10(amount);
  switch(level)
  {
  case (level>1&&level<10):
  cost *=0.9;
  break;
   
  case (level>100&&level<1000):
  cost*=0.85;
  break;
  case (level>1000):
  cost*=0.8;
  break;
  default :cost=cost;
  break;
  }
  Console.WriteLine("费用:\n,{0}",cost);


  }
  }
}


------解决方案--------------------
C# code
            cost  = amount * 25; 
            level = (int)Math.Log10(amount); 
            switch(level) 
            { 
                case 0:          // 表示:   1 ≤ amount < 10
                    break; 
                case 1:          // 表示:  10 ≤ amount < 100
                    cost *= 0.9; 
                    break; 
                case 2:          // 表示: 100 ≤ amount < 1000
                    cost *= 0.85; 
                    break; 
                default:          // 表示: 其他情形,也就是 amount ≥ 1000 或 amount < 1
                    cost *= 0.8; 
                    break; 
            }