【发布时间】: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