簡體   English   中英

Java中的連續循環代碼

[英]Continuous Loop Code in Java

這是我的第一門Java課程。 當我編寫此代碼時,它會在打印gcd時停止。 我希望代碼從頭重新開始並繼續。 由於我在課程中沒有學習任何復雜的代碼,因此我不被允許這樣做。 該程序是一個循環,該循環從較大的整數中減去較小的整數,然后繼續循環直到整數之一變為零,以便打印出非零整數。

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);

                }
            }
        }
    }
}

代碼的問題是您使用了不正確的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);


  }

}

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

解決此問題的另一種方法:

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) ;
}
}

該代碼將等到用戶輸入整數后才開始執行。

這是我想要的答案,謝謝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: ");



 }

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM