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

Java 如何声明循环的}while(???)部分?,java,loops,conditional,do-while,Java,Loops,Conditional,Do While,我需要这段代码来循环,不管有人决定使用多少次迭代,我不知道我需要在{while**(??here???)**中设置什么条件 我也知道循环应该围绕我的输入语句和税务计算,我是否将do和while放在了正确的位置 编辑*我已经从代码中删除了两个几乎相同的案例,因此我可以在这里的规则中发布 import java.util.Scanner; public class Assignment333 { public static void main(String[] args) { Scan

我需要这段代码来循环,不管有人决定使用多少次迭代,我不知道我需要在{while**(??here???)**中设置什么条件

我也知道循环应该围绕我的输入语句和税务计算,我是否将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();
  }
}
import java.util.Scanner;
公共类分配333{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
做{
System.out.println(“输入您的名字:”);
String name=input.next();
System.out.println(“以年为单位输入您的年龄:”;
字节年龄=input.nextByte();
System.out.println(“输入您的性别(F/M):”;
char gender=input.next().charAt(0);
System.out.println(“输入您的婚姻状况(S/M/D/W):”;
char\u status=input.next().charAt(0);
System.out.println(“输入您2016年的应纳税所得额:”);
长期收入=input.nextLong();
字符串名称\前缀;
双倍税额;
如果(性别=‘M’){
姓名前缀=(年龄<18岁)-“船长”:“先生”;
}否则{
姓名前缀=(婚姻状况='M')?“女士”:“女士”;
}
开关(婚姻状况){
案例“M”:
如果(收入<8500){
税额=0;
System.out.println(name_prefix+“”+name+”,根据提供的收入,您在2016财年不欠税);
}否则{
如果(收入<24000){
税额=收入*0.01;
}否则{
税额=收入*0.025;
}
System.out.println(name_前缀+“”+name+”,根据提供的收入,您在2016财年应缴纳的税款为“$”+税额+”;
}
打破
案例“W”:
如果(收入<8500){
税额=0;
System.out.println(name_prefix+“”+name+”,根据提供的收入,您在2016财年不欠税);
}否则{
如果(收入<24000){
税额=收入*.015;
}否则{
税额=收入*0.034;
}
System.out.println(name_前缀+“”+name+”,根据提供的收入,您在2016财年应缴纳的税款为“$”+税额+”;
}
而()
}
打破
默认值:System.out.println(“对不起!我们的系统此时无法计算您的税款。”);
}
System.out.println(“谢谢!”);
//关闭所有对象
input.close();
}
}

您可以这样完成:

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

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

...

答案是为什么要使用do while循环?@Max您需要格式化代码,并将相关部分保留在这里。我认为您应该在循环中的某个位置使用
while(true)
break
,在某些条件下。您可以说“不管有人决定使用多少次迭代”-这是如何决定的?在不知道您需要什么条件的情况下,我们无法帮助您。另外,请格式化您的代码。无法按原样阅读。(顺便说一句,如果可以进行
输入,您可能需要
中断
,而不是
时的
条件。next()
要返回
null
循环将中断,通常最好在
的左侧放置一个常量。equals
“y”。equals(input.next())
,因为“y”不能为空。我只是提到它,因为Max似乎对编程来说可能是新手,我想可能会让他从最佳实践的角度思考:)还有一件事-我想你的意思是
对象
不是
对象
你明白了。更正。