2016-11-23 103 views
0

這是我在java中的第一場。當我寫這段代碼時,它只是在打印gcd時停止。我希望代碼從頭開始並繼續。由於我沒有在課程中採用任何複雜的代碼,因爲我不允許。該方案是關於一個循環,減去較大的一個較小的整數並繼續循環,直到整數中的一個變成零,使得它打印出的非零整數。連續循環代碼Java中

import java.util.Scanner; 

public class JavaApplication8 { 

    public static void main(String[] args) { 

     Scanner in = new Scanner(System.in); 

     System.out.println("Enter the first integer: "); 

     while (in.hasNextInt() || in.hasNext()) { 

      while (in.hasNextInt() || in.hasNext()) { 
       int x = in.nextInt(); 
       System.out.println("x = " + x); 
       System.out.println("Enter the second integer: "); 
       int y = in.nextInt(); 
       System.out.println("y = " + y); 

       while (x != 0) { 
        while (x >= y) { 
         int a = Math.max(x, y); 
         int b = Math.min(x, y); 
         a = a - b; 
         x = a; 
         y = b; 
        } 
        while (x < y) { 
         int a = Math.min(x, y); 
         int b = Math.max(x, y); 
         b = b - a; 
         x = a; 
         y = b; 
        } 

        System.out.println("The gcd =" + y); 

       } 
      } 
     } 
    } 
} 
+0

'而(in.hasNextInt()|| in.hasNext()){...}'你爲什麼有兩個具有相同條件的'while'循環? –

+0

@TimBiegeleisen IDK,也許我雖然它升會重複整個代碼 – Smas

+0

您可能需要使用while裏面,如果和else語句(X!= 0)循環 – Ajay

回答

0

您的代碼的問題是您正在使用While循環不正確。確保有效利用的if語句以及類(如果你知道如何使用它們)。

Scanner in = new Scanner(System.in); 

    System.out.println("Enter the first integer: "); 

    int x = in.nextInt(); 
    System.out.println("x = " + x); 
    System.out.println("Enter the second integer: "); 
    int y = in.nextInt(); 
    System.out.println("y = " + y); 

    while (x != 0) { 
     if (x >= y) { 
      int a = Math.max(x, y); 
      int b = Math.min(x, y); 
      a = a - b; 
      x = a; 
      y = b; 
     }else if (x<y){ 

      int a = Math.min(x, y); 
      int b = Math.max(x, y); 
      b = b - a; 
      x = a; 
      y = b; 
     } 
    } 

    System.out.println("The gcd =" + y); 


    } 

} 

試試這個(應該可以正常工作)。

+0

即使它裏面沒有循環,但它幫助我,我得到了答案。謝謝:d @marshmellooooooos – Smas

0

另一種方式來解決這個問題:

Scanner in = new Scanner(System.in); 
while(true) { 
System.out.println("Enter the first integer: "); 
while(in.hasNextInt() ==false) { 
} 
int x = in.nextInt(); 
System.out.println("x = " + x); 
System.out.println("Enter the second integer: "); 
while(in.hasNextInt() ==false) { 
} 
int y = in.nextInt(); 
System.out.println("y = " + y); 

while (x != 0 && y != 0) { 
    if (x >= y) { 
     x = x - y ; 
    }else if (x<y){ 
     y = y - x ; 
    } 
} 
if(x != 0) { 
System.out.println("The gcd is " + x) ; 
} else { 
System.out.println("The gcd is " + y) ; 
} 
} 

的代碼會等到用戶開始執行之前輸入的整數。

+0

謝謝:) @Ajay – Smas

0

這是我想要的答案,謝謝4大家的幫助:d

Scanner in = new Scanner(System.in); 
    System.out.println("Enter the first integer: "); 
    while (in.hasNextInt()) { 
    int x = in.nextInt(); 
    System.out.println("x = " + x); 
    System.out.println("Enter the second integer: "); 
    int y = in.nextInt(); 
    System.out.println("y = " + y); 

while (x != 0) { 
    if (x >= y) { 
     int a = Math.max(x, y); 
     int b = Math.min(x, y); 
     a = a - b; 
     x = a; 
     y = b; 
    }else if (x<y){ 

     int a = Math.min(x, y); 
     int b = Math.max(x, y); 
     b = b - a; 
     x = a; 
     y = b; 
    } 
} 

System.out.println("The gcd =" + y); 
System.out.println("Enter the first integer: "); 



} 

    } 

}