我有这个显示某些错误的Java代码。我不明白这些错误。 问题是“我必须一个人一个人的名字和收入,并将它们分别存储在各自的数组中,然后我必须在method()中计算他们的所得税,并用他们的名字打印他们的所得税。”
import java.util.Scanner;
public class tax {
int calculateTax(int Income) {
int num, i;
String[] arrayOfNames = new String[num];
Long[] arrayOfIncome = new Long[num];
int Incometax;
if (Income >= 300000) {
Incometax = (Income * 20) / 100;
System.out.println(arrayOfNames[i] + ": " + Incometax);
} else if (Income >= 100000 && Income <= 300000) {
Incometax = (Income * 10) / 100;
System.out.println(arrayOfNames[i] + ": " + Incometax);
} else if (Income <= 100000) {
System.out.println(arrayOfNames[i] + ": No tax" );
}
return Incometax;
}
public static void main(String[] args) {
int num, i;
String Name;
int Income;
System.out.println("Enter person count: ");
Scanner scanner = new Scanner(System.in);
num = scanner.nextInt();
String[] arrayOfNames = new String[num];
Long[] arrayOfIncome = new Long[num];
for (i = 0; i < num; ++i) {
System.out.println("Enter name " + (i + 1) + ":");
arrayOfNames[i] = scanner.next();
System.out.println("Enter " + arrayOfNames[i] + "'s income: ");
arrayOfIncome[i] = scanner.nextLong();
System.out.println();
}
for (i = 0; i <= num; i++) {
int Incometax=calculateTax(int Income);
}
scanner.close();
}
}
答案 0 :(得分:1)
Op有很多错误:
所以我建议您学习一些基础知识并开始阅读有关Java的内容。
答案 1 :(得分:0)
有些事情“错了”,有些事情可以做得更好。内联注释需要修复的内容(这不是固定的代码,只是指出您需要改进的地方)
import java.util.Scanner;
public class tax {
/// typically, variables are camel cased (e.g. income instead of Income)
int calculateTax(int Income) {
int num, i;
String[] arrayOfNames = new String[num];
Long[] arrayOfIncome = new Long[num];
/// As mentioned, variables should be camel case
int Incometax;
if (Income >= 300000) {
Incometax = (Income * 20) / 100;
/// At this point, arrayOfNames is empty, so this will error
System.out.println(arrayOfNames[i] + ": " + Incometax);
/// the <= 300000 is not needed here, as it will have triggered the first if
} else if (Income >= 100000 && Income <= 300000) {
Incometax = (Income * 10) / 100;
/// At this point, arrayOfNames is empty, so this will error
System.out.println(arrayOfNames[i] + ": " + Incometax);
/// This can likely just be an else statement
} else if (Income <= 100000) {
/// At this point, arrayOfNames is empty, so this will error
System.out.println(arrayOfNames[i] + ": No tax" );
}
return Incometax;
}
public static void main(String[] args) {
int num, i;
String Name;
int Income;
System.out.println("Enter person count: ");
Scanner scanner = new Scanner(System.in);
num = scanner.nextInt();
String[] arrayOfNames = new String[num];
Long[] arrayOfIncome = new Long[num];
/// ++i increments before evaluation, so this will not work the way you
/// think it will. You will likely be one iteration "short"
for (i = 0; i < num; ++i) {
/// i will be incremented before execution, so no need for i+1
System.out.println("Enter name " + (i + 1) + ":");
arrayOfNames[i] = scanner.next();
System.out.println("Enter " + arrayOfNames[i] + "'s income: ");
arrayOfIncome[i] = scanner.nextLong();
System.out.println();
}
/// You never use i within this for loop
for (i = 0; i <= num; i++) {
/// this will error, as you shouldn't have a type declaration in a method call
/// Income has already been declared
/// At this point, Income is 0, as it has not been assigned to.
int Incometax=calculateTax(int Income);
}
scanner.close();
}
}
答案 2 :(得分:0)
int Incometax=calculateTax(int Income);
替换为
int Incometax=calculateTax(Income);
此外,calculateTax不是静态方法。因此,您不能从静态main调用它。
答案 3 :(得分:-1)
替换:
int Income;
具有:
int income;
只能将对象命名为大写。
和
calculateTax(int Income);
不正确,它不应包含类型。
calculateTax(Income); // is ok