如何声明循环的while(???)部分?

时间:2017-03-03 03:57:13

标签: java loops conditional do-while

我需要这个代码来循环,无论有多少次迭代决定使用,我不明白我需要把什么条件放入{while **(?? ?? ??)**;

另外我理解循环应该围绕我的输入语句和税收计算,我将do和while放在正确的位置吗?

编辑*我已从代码中删除了两个几乎完全相同的案例,因此我可以在此处发布规则。

import java.util.Scanner;

public class Assignment333 {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    do {
      System.out.println("Enter your first name:");
      String name = input.next();

     System.out.println("Enter your age in years:");
      byte age = input.nextByte();

      System.out.println("Enter your gender (F/M):");
      char gender = input.next().charAt(0);

      System.out.println("Enter your marital status (S/M/D/W):");
      char marital_status = input.next().charAt(0);

      System.out.println("Enter your taxable income for 2016:");
      long income = input.nextLong();

      String name_prefix;
      double tax_amount;
      if (gender == 'M') {
        name_prefix = (age < 18) ? "Master." : "Mr.";
      } else {
        name_prefix = (marital_status == 'M') ? "Mrs." : "Ms.";
      }
      switch (marital_status) {
        case 'M':
          if (income < 8500) {
            tax_amount = 0;
            System.out.println(name_prefix + " " + name + ", based on the income provided, you owe no tax for the fiscal year 2016");
          } else {
            if (income < 24000) {
              tax_amount = income * 0.01;
            } else {
              tax_amount = income * 0.025;
            }
            System.out.println(name_prefix + " " + name + ", based on the income provided, you owe a tax of $" + tax_amount + " for the fiscal year 2016");
          }
          break;
        case 'W':
          if (income < 8500) {
            tax_amount = 0;
            System.out.println(name_prefix + " " + name + ", based on the income provided, you owe no tax for the fiscal year 2016");
          } else {
            if (income < 24000) {
              tax_amount = income * .015;
            } else {
              tax_amount = income * 0.034;
            }
            System.out.println(name_prefix + " " + name + ", based on the income provided, you owe a tax of $" + tax_amount + " for the fiscal year 2016");
          }
          while ()
      }
      break;
      default: System.out.println("Sorry! Our system is unable to calculate your tax at this time.");
    }
    System.out.println("Thank you!");
    //closing all objects
    input.close();
  }
}

1 个答案:

答案 0 :(得分:0)

你可以这样完成:

...
    System.out.println("Would you like to try again? (y/n)");

    } while (Objects.equals("y", input.next()))

...