如何在Java中使用int/double入环和出环 public class TaxesV2Wendt{ 公共静态void main(字符串[]args){ 扫描仪输入=新扫描仪(系统输入); 最终双重税=.1; 最终双重分类1=.15; 最终双重分类2=.25; 最终双重分类3=.28; 最终双重分类4=.33; 最终的双重分类5=.35; 最终双重分类6=.396; 最终国际收入=9075; 最终国际收入1=36900; 最终国际收入2=89350; 最终国际收入3=186350; 最终国际收入4=405100; 最终国际收入5=406750; 对于(int i=0;i
定义如何在Java中使用int/double入环和出环 public class TaxesV2Wendt{ 公共静态void main(字符串[]args){ 扫描仪输入=新扫描仪(系统输入); 最终双重税=.1; 最终双重分类1=.15; 最终双重分类2=.25; 最终双重分类3=.28; 最终双重分类4=.33; 最终的双重分类5=.35; 最终双重分类6=.396; 最终国际收入=9075; 最终国际收入1=36900; 最终国际收入2=89350; 最终国际收入3=186350; 最终国际收入4=405100; 最终国际收入5=406750; 对于(int i=0;i,java,loops,percentage,Java,Loops,Percentage,定义all循环外变量。以便您可以在打印报表中使用此变量 在您的情况下,修改后的代码将是: public class TaxesV2Wendt { public static void main(String[] args) { Scanner in = new Scanner(System.in); final double TAX = .1; final double TAX1 = .15; final double T
all
循环外变量。以便您可以在打印报表中使用此变量
在您的情况下,修改后的代码将是:
public class TaxesV2Wendt {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
final double TAX = .1;
final double TAX1 = .15;
final double TAX2 = .25;
final double TAX3 = .28;
final double TAX4 = .33;
final double TAX5 = .35;
final double TAX6 = .396;
final int INCOME = 9075;
final int INCOME1 = 36900;
final int INCOME2 = 89350;
final int INCOME3 = 186350;
final int INCOME4 = 405100;
final int INCOME5 = 406750;
for (int i = 0; i < 5; i++) {
System.out.println("");
System.out.println("Enter your yearly income");
double year_Income = in.nextDouble();
double all = 0;
if (year_Income <= 9074) {
System.out.println("Your tax is 10%");
double taxes = ((double) year_Income * TAX);
System.out.println("You Owe" + taxes);
all = +taxes;
} else if (year_Income >= INCOME && year_Income <= INCOME1) {
System.out.println("Your tax is 15%");
double taxes = ((double) year_Income * TAX1);
System.out.println("You Owe" + taxes);
all = +taxes;
} else if (year_Income >= INCOME1 && year_Income <= INCOME2) {
System.out.println("Your tax is 25%");
double taxes = ((double) year_Income * TAX2);
System.out.println("You Owe" + taxes);
all = +taxes;
} else if (year_Income >= INCOME2 && year_Income <= INCOME3) {
System.out.println("Your tax is 28%");
double taxes = ((double) year_Income * TAX3);
System.out.println("You Owe" + taxes);
all = +taxes;
} else if (year_Income >= INCOME3 && year_Income <= INCOME4) {
System.out.println("Your tax is 33%");
double taxes = ((double) year_Income * TAX4);
System.out.println("You Owe" + taxes);
all = +taxes;
} else if (year_Income >= INCOME4 && year_Income <= INCOME5) {
System.out.println("Your tax is 35%");
double taxes = ((double) year_Income * TAX5);
System.out.println("You Owe" + taxes);
all = +taxes;
} else {
System.out.println("Your tax is 39.6");
double taxes = ((double) year_Income * TAX6);
System.out.println("You Owe" + taxes);
all = +taxes;
}
}
System.out.println("Total Taxes");
System.out.println(all);
}
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
最终双重税=.1;
最终双重分类1=.15;
最终双重分类2=.25;
最终双重分类3=.28;
最终双重分类4=.33;
最终的双重分类5=.35;
最终双重分类6=.396;
最终国际收入=9075;
最终国际收入1=36900;
最终国际收入2=89350;
最终国际收入3=186350;
最终国际收入4=405100;
最终国际收入5=406750;
全部加倍=0;
对于(int i=0;i<5;i++){
System.out.println(“”);
System.out.println(“输入您的年收入”);
两年收入=下一年加倍();
如果(年收入=收入和年收入=收入和年收入=收入1和年收入=收入2和年收入=收入3和年收入=收入4和年收入定义all
循环外变量。以便您可以在打印报表中使用此变量
在您的情况下,修改后的代码将是:
public class TaxesV2Wendt {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
final double TAX = .1;
final double TAX1 = .15;
final double TAX2 = .25;
final double TAX3 = .28;
final double TAX4 = .33;
final double TAX5 = .35;
final double TAX6 = .396;
final int INCOME = 9075;
final int INCOME1 = 36900;
final int INCOME2 = 89350;
final int INCOME3 = 186350;
final int INCOME4 = 405100;
final int INCOME5 = 406750;
for (int i = 0; i < 5; i++) {
System.out.println("");
System.out.println("Enter your yearly income");
double year_Income = in.nextDouble();
double all = 0;
if (year_Income <= 9074) {
System.out.println("Your tax is 10%");
double taxes = ((double) year_Income * TAX);
System.out.println("You Owe" + taxes);
all = +taxes;
} else if (year_Income >= INCOME && year_Income <= INCOME1) {
System.out.println("Your tax is 15%");
double taxes = ((double) year_Income * TAX1);
System.out.println("You Owe" + taxes);
all = +taxes;
} else if (year_Income >= INCOME1 && year_Income <= INCOME2) {
System.out.println("Your tax is 25%");
double taxes = ((double) year_Income * TAX2);
System.out.println("You Owe" + taxes);
all = +taxes;
} else if (year_Income >= INCOME2 && year_Income <= INCOME3) {
System.out.println("Your tax is 28%");
double taxes = ((double) year_Income * TAX3);
System.out.println("You Owe" + taxes);
all = +taxes;
} else if (year_Income >= INCOME3 && year_Income <= INCOME4) {
System.out.println("Your tax is 33%");
double taxes = ((double) year_Income * TAX4);
System.out.println("You Owe" + taxes);
all = +taxes;
} else if (year_Income >= INCOME4 && year_Income <= INCOME5) {
System.out.println("Your tax is 35%");
double taxes = ((double) year_Income * TAX5);
System.out.println("You Owe" + taxes);
all = +taxes;
} else {
System.out.println("Your tax is 39.6");
double taxes = ((double) year_Income * TAX6);
System.out.println("You Owe" + taxes);
all = +taxes;
}
}
System.out.println("Total Taxes");
System.out.println(all);
}
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
最终双重税=.1;
最终双重分类1=.15;
最终双重分类2=.25;
最终双重分类3=.28;
最终双重分类4=.33;
最终的双重分类5=.35;
最终双重分类6=.396;
最终国际收入=9075;
最终国际收入1=36900;
最终国际收入2=89350;
最终国际收入3=186350;
最终国际收入4=405100;
最终国际收入5=406750;
全部加倍=0;
对于(int i=0;i<5;i++){
System.out.println(“”);
System.out.println(“输入您的年收入”);
两年收入=下一年加倍();
if(year_Income=Income&&year_Income=INCOME1&&year_Income=INCOME2&&year_Income=INCOME3&&year_Income=INCOME4&&year_Income您all
变量是一个块变量。在if/else块之外看不到它。
如果要在块外部(但在for循环内部)访问它,必须将all
变量的声明移动到for循环的范围内
e、 g
您的all
变量是一个块变量。在if/else块之外看不到它。
如果要在块外部(但在for循环内部)访问它,必须将all
变量的声明移动到for循环的范围内
e、 g
您正在大括号内定义all。大括号一结束,all的范围就结束了。因此编译器将无法找到all。
您必须在for循环外部定义all。如果在循环内部定义它,那么每次循环开始时它都会被覆盖。您正在定义all内部大括号。大括号一结束,all的范围就结束了。因此,编译器将无法找到all。
您必须定义allfor循环外部。如果在循环内部定义,则每次循环开始时它都会被覆盖。定义for循环外部的所有内容。此外,我看不到for循环的结尾。在System.out.println(all);statement.usebacktick
(键1左侧选项卡上方波浪线下方的键)要将格式设置为代码,请不要这样粗体@Lưu Vĩnh Phúc谢谢您的建议。下次我会注意的。定义所有外部for循环。此外,我看不到for循环的结尾。在System.out.println(all);statement.usebacktick
(键1左侧选项卡上方波浪线下方的键)要将格式作为代码,请不要这样粗体@Lưu Vĩnh Phúc谢谢您的建议。下次我会注意。}System.out.println(“总税”);System.out.println(全部);好了,你花了很长时间bc 4空格的内容你试图在for循环外打印变量的值。但是你已经在for循环内声明了它。你必须在for循环之前声明它。就像我做了1000次一样,5次它只做100次而不是500次所有+=…应该在每次for循环执行时执行}System.out.println(“总税”);System.out.println(全部);好了,你花了很长时间bc 4空格的内容你试图在for循环外打印变量的值。但是你已经在for循环内声明了它。你必须在for循环之前声明它。就像我做了1000次一样,5次它只做100次而不是500次所有+=…应该在每次for循环执行时执行我也做了在.close()中添加;
以避免任何内存泄漏。唯一的问题是,现在它不会为5个输入加起来。100被输出5次,应该加到500,但不确定它会去哪里现在它会去做,将代码更新为all+=taxes;
以前您使用的all=+taxes
不会向变量添加数据,您必须
for(int i = 0; i < 5; i++){
double all = 0.0;
..
double all = 0.0;
for(int i = 0; i < 5; i++){
..