【问题标题】:How do I state the }while(???) portion of the loop?如何声明循环的 }while(???) 部分?
【发布时间】:2017-03-03 03:57:13
【问题描述】:

无论某人决定使用多少次迭代,我都需要此代码循环,我不明白我需要在 {while**(??here??)**; 中添加什么条件;

另外我知道循环应该绕过我的输入语句和税收计算,我是否将 do 和 while 放置在正确的位置?

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

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();
  }
}

【问题讨论】:

  • 答案是为什么要使用 do while 循环?
  • @Max 您需要格式化您的代码并在此处仅保留相关部分。
  • 我认为在某些情况下,您应该在循环中的某处使用 while(true)break
  • 您说“无论某人决定使用多少次迭代” - 这是如何决定的?在不知道您想要什么条件的情况下,我们无法帮助您。另外,请格式化您的代码。无法按原样阅读。 (顺便说一句,您可能想要 break 而不是 while 条件)

标签: java loops conditional do-while


【解决方案1】:

你可以这样完成它:

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

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

...

【讨论】:

  • 如果input.next() 有可能返回null,则循环将中断,通常最好在.equals 左侧放置一个常量,例如"y".equals(input.next()),因为“y”永远不能为空。
  • 我只是提到它,因为看起来 Max 可能是编程新手,觉得不妨让他从最佳实践的角度思考 :)
  • 还有一件事 - 我想你的意思是 Objects 不是 Object
  • 你明白了。已更正。
猜你喜欢
  • 1970-01-01
  • 2011-12-23
  • 1970-01-01
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
  • 2020-02-03
  • 2021-10-10
  • 2023-03-15
相关资源
最近更新 更多