我如何在java中循环这个扫描仪?

时间:2013-04-17 15:57:18

标签: java loops while-loop

package javaapplication97;

import java.util.Scanner;    

public class JavaApplication97 {    

    public static void main(String[] args) {

        System.out.println("Enter a number: ");
        Scanner s1 = new Scanner(System.in);    

        if (s1.hasNextInt())
           System.out.println("This is an int");    

    }
}

1 个答案:

答案 0 :(得分:0)

我认为你的意思是

import java.util.*;

public class test{

    public static void main(String[] args) {

        while(true){
            System.out.println("Enter a number: ");
            Scanner s1 = new Scanner(System.in);    

            if (s1.hasNextInt()){
                System.out.println("This is an int");
            }
            else if(s1.nextLine().compareTo("stop")==0){
                break;
            }
            else{
                System.err.println("this is not an int");
            }
            System.out.println("---------");
    }
}