Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 布尔值不能转换为字符串_Java_String_Boolean_Output - Fatal编程技术网

Java 布尔值不能转换为字符串

Java 布尔值不能转换为字符串,java,string,boolean,output,Java,String,Boolean,Output,我正在写一个程序,根据限速计算超速罚单的费用,但最重要的是,如果他们在学校区域,我在公共场合遇到麻烦(罚单),作为我的朋友 if(schoolZone.equals(“Y”)){ 学校地带=正确; }否则{ 学校区域=假; } } 行不通。它似乎不能将其转换为字符串值,我正在寻求帮助,因为我是计算机科学的新手 公共舱票{ /* *票证的实例变量 */ 私有字符串名称、TDL、地址、城市、州、邮政编码; 私人国际邮政速度、旅行、票数、日、月、年; 私立学校区; //默认构造函数 公众票{ } /

我正在写一个程序,根据限速计算超速罚单的费用,但最重要的是,如果他们在学校区域,我在公共场合遇到麻烦(罚单),作为我的朋友

if(schoolZone.equals(“Y”)){
学校地带=正确;
}否则{
学校区域=假;
}
}
行不通。它似乎不能将其转换为字符串值,我正在寻求帮助,因为我是计算机科学的新手

公共舱票{
/*
*票证的实例变量
*/
私有字符串名称、TDL、地址、城市、州、邮政编码;
私人国际邮政速度、旅行、票数、日、月、年;
私立学校区;
//默认构造函数
公众票{
}
//参数化构造函数
公共票证(字符串名称、字符串tDL、字符串地址、字符串城市、字符串州、字符串zip、int postedSpeed、,
整数旅行、整数票数、整数日、整数月、整数年、字符串学区){
名称=名称;
TDL=TDL;
地址=地址;
城市=城市;
状态=状态;
zip=zip;
postedSpeed=postedSpeed;
旅行=旅行;
ticketAmount=ticketAmount;
天=天;
月=月;
年=年;
如果(schoolZone.equals(“Y”){
学校地带=正确;
}否则{
学校区域=假;
}
}
//计票
公众票(){
双重罚款=0.0;
如果(旅行>(postedSpeed+30)和&schoolZone==真){
罚款=2*(6*(行驶-后速度)+160+票数);
}否则,如果(旅行>postedSpeed&&schoolZone==真){
罚款=2*(6*(行驶-后速度)+票数);
}否则,如果(行驶>(后速度+30)){
罚款=票数+160+6*(行驶-后速度);
}否则如果(行驶>后速度){
罚款=票数+6*(行驶-后速度);
}否则如果(行驶<后速度){
精细=0;
}
if(schoolZone==true){
System.out.println(“亲爱的公民骑士”+“您因驾驶”+旅行而收到此传票”
+“在张贴速度限制为“+postedSpeed+”\r\n“+”\r\n”的区域内以英里/小时的速度行驶”
+此违规行为发生在学校区域。\r\n“+”\r\n“+”您的罚款为$“+罚款
+并且可以在下面的地址付款。\r\n“+”\r\n
+“请记住系好安全带并安全驾驶。”);
}否则{
System.out.println(“亲爱的公民骑士”+“您因驾驶”+旅行而收到此传票”
+“在张贴速度限制为“+postedSpeed+”\r\n“+”\r\n”的区域内以英里/小时的速度行驶”
+“此违规行为没有发生在学校区域。\r\n”+“\r\n”+“您的罚款是$”+罚款。”
+并且可以在下面的地址付款。\r\n“+”\r\n
+“请记住系好安全带并安全驾驶。”);
}
}
//返回姓氏
公共字符串lastName(){
字符串[]名称=名称。拆分(\\s+);
返回名称[names.length-1];
}
//toString方法以获取详细信息
公共字符串toString(){
返回月+“/”+日+“/”+年+“\n”+名称+”(TDL:“+TDL+”)\n“+地址+“\n”+城市+”,“
+状态+”,“+zip+”\n”;
}
}
//跑步者
导入java.util.Scanner;
公共类售票员{
公共静态void main(字符串[]args){
//扫描对象
扫描仪键盘=新扫描仪(System.in);
//打印程序员详细信息
System.out.println(“超速罚单程序”);
System.out.println();
System.out.println(“作者:约翰·史密斯”);
System.out.println(“\n=================================================\n”);
//输入驱动程序数据
System.out.print(“驱动程序名称?-->”);
字符串名称=keyboard.nextLine();
系统输出打印(“TDL-->”);
字符串tdl=keyboard.nextLine();
系统输出打印(“地址?-->”);
字符串地址=keyboard.nextLine();
系统输出打印(“城市?-->”);
字符串city=keyboard.nextLine();
系统输出打印(“状态?-->”);
字符串状态=keyboard.nextLine();
System.out.print(“Zip?-->”);
String zip=keyboard.nextLine();
System.out.print(“违规行为是否发生在学校区域?{Y/N}-->”);
字符串区域=keyboard.nextLine();
System.out.println(“违反日期:”);
系统输出打印(“月(号)?-->”);
int month=keyboard.nextInt();
系统输出打印(“日?-->”);
int day=keyboard.nextInt();
系统输出打印(“年?-->”);
int year=keyboard.nextInt();
System.out.print(“张贴的限速是多少?-->”);
int postedSpeed=keyboard.nextInt();
System.out.print(“汽车以每小时多少英里的速度行驶?-->”);
int=keyboard.nextInt();
//实例化票据
票证=新票证(姓名、tdl、地址、城市、州、邮编、邮速、旅行、75、日、月、年、,
区域);
//打印车票详细信息
System.out.println(ticket.toStr
if (schoolZone.equals("Y")) {
    this.schoolZone = true;
} else {
    this.schoolZone = false;
}
if (schoolZone.equalsIgnoreCase("Y")) {
    this.schoolZone = true;          
} else {       
    this.schoolZone = false;
}
// 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) {
       this.name = name;
       this.TDL = tDL;
       this.address = address;
       this.city = city;
       this.state = state;
       this.zip = zip;
       this.postedSpeed = postedSpeed;
       this.travelling = travelling;
       this.ticketAmount = ticketAmount;
       this.day = day;
       this.month = month;
       this.year = year;

       if (schoolZone.equalsIgnoreCase("Y")) {
          this.schoolZone = true;
       } else {
           this.schoolZone = false;
       }
   }
package stackoverflow;

import java.text.DecimalFormat;
import java.util.Objects;

public class Ticket {

    static public String toLastName(final String pFullName) {
        if (pFullName == null) return null; // fail fast, fail early: we could also throw an exception here

        final String[] names = pFullName.split("\\s+");
        return names[names.length - 1];
    }




    /*
    * Instance variable for ticket
    */
    // if member variables are truly immutable, and you can have them final, then you can make em public final, so you dont need getters
    // BUT: if you're planning on using advanced Java features, with bytecode manipulation, reflection, like Java EE or aspect-oriented libraries,
    // you better stick to the getter/setters at all times
    public final String     mTDL;
    public final String     mAddress;
    public final String     mCity;
    public final String     mState;
    public final String     mZipCode;
    public final int        mPostedSpeedMph;
    public final int        mTravellingSpeedMph;
    public final int        mTicketAmount;
    public final int        mDay;
    public final int        mMonth;
    public final int        mYear;
    public final boolean    mSchoolZone;

    private String mName; // name can be changed du to marriage

    // default constructor
    // public Ticket() {} // this will not create a valid state, so we leave it out unless it's essential
    // if it's truly essential to your code, use a flag and a getter to see if you're interacting with valid objects

    // parameterized constructor
    public Ticket(final String pName, final String pTDL, final String pAddress, final String pCity, final String pState, final String pZipCode,
            final int pPostedSpeedMph, final int pTravellingSpeedMph, final int pTicketAmount,
            final int pDay, final int pMonth, final int pYear, final String pSchoolZone) {
        mName = pName;
        mTDL = pTDL;
        mAddress = pAddress;
        mCity = pCity;
        mState = pState;
        mZipCode = pZipCode;
        mPostedSpeedMph = pPostedSpeedMph; // it's better to explicitly name the unit. keeps you and others out of trouble
        mTravellingSpeedMph = pTravellingSpeedMph; // especially interesting in time units: is this parameter in seconds or milliseconds?
        mTicketAmount = pTicketAmount;
        mDay = pDay;
        mMonth = pMonth;
        mYear = pYear;
        mSchoolZone = "Y".equals(pSchoolZone); // use one-line assignments and conditionals when possible
        // also when trying to match two strings, beware of the NullPointerException. Having constants as first operand dodges that problem
    }



    public String getName() { // simple getter, for the only non-final member variable
        return mName;
    }



    // calculate ticket
    public void calcTicket() {
        final double fine = calcFine();
        final DecimalFormat df = new DecimalFormat("#.##"); // kicking a raw float to the string might get ugly results at times.
        // use dedicated formatting to prevent that

        final String schoolZoneText = mSchoolZone ? "This violation occurred in a school zone." : "This violation did not occurr in a school zone.";
        final String output = "Dear Citizen Knight " + getName() + ",\n\n"
                + "You have received this citation for driving " + mTravellingSpeedMph
                + " mph in an area with a posted speed limit of " + mPostedSpeedMph + ".\r\n"
                + "\r\n"
                + schoolZoneText + "\r\n"
                + "\r\n" + "Your fine is $" + df.format(fine) + " and can be paid at the address below.\r\n"
                + "\r\n"
                + "Please remember to buckle up and drive safely.";
        /* HINT: do NOT mix \n and \r\n and \n\r and \r newlines! Stick to one. Java tries to works with \n alone.
         * Unless it's too much of a hassle, try to convert to \n, then work with \n internally and only re-convert at output,
         * if you need specific newline control.
         * Also, its is easier if you have the text somewhat outlined like the output is supposed to be displayed
         */

        System.out.println(output);

        // I left a remnant of your old code to tell you sth else:
        //if (mSchoolZone) { // if(true==true) is a bit overkill, if(true) usually suffices
    }

    public int calcFine() { // have this as its own method, to it is more focused and can be used from other places too, f conventient
        // early return approach: jump back as soon as solution is done. this way it is easier for the compiler to see problems and warn you.
        // here are also lots of magic numbers, that in other cases should be replaced by constants
        if (mTravellingSpeedMph > (mPostedSpeedMph + 30) && mSchoolZone == true) return 2 * (6 * (mTravellingSpeedMph - mPostedSpeedMph) + 160 + mTicketAmount);
        else if (mTravellingSpeedMph > mPostedSpeedMph && mSchoolZone == true) return 2 * (6 * (mTravellingSpeedMph - mPostedSpeedMph) + mTicketAmount);
        else if (mTravellingSpeedMph > (mPostedSpeedMph + 30)) return mTicketAmount + 160 + 6 * (mTravellingSpeedMph - mPostedSpeedMph);
        else if (mTravellingSpeedMph > mPostedSpeedMph) return mTicketAmount + 6 * (mTravellingSpeedMph - mPostedSpeedMph);
        // else if (mTravelling < mPostedSpeed) return 0; we actually do not even need this line
        return 0; // my compiler told me we need this, so then I realized the line above was redundant
    }

    // return last name
    public String getLastName() { // we call them GETTERS and SETTERS
        return toLastName(mName);
    }

    // toString method to get details
    @Override public String toString() { // do not forget annotations like @Override etc, they may hint at possible problems
        return mMonth + "/" + mDay + "/" + mYear + "\n" + mName + "(TDL: " + mTDL + ")\n" + mAddress + "\n" + mCity + ", "
                + mState + ", " + mZipCode + "\n";
    }

    /*
     * Here a fine little method that shows where my prefixing really helps.
     * Of course, I bent the naming to showcase this.
     */
    public String takeLastName(final String pName) { // in case of marriage
        final String name = toLastName(pName);
        final String[] myNameParts = mName.split("\\s+");
        myNameParts[myNameParts.length - 1] = name;
        mName = String.join(" ", myNameParts);
        return mName;
    }
    public boolean hasSameLastNameAs(final String pOtherName) {
        final String otherLastName = toLastName(pOtherName);
        final String myLastName = toLastName(mName);
        return Objects.equals(otherLastName, myLastName);
    }



    public static void main(final String[] args) {
        final Ticket t = new Ticket("Peter Lustig", null, null, null, null, null, 30, 60, 2, 14, 1, 2021, "Y");
        t.calcTicket();
        t.takeLastName("Hans Dampf");
        System.out.println("\n\n\nAfter his marriage, he is now called " + t.getName());

    }

}