提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
演示开关中的字符串。
public class Main {/*NowJava.com - 时代Java 提供*/ public static void main(String[] args) { // create two AutoPolicy objects Car policy1 = new Car(1, "Toyota", "NJ"); Car policy2 = new Car(2, "Ford", "ME"); // display whether each policy is in a no-fault state policyInNoFaultState(policy1); policyInNoFaultState(policy2); } // method that displays whether an AutoPolicy // is in a state with no-fault auto insurance public static void policyInNoFaultState(Car policy) { System.out.println("The auto policy:"); System.out.printf( "Account #: %d; Car: %s;%nState %s %s a no-fault state%n%n",//来 自 nowjava.com - 时代Java policy.getAccountNumber(), policy.getMakeAndModel(), policy.getState(), (policy.isNoFaultState() ? "is" : "is not")); } } class Car { private int accountNumber; // policy account number private String makeAndModel; // car that the policy applies to private String state; // two-letter state abbreviation // constructor public Car(int accountNumber, String makeAndModel, String state) { this.accountNumber = accountNumber; this.makeAndModel = makeAndModel; this.state = state; } // sets the accountNumber public void setAccountNumber(int accountNumber) { this.accountNumber = accountNumber; } // returns the accountNumber public int getAccountNumber() { return accountNumber; } // sets the makeAndModel public void setMakeAndModel(String makeAndModel) { this.makeAndModel = makeAndModel; } // returns the makeAndModel public String getMakeAndModel() { return makeAndModel; } // sets the state public void setState(String state) { this.state = state; } // returns the state public String getState() { return state; } // predicate method returns whether the state has no-fault insurance public boolean isNoFaultState() { boolean noFaultState;