【发布时间】:2021-05-07 06:28:12
【问题描述】:
我正在编写一个程序,根据速度限制计算超速罚单成本,但最重要的是,如果他们在学区,并且我在公共(罚单)作为我的布尔值时遇到麻烦
if (schoolZone.equals("Y")) {
schoolZone = true;
} else {
schoolZone = false;
}
}
不会工作。它似乎无法将其转换为字符串值,我正在寻求帮助,因为我对计算机科学还很陌生
public class Ticket {
/*
* Instance variable for ticket
*/
private String name, TDL, address, city, state, zip;
private int postedSpeed, travelling, ticketAmount, day, month, year;
private boolean schoolZone;
// default constructor
public Ticket() {
}
// parameterized constructor
public Ticket(String name, String tDL, String address, String city, String state, String zip, int postedSpeed,
int travelling, int ticketAmount, int day, int month, int year, String schoolZone) {
name = name;
TDL = tDL;
address = address;
city = city;
state = state;
zip = zip;
postedSpeed = postedSpeed;
travelling = travelling;
ticketAmount = ticketAmount;
day = day;
month = month;
year = year;
if (schoolZone.equals("Y")) {
schoolZone = true;
} else {
schoolZone = false;
}
}
// calculate ticket
public void calTicket() {
double fine = 0.0;
if (travelling > (postedSpeed + 30) && schoolZone == true) {
fine = 2 * (6 * (travelling - postedSpeed) + 160 + ticketAmount);
} else if (travelling > postedSpeed && schoolZone == true) {
fine = 2 * (6 * (travelling - postedSpeed) + ticketAmount);
} else if (travelling > (postedSpeed + 30)) {
fine = ticketAmount + 160 + 6 * (travelling - postedSpeed);
} else if (travelling > postedSpeed) {
fine = ticketAmount + 6 * (travelling - postedSpeed);
} else if (travelling < postedSpeed) {
fine = 0;
}
if (schoolZone == true) {
System.out.println("Dear Citizen Knight,\n" + "You have received this citation for driving " + travelling
+ " mph in an area with a posted speed limit of " + postedSpeed + ".\r\n" + "\r\n"
+ "This violation occurred in a school zone.\r\n" + "\r\n" + "Your fine is $" + fine
+ " and can be paid at the address below.\r\n" + "\r\n"
+ "Please remember to buckle up and drive safely.");
} else {
System.out.println("Dear Citizen Knight,\n" + "You have received this citation for driving " + travelling
+ " mph in an area with a posted speed limit of " + postedSpeed + ".\r\n" + "\r\n"
+ "This violation didn't occurred in a school zone.\r\n" + "\r\n" + "Your fine is $" + fine
+ " and can be paid at the address below.\r\n" + "\r\n"
+ "Please remember to buckle up and drive safely.");
}
}
// return last name
public String lastName() {
String[] names = name.split("\\s+");
return names[names.length - 1];
}
// toString method to get details
public String toString() {
return month + "/" + day + "/" + year + "\n" + name + "(TDL: " + TDL + ")\n" + address + "\n" + city + ", "
+ state + ", " + zip + "\n";
}
}
//Runner
import java.util.Scanner;
public class TicketRunner {
public static void main(String[] args) {
//Scanner object
Scanner keyboard = new Scanner(System.in);
//printing programmer details
System.out.println("The Speeding Ticket Program");
System.out.println();
System.out.println("By: John Smith");
System.out.println("\n===================================\n");
//input driver data
System.out.print("Name of Driver? --> ");
String name = keyboard.nextLine();
System.out.print("TDL? -->");
String tdl = keyboard.nextLine();
System.out.print("Address? -->");
String address = keyboard.nextLine();
System.out.print("City? -->");
String city = keyboard.nextLine();
System.out.print("State? -->");
String state = keyboard.nextLine();
System.out.print("Zip? -->");
String zip = keyboard.nextLine();
System.out.print("Did the violation occur in a school zone? {Y/N} -->");
String zone = keyboard.nextLine();
System.out.println("Date of Violation:");
System.out.print("Month (number)? --> ");
int month = keyboard.nextInt();
System.out.print("Day? -->");
int day = keyboard.nextInt();
System.out.print("Year? -->");
int year = keyboard.nextInt();
System.out.print("What is the posted speed limit? -->");
int postedSpeed = keyboard.nextInt();
System.out.print("How fast was the car travelling in mph? --> ");
int travelling = keyboard.nextInt();
//instantiate ticket
Ticket ticket = new Ticket(name, tdl, address, city, state, zip, postedSpeed, travelling, 75, day, month, year,
zone);
//print ticket details
System.out.println(ticket.toString());
ticket.calTicket();
keyboard.close();//close scanner
}
}
/********************output******************/
【问题讨论】:
标签: java string boolean output