C#while循环不起作用

时间:2018-03-03 10:07:07

标签: c# while-loop

我正在努力建立一个刺激所得税计算器。我想要取得的进展是:

  1. 打印出“您的总收入是多少:”的消息,要求用户以正数字数字输入收入。

  2. 阅读他们的输入

  3. 制作循环:如果用户输入字符串,则打印“输入您的收入作为整数美元数字”,然后返回步骤1.如果用户输入负数,则打印“您的收入不能否定“,并回到第1步。

  4. 4.如果用户已成功输入正数数字,请转到步骤5.

    1. 打印出一条消息“你有几个孩子?”

    2. 阅读用户的输入。

    3. 制作循环:如果用户键入字符串,则打印“您必须输入有效数字”,然后返回步骤5.如果用户键入负数,请打印“您必须输入正数“,然后回到第5步。

    4. 如果用户成功输入了正数字,请执行步骤9.

    5. 将收入和子女的数字纳入税收计算公式。如果totalTax< = 0,则打印“您欠税”。如果总税收> 0,打印“你必须支付[税额]税”。 END

    6. 我试图使用下面的代码,但它根本不起作用。

      //Record user's income into double list "income"
      double i;
      
      //Record user's number of children into double list "kid"
      double k;
      
      
      //if...elif statement to calculate the toatal tax of the user
      bool incomeOK = false;
      bool kidOK = false;
      
      do
      {
          Console.Write("What is your total income : ");
          incomeOK = double.TryParse(Console.ReadLine(), out i);
      
          if (!incomeOK || i < 0)
          {
              if (!incomeOK)
              {
                  Console.WriteLine("Enter your income as a whole-dollar numeric figure.");
              }
              else if (i < 0)
              {
                  Console.WriteLine("Your income cannot be negative.");
              }
          }
          else
          {
              Console.Write("How many children do you have: ");
              kidOK = double.TryParse(Console.ReadLine(), out k);
      
              while (!kidOK || k < 0)
              {
                  Console.Write("How many children do you have: ");
                  kidOK = double.TryParse(Console.ReadLine(), out k);
      
                  if (kidOK && k >= 0)
                  {
                      //Calculate the total payable tax of the user
                      double totalTax = (i - 10000 - (k * 2000)) * 0.02;
      
                      if (totalTax <= 10000)
                      {
                          Console.WriteLine("You owe no tax.");
                          Console.WriteLine("\n\n Hit Enter to exit.");
                          Console.ReadLine();
                      }
                      else
                      {
                          Console.WriteLine("You owe a total of $ " + totalTax + " tax.");
                          Console.WriteLine("\n\n Hit Enter to exit.");
                          Console.ReadLine();
                      }
                  }
      
                  if (!kidOK || k < 0)
                  {
                      if (!kidOK)
                      {
                          Console.WriteLine("You must enter a valid number.");
                      }
                      else if (k < 0)
                      {
                          Console.WriteLine("You must enter a positive number.");
                      }
                  }
              }
          }
      }
      while (!incomeOK || !kidOK);
      

      但是OUTPUT:

        

      你的总收入是多少:sfd
        输入您的收入作为一个整数美元的数字。

           

      按任意键继续......

1 个答案:

答案 0 :(得分:1)

您需要将其拆分为多个部分。

首先:获得正数

# Training Data
train_X = ...# shape of 1024 x 50
train_Y = ...# shape of 1x50
n_samples = 50
learning_rate = 0.0001
training_epochs = 1000
display_step = 50
# tf Graph Input
X = tf.placeholder("float")
Y = tf.placeholder("float")
# Set model weights
W = tf.Variable(tf.truncated_normal([1024, 1], mean=0.0, stddev=1.0, dtype=tf.float32))
b = tf.Variable(tf.zeros(1, dtype = tf.float32))
# Construct a linear model
pred = tf.add(tf.multiply(X, W), b)
# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

init = tf.global_variables_initializer()
# Start training
with tf.Session() as sess:
    # Run the initializer
    sess.run(init)
    # Fit all training data
    for epoch in range(training_epochs):
        for (x, y) in zip(train_X, train_Y):
            sess.run(optimizer, feed_dict={X: x, Y: y})
        # Display logs per epoch step
        if (epoch + 1) % display_step == 0:
            c = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
            print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(c), \
                  "W=", sess.run(W), "b=", sess.run(b))
    print("Optimization Finished!")

第二名:获得孩子数

double i;
double k;

Console.Write("What is your total income : ");

while (!double.TryParse(Console.ReadLine(), out i) || i < 0)
{
    if (i < 0)
    {
        Console.WriteLine("Your income cannot be negative.");
    }
    else 
    {
        Console.WriteLine("Enter your income as a whole-dollar numeric figure.");
    }
}

第三:计算

Console.Write("How many children do you have: ");

while (!double.TryParse(Console.ReadLine(), out k) || k < 0)
{
    if (k < 0)
    {
        Console.WriteLine("You must enter a positive number.");
    }
    else 
    {
        Console.WriteLine("You must enter a valid number.");
    }
}

每当输入错误时,while循环会强制你留在里面。当你离开时,你可以确定输入是正确的。

PS:我会将double totalTax = (i - 10000 - (k * 2000)) * 0.02; if (totalTax <= 10000) { Console.WriteLine("You owe no tax."); Console.WriteLine("\n\n Hit Enter to exit."); Console.ReadLine(); } else { Console.WriteLine("You owe a total of $ " + totalTax + " tax."); Console.WriteLine("\n\n Hit Enter to exit."); Console.ReadLine(); } 用于孩子的数量,因为没有任何意义你会有 2.3 孩子。< / p>