[英]Why is part of my output not being displayed when I print it?
我正在做的是輸入吉他名稱時會顯示一些規格,例如琴體形狀,琴格數量等。
所以我創建了一個主編碼類
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);
}
}
還有另一種針對吉布森吉他的課程
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 :
當我運行它時,它沒有顯示品格的數量,身體的形狀等。
在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()
調用,並對其余所有調用執行相同操作。 這稱為字符串串聯
System.out.println("Body Shape of the Guitar is : " + g.body_shape(name_gf));
發生這種情況是因為System.out.println()
是實際在屏幕上打印的內容,但第二部分是g.body_shape(name_gf);
只是在進行搜索,而沒有進行其他操作,因此這就是為什么您只需要將其放入打印呼叫中的原因。
更改您的主要看起來像這樣:
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));
}
}
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));
那么這些變量不會被打印出來,因為它們不在print語句中。 您只需要移動g.body_shape(name_gf);
包含在您的打印語句中,例如System.out.println("Body Shape of the Guitar is : " + g.body_shape(name_gf));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.