打印输出参数

时间:2015-03-21 21:23:23

标签: java

package cwk13g0;
import java.util.Scanner;


public class Cwk13g0 {

  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    Taxpayer johnnyhopkins = new Taxpayer(scan.nextBoolean() , scan.nextBoolean() , scan.nextInt(),scan.nextInt());
    Taxpayer sloanketterin = new Taxpayer(scan.nextBoolean() , scan.nextBoolean() , scan.nextInt(),scan.nextInt());
    johnnyhopkins.reportTax();
    sloanketterin.reportTax();
  }
}

package cwk13g0;

public class Taxpayer {

  private int income;
  private int age;
  private boolean married;
  private boolean veteran;

  public Taxpayer() {
    this.income = 0;
    this.age = 0;
    this.married = true;
    this.veteran = false;
}

public Taxpayer(boolean m, boolean v, int i, int a) {
    this();
    System.out.println("hello");
    this.setMarried(m);

    this.setVeteran(v);
    this.setIncome(i);
    this.setAge(a);
}

// married accessor
public boolean getMarried() {
    return this.married;
}

// married mutator
public void setMarried(boolean val) {
    this.married = val;
}

// veteran accessor
public boolean getVeteran() {
    return this.married;
}

// veteran mutator
public void setVeteran(boolean val) {
    this.veteran = val;
}

// income accessor
public int getIncome() {
    return this.income;
}

// income mutator
public void setIncome(int val) {
    if (val > 0) {
        this.income = val;
    }
}

//  age accessor
public int getAge() {
    return this.age;
}

// age mutator
public void setAge(int val) {
    if (val > -1 && val < 115) {
        this.age = val;
    }
}

// toString
@Override
public String toString() {
    return "age: " + age + ", " + "income: " + income + ", "
            + "married: " + married + ", " + "veteran: " + veteran + " ";
}

public int taxExemption() {
    int val = income;
    if (!married) {
        val = val - 5000;
        if (veteran) {
            val = val - 3000;
        }
    } else {
        val = val - 8000;
    }
    if (age > 65 || age < 12) {
        val = val - 1000;
    }
    return val;
}

public double tax() {
    double val = taxExemption();
    if (val <= 10000) {
        val = 0;
    } else if (val <= 35000) {
        val = val * .15;
    } else if (val <= 85000) {
        val = val * .25;
    } else if (val <= 175000) {
        val = val * .28;
    } else if (val <= 375000) {
        val = val * .33;
    } else if (val > 375000) {
        val = val * .38;
    }
    return val;
  }

  public void reportTax() {
    System.out.println("For an income of " + income + ", " + tax() + " in taxes are owed, leaving " + (income - tax()));
  }

}

所以我想做的就是在parameters之间打印一些东西。

我无法弄清楚如何。

所以,当我在纳税人johnnyhopkins的参数中询问用户input时,我该怎么说&#34;输入婚姻状况。 TrueFalse?&#34;。

然后让他们输入,然后在第一次输入后再打印输出

1 个答案:

答案 0 :(得分:0)

简单,不要直接从扫描仪传递参数。首先将它们扫描到局部变量中,然后在每个变量之前打印一个你喜欢的语句,然后将所有局部变量传递给每个对象的构造函数。为了简化这一过程,您可以创建一个方法来执行此操作,以便您可以重复使用它。

public TaxPayer createUser(Scanner scan) {
    System.out.println("Say something");
    boolean x = scan.nextBoolean();
    ...
    return new TaxPayer(x,y,z);
}

public static void main(String...args) {
    Scanner scan = new Scanner(System.in);
    TaxPayer somebody = createUser(scan);
}
相关问题