2016-04-06 135 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

这工作打印出反转方法,但其余的我的方法仍然不会打印。 –

相关问题