如何格式化字符串

时间:2015-07-30 20:53:28

标签: java string

我无法重新格式化某些字符串,如下所示。如何在不将它们打印到控制台的情况下重新格式化这些字符串? System.out.printf(...)只是将它们打印到控制台。如何将它们打印到我的面板?

public String toString() {

    // Print all stuff to the PaintShop
    String a = "five gallons container";
    String b = "one gallon container";
    String c = "one half gallon container";
    String d = "one quart container";
    String f = "one pint container";
    String g = "one half pint container";
    String s = "For this job, you need " + totalGallons + " gallons of paint. \n";
    s += "You will need to purchase:  \n";
    if (this.Five_gallons > 0 ){
        s += this.Five_gallons + " five gallons container";
        if (this.Five_gallons > 1) s+= "s";
        s += "\n";
    }
    if (this.One_gallon > 0){
        s += this.One_gallon + " one gallon container";
        if (this.One_gallon > 1) s+= "s";
        s += "\n";
    }
    if (this.Half_gallon > 0){
        s+= this.Half_gallon + " one half gallon container";
        if(this.Half_gallon > 1) s+= "s";
        s += "\n";
    }
    if (this.Quart > 0){
        s+= this.Quart + " one quart container";
        if(this.Quart > 1) s+= "s";
        s += "\n";
    }
    if (this.pint > 0){
        s+= this.pint + " one pint container";
        if(this.pint > 1) s+= "s";
        s+= "\n";
    }
    if(this.Half_pint > 0){
        s+= this.Half_pint + " one half pint container";
        if(this.pint > 1) s+= "s";
        s+= "\n";
    }   
    if(this.totalCost > 0){
        s+= "The total price is $" + this.totalCost;
        s+= "\n";
    }
    s += "Thank you for your business"; // CHANGE THIS!
    return s;
}

0 个答案:

没有答案