2016-10-19 3 views
0
할당 어떤 사용자의 입력에서 소수를 얻으려고 노력 관한

에서 "0"으로 루프를 종료하려고 내가 입력하는 사용자가 루프를 종료하기 위해 노력하고있어 0사용자

/* 
Natasha Shorrock 
Assignment A4 
10/20/16 
*/ 
package a4; 

import java.util.Scanner; 

public class MainApp { 

     static boolean isPrimeOptimized(int candidate) { 
     if (candidate < 2) { 
      return false; 
     }//if 
     if (candidate % 2 == 0) { 
      //divisble by 2 with no remainder 
      //is not prime 
      return false; 
     }//if 
     int divisor = 3; 
     while (divisor < candidate/2) { 
      if (candidate % divisor == 0) { 
       return false;// this is not prime 
      }//if 
      divisor = divisor + 2; 
     }//while 
     return true; 
    }//isPrime 

    public static void main(String[] args) { 
     while (true) { 
      Scanner sc = new Scanner(System.in); 
      System.out.println("Enter 2 numbers or zero to exit: "); 

      //ask the user for low and high 
      //range of [prime numbers to generate 
      int low = sc.nextInt(); 
      int high = sc.nextInt(); 

      int candidate = low; 
      int count = 0; 
      while (candidate <= high) { 
       if (isPrimeOptimized(candidate)) { 
        //if prime print 
        count = count + 1; 
        System.out.println(); 
        System.out.println(count + " " + candidate); 

       }//if 
       candidate = candidate + 1; 
      }//while 

     }//while (true) 

    }//class 

}//classMainApp 
+0

그녀는 하나 개의 번호를 요청, 그래서 그녀는 '0'입력 한 경우 만'low' 채워 것하고 high''을 기다릴 것입니다. 따라서 두 스캔 사이에 if가 있어야하며 '낮음'만 확인해야합니다. –

답변

0

만, 0를 종료 한 번호를 요청할 때문에 만 low 채워 될 수 있으며 high 기다릴 것입니다. 그래서 if는 두 스캔 사이에 있어야하며, 사용자가 0 당신을 입력 할 경우에도 아니라, 모두의 0

int low = sc.nextInt(); 
if(low == 0){ 
    break; 
} 
+0

어느 낮은 수준을 언급하고 있습니까? –

+0

Nevermind 하하는 방금 실현했습니다 –

+0

@Michael 그것은 종료하거나 오른쪽 중단해야합니까? –

0

먼저인지 확인합니다 low하에 if 문을 추가 low

확인해야합니다 두 숫자를 입력하도록 강요 :

,369 :

int low = sc.nextInt(); 
int high = sc.nextInt(); 

을 나는 당신이 코드에서 다음을 수행 제안

int low = sc.nextInt(); 
if(low==0){ 
    return 0; 
} else{ 

    int high = sc.nextInt(); 
    //rest of your code and while loop 
}