2013-04-17 43 views
-4
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");  

    } 
} 
+2

有什麼問題嗎? –

+1

你有關於'for'和'while'循環的想法嗎? [ - > while和do-while語句](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html)AND [ - > for語句](http:// docs .oracle.com/javase/tutorial/java/nutsandbolts/for.html) – Smit

+0

請格式正確。 – NINCOMPOOP

回答

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("---------"); 
    } 
} 
+0

循環將永不停止。你需要一些機制來停止循環。 – Smit

+0

如果你想在一個更大的程序中演奏,你應該把它放在線程中 –