2016-04-06 133 views
0

我已經在一個類中做了一個主要方法,並在另一個類中做了很多其他小型方法。當我使用他們的位置在我的主要方法中調用它們時,如果我呼叫它們,確保它們會打印出來,它們仍然不會打印出來。只有打印兩種方法才顯示任何輸出。我不知道如何去修復它,所以我還沒有嘗試過很多東西。你能看看我的代碼,並檢查他們爲什麼不工作?爲什麼我的方法不能打印出來?

更新:我設法得到主要方法中的所有行,除了28在我收到的幫助下工作。現在剩下的只有一個輸出。我已經更改了代碼,所以它的效果更好一些,如果它不輸出,它會關閉,但輸出仍然丟失.package rational;

我的主要方法

package rational; 

    /** 
    * 
    * @author Dominique 
    */ 

    public class Rational { 


     public static String number() { 
     rational1 number= new rational1(27, 3); 
     String r3= number.printRational(number); 
     return r3; 
    } 
     public static void main(String[] args) { 
      rational1 number= new rational1(27,3); 
      System.out.println(number()); 
      String r3=number(); 
      System.out.println(rational1.toDouble(27,3)); 
      rational1.add(number); 
      rational1.invert(r3, number); 
      rational1.negate(r3, number); 
      rational1.toDouble(27, 3); 
     } 


    } 

我的其它方法類

package rational; 

/** 
* 
* @author Dominique 
*/ 
public class rational1 { 
    public int top; 
    public int bottom; 

public rational1() { 
this.top = 0;  
this.bottom = 0; 

} 
public rational1(int top, int bottom){ 
     this.top=top; 
     this.bottom=bottom; 
    } 
public String printRational(rational1 r1){ 
    String r3=("Your fraction is "+String.format(r1.top+"/"+r1.bottom)); 
    return r3; 
} 
public static void invert(String r2, rational1 r1) { 
    int index = r2.indexOf('s'); 

    if (index != -1) { 
      System.out.print(r2.substring(0, index+1));//works 
      System.out.println(" "+r1.bottom + "/" + r1.top); 
      index++; 
    } 
    else { 
      System.exit(0); 
     } 
} 
public static void negate(String r2, rational1 r1){ 
    int index = r2.indexOf('-'); 
if (index != -1) { 
    String stringValueOf = String.valueOf(r1.top); 
    System.out.println(r2.substring(0, 17));//works 
    System.out.println(r1.bottom+"/"+stringValueOf.substring(1)); 
    index++; 
    } 
} 


public static double toDouble(int one, int two){ 
    int three= one/two; 
    return three; 
} 

public static double gcd(double a, double b) 
{ 
double r = a % b; 
if (r != 0) 
{ 
return gcd(b, r); 
} 
else 
{ 
return b; 
} 
} 
public static double reduce(double t, double b){ 
     double numberone=gcd(t, b); 
     double pick=numberone*(b/t); 
     return pick; 
    } 

public static double add(rational1 r1){ 
    double pickone=(r1.top); 
    double choice= pickone+pickone; 
    double choice2=reduce(choice, r1.bottom); 
    return choice2; 
} 
} 
+0

問題到底在哪裏?你在哪一行打印出來並沒有得到它? – Gendarme

+0

我希望從我的主要方法中提出的第一個代碼的第26-29行獲得輸出。 –

回答

0

打印方法不一定刷新緩衝區到屏幕上。嘗試用println方法替換print方法。

+0

這似乎不起作用。 :/ –

2

所以問題是在轉化方法:

public static void invert(String r2, rational1 r1){ 
     int index = 0; 

    while (index < 1) { 
    if (r2.charAt(index) == '/') { 
    System.out.print(r2.substring(0, 17)); 
    System.out.print(r1.bottom+"/"+r1.top); 
    index++; 
    }else{ 
     System.exit(0); 
    } 
`} 
} 

此方法直接檢查在r2.charAt(索引)的字符==「/」),但是這是從未如此。因爲index = 0處的字符是printRational方法中的'Y'。因爲情況並非如此,System.exit(0)被調用,它立即結束程序而不運行程序的其餘部分。

我相信這段代碼會起作用。

public static void invert(String r2, rational1 r1) { 
    int index = r2.indexOf('/'); 

    if (index != -1) { 

      index++; 
    } 
    else { 
          System.out.print(r2.substring(0, index));//works 
      System.out.print(r1.bottom + "/" + r1.top); 
     } 
} 
+0

這是有道理的。我也擺脫了否定方法中的System.exit(0)。但是,當我運行該程序時,它只是打印出兩行工作,其餘從未打印。還有其他建議嗎? –

+0

在我的原始回覆中增加了說明。 –

+0

這工作打印出反轉方法,但其餘的我的方法仍然不會打印。 –

相關問題