如何在java中为下面的程序添加goto语句

时间:2016-02-01 03:44:54

标签: java

如何在java中使用goto语句..

package test;

import java.util.Scanner;

public class PrimeComposite {

    public static void main(String[] args) {

        int n,r;

        Scanner input = new Scanner(System.in);

        System.out.print("Ener a number: ");
        n= input.nextInt();

        for (int i=2; i<=n; i++){
            r= n%i;
            if (r==0){
                System.out.print("The number is composite");
                goto a: //want to jump from this loop to the a: label.

            }

            System.out.print("The number is prime");

        }
        a: //want to jump here if the if condition gets true... 

    }
}

1 个答案:

答案 0 :(得分:0)

Java中没有goto语句。只保留一个字。