2015-11-23 50 views
1

這是作業,我大部分都是這樣,但我覺得我的素數生成器已關閉,因爲當我運行它時,它只會將1和3的Y值增加5而不是所有其他的素數之間1和100;Prime Number Java

繼承人的功課問題

It's Joen's Birthday and his friends have hidden his gift somewhere in his  house. 

Joen begins at the origin: (0,0) 

He has to follow 100 commands. The first being command 1, the second being command 2, etc. 

Only one of the following can be applied at once. 

-If the command number is both prime and odd, he must take 5 steps ahead (up) 
-Otherwise if the command number is both prime and even, he must take 3 steps backwards (down) 
-Otherwise if the command number is even, he must take one step to the right. 
-Otherwise if the command number is odd, he must take one step to the left 

The following rule can be applied in addition to another rule 
-If the command number is divisible by 7, he must take the quotient number of steps down. 

Using a coordinate system, at what point is the gift hidden? Create a program that shows the steps Joen takes to find his hidden gift. 

這裏是我的代碼

import java.util.*; 

public class Party{ 
public static void main(String[] args) { 

    int counterCommand = 1; 
    int x = 0; 
    int y = 0; 
    int temp; 
    boolean isPrime=true; 
    while (counterCommand <= 100) { 
     int num=counterCommand; 
     for(int i=2;i<=num/2;i++){ 
     temp=num%i; 
     if(temp==0){ 
      isPrime=false; 
      break;} 
     } 

     if (isPrime==true){ 
      if (counterCommand % 2 != 0){ 
       y = y + 5; 
      } 
       else { 
        y = y - 3; 
       } 
      } 
      else{ 
       if (counterCommand % 2 != 0){ 
        x = x - 1; 
       } 
       else{ 
        x = x + 1; 
       } 

      } 
      counterCommand = counterCommand + 1; 
     } 
     System.out.print(x + "," + y); 
    } 
} 

怎麼過,當我運行它時,Y的值不針對任何首相的賠率是增加不是一個或三個(5,7,13等)爲什麼呢?因爲我知道1,7 Im得到的是不正確的

回答

3

在我看來,你需要在循環內重置isPrime爲真,此時你只設置一次,而不是每個迭代器的數字。

因此,在找到第一個合成數(4)後,您的質數測試失敗。

+0

好的生病請嘗試thx – inzikind

+0

工作的thankx – inzikind