打印时为什么部分输出没有显示?

时间:2015-12-10 20:19:54

标签: java

我正在做的是当我输入一个吉他的名字时,它会显示一些规格,如身体的形状,音品的数量等。

所以我创建了一个主编码类

package Guitar;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println("Input name of the guitar");

        Scanner name_g = new Scanner(System.in);
        String name_gf = name_g.next();

        Gibson g = new Gibson();

        System.out.println("Body Shape of the Guitar is : ");
        g.body_shape(name_gf);

        System.out.println("Number of frets of the Guitar is : ");
        g.num_frets(name_gf);

        System.out.println("Neck type of the guitar is : ");
        g.neck_type(name_gf);

        System.out.println("Pickup configuration of the guitar is : ");
        g.pup_conf(name_gf);


    }

}

另一个专门针对Gibson吉他的课程

package Guitar;

public class Gibson extends SpecsVar implements SpecsInterface  {




    @Override
    public String body_shape(String input) {
        // TODO Auto-generated method stub

        System.out.println("Body shape of the guitar is : ");

        if (input.equals(lp)){ 
            return lp;}
        else if (input.equals(ex)){
            return ex;}
        else if (input.equals(sg)){
            return sg;}
        else return "invalid Input.";


    }

    @Override
    public String num_frets(String input) {
        // TODO Auto-generated method stub

        if (input.equals(lp)){ 
            return shrt_fret;}
        else if (input.equals(ex)){
            return shrt_fret;}
        else if (input.equals(sg)){
            return shrt_fret;}
        else return "";

    }

    @Override
    public String neck_type(String input) {
        // TODO Auto-generated method stub

        if (input.equals(lp)){ 
            return mh;}
        else if (input.equals(ex)){
            return rw;}
        else if (input.equals(sg)){
            return rw;}
        else return "";

    }

    @Override
    public String pup_conf(String input) {
        // TODO Auto-generated method stub

        if (input.equals(lp)){ 
            return hh;}
        else if (input.equals(ex)){
            return hh;}
        else if (input.equals(sg)){
            return hh;}
        else return "";

    }

}

然后我有规格类

package Guitar;

public class SpecsVar {

    //Body Shape
    String lp = "Les Paul";
    String ex = "Explorer";
    String sg = "SG";

    //Number of Frets
    String shrt_fret = "22";
    String lng_fret = "24";

    //Neck Type
    String rw = "Rosewood";
    String mp = "Maple";
    String mh = "Mahogany";

    //Pickup Configuration
    String hsh = "HSH";
    String sss = "SSS";
    String hss = "HSS";
    String hh = "HH";

}

然后是界面

package Guitar;

public interface SpecsInterface {

    String body_shape(String input);

    String num_frets(String input);

    String neck_type(String input);

    String pup_conf(String input);

}

它显示的是

Input name of the guitar
Gibson
Body Shape of the Guitar is : 
Body shape of the guitar is : 
Number of frets of the Guitar is : 
Neck type of the guitar is : 
Pickup configuration of the guitar is : 

当我跑它但它没有显示音品的数量,身体的形状等等。

4 个答案:

答案 0 :(得分:4)

System.out.println()你这样做:

System.out.println("Body Shape of the Guitar is : ");
g.body_shape(name_gf);

只需将g.body_shape(name_gf);移至您的System.out.println()来电,并对其余所有人执行相同操作。这称为String concatenation

System.out.println("Body Shape of the Guitar is : " + g.body_shape(name_gf));

这是因为System.out.println()实际上是在屏幕上打印的,但是你的第二部分g.body_shape(name_gf);只进行了搜索,但没有别的,所以这就是为什么你只需将它放入你的打印电话中

答案 1 :(得分:2)

将您的主要内容更改为:

public class Main {

  public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.out.println("Input name of the guitar");

    Scanner name_g = new Scanner(System.in);
    String name_gf = name_g.next();

    Gibson g = new Gibson();

    System.out.println("Body Shape of the Guitar is : " + g.body_shape(name_gf));


    System.out.println("Number of frets of the Guitar is : " + g.num_frets(name_gf));


    System.out.println("Neck type of the guitar is : " + g.neck_type(name_gf));


    System.out.println("Pickup configuration of the guitar is : " + g.pup_conf(name_gf));



  }

}

答案 2 :(得分:1)

System.out.println("Body Shape of the Guitar is : ");
g.body_shape(name_gf);

第一行是打印出部分消息,但第二行不是。第二行只是从吉他获得一个值,它不显示它。将该值连接到消息的末尾,如下所示:

System.out.println("Body Shape of the Guitar is : " + g.body_shape(name_gf));

答案 3 :(得分:0)

这些变量没有被打印出来,因为它们不在print语句中。您只需将g.body_shape(name_gf);之类的行移到print语句中就像System.out.println("Body Shape of the Guitar is : " + g.body_shape(name_gf));

一样