2016-08-21 2 views
-2

私の質問は、以下のコードを数字だけを受け入れるようにするにはどうすればいいですか?C#で値を乗算する方法は?

Console.WriteLine("Please enter the number of tickets sold for movie x or 1 to exit now!"); 
int adult = Convert.ToInt32(Console.ReadLine()); 
int total1 = 30 * adult; 
if (adult >= 5 && adult <= 30) 
{ 
    Console.WriteLine("The total cost for the adults tickets is : {0}", total1); 
} 
else if (adult == 1) 
{ 
    Environment.Exit(0); 
} 
else 
{ 
    Console.WriteLine("Error for adults"); 
} 
Console.WriteLine("Please enter the number of tickets sold for z or y to exit now!"); 
int child = Convert.ToInt32(Console.ReadLine()); 
int total2 = 20 * child; 
if (child >= 5 && child <= 30) 
{ 
    Console.WriteLine("The total cost for the child tickets is : {0}", total2); 
} 
else if (child == 1) 
{ 
    Environment.Exit(0); 
} 
else 
{ 
    Console.WriteLine("Error for child"); 
} 
int finTotal = total1 + total2; 
Console.WriteLine("The cost of all the tickets together is : {0}", finTotal); 
Console.ReadLine(); 
+0

フォーマットを使用してください –

答えて

3

条件場合TOTAL1TOTAL2を計算します。私はif文の外でtotal1とtotal2を計算する理由を理解していません。

例:

if (child >= 5 && child<= 30) 
{ 
    int total2 = 20 * child; 
    Console.WriteLine("The total cost for the child tickets is : {0}", total2); 
} 

EDIT1:数字のみを受け入れるためには、以下のanswerを見て:if文

0

これは動作するはずですが、私はあなたの他を残していますが、それらを書くことができます戻ってください。

using System; 

public class Program 
{ 
    public static void Main() 
    { 
     try 
     { 
      int totalAmount = 0; 

      Console.WriteLine("Insert the number of adult tickets sold (1 for exit)");  
      int adultTickets = Convert.ToInt32(Console.ReadLine()); 
      if(adultTickets >= 5 && adultTickets <= 30) 
      { 
       totalAmount = adultTickets * 30; 
      } 

      Console.WriteLine("Insert the number of child tickets sold (1 for exit)"); 
      int childTickets = Convert.ToInt32(Console.ReadLine()); 
      if(childTickets >= 5 && childTickets <= 30) 
      { 
       totalAmount += childTickets * 20; 
      } 

      Console.WriteLine(totalAmount); 
     } 
     catch(FormatException) 
     { 
      Console.WriteLine("Value input was not an integer."); 
     } 
    } 
} 
関連する問題