2017-02-20 61 views
0
public class FavNum { 

    public static void isEven(int x){ 
     boolean b; 
     if (x%2==0) { 
      b=true; 
     }else{ 
      b=false; 
     } 
    } 

    public static void isSingle(int x){ 
     boolean b; 
     if (x > 0 && x < 10){ 
      b=true;   
     }else{ 
      b=false; 
      JOptionPane.showMessageDialog(null, "Your favorite number is not a single digit!");   
     } 
    } 

    public static void all(int x){ 
     isEven (x); 
     //What should I add here to use isEven and isSingle in conditionals? 
     //For example, I want to say something like if isEven and isSingle are true, then say this. 
     //Or if isEven is true and isSingle is not, say this. But I don't know how to properly write those conditionals. 
    } 

    public static void main(String[] args) { 
     int x = Integer.parseInt(JOptionPane.showInputDialog(null, "What is your favorite number?"));   
    }  
} 

這個方向是如下「使用布爾真,在條件語句假

  • 創建一個名爲FavoriteNumber新項目
  • 編寫一個叫做ISEVEN布爾方法,該方法取一個整數參數,並檢測是否返回true或false
  • 編寫第二個名爲isSingleDigit的布爾方法,它接受一個整型參數並檢測該整數是否爲單個數字,返回true或false
  • 主要提示用戶輸入他們最喜歡的號碼。
  • 測試一個條件語句,如果偶數和一個數字的數量,打印消息(創意)。
  • 測試與另一條件如果兩個奇數,而不是一個單一的數字和打印另一(素材)消息。
  • 添加一個條件是測試如果奇數或一個數字,並打印創造性的消息。
  • 添加的最終條件是測試如果連而不是一個單一的數字,並打印

到目前爲止,我已經完成了前4發子彈。我有麻煩(不要以爲一個最終的廣告訊息」我完全理解)第5子彈。我試圖創建一個名爲所有新的方法,我打算打電話isSingle和ISEVEN和使用if else語句來比較它們和返回相應的信息做到這一點。但我被卡住,留下評論在我的上面的代碼,解釋我的問題。

有人可以幫助我完成這項任務嗎?或者至少指出我在正確的方向嗎?

謝謝!

+2

x已經是布爾型 –

+0

我該如何解決這個問題?這是我在班上的第一週,所以我還不是很有經驗。 –

+0

@LaurenMcCabe我認爲你將函數的返回類型與函數所需的參數混合在一起。函數isOff可能需要一個數字作爲參數並返回一個布爾值。你的布爾值作爲一個參數並且什麼也不返回。 –

回答

1
public class Joption { 

    // declared outside so easier to be accessed by other methods in the class 
    private static int x; 

    // is the number passed into this method even? 
    private static boolean isEven(int x) { 
     if (x % 2 == 0) { 
      return true; 
     } 
     return false; 
    } 

    // is the number passed into this method single? 
    private static boolean isSingle(int x) { 
     if (x >= 0 || x < 10) { 
      return true; 
     } 
     return false; 
    } 

    // is the number passed in even AND single? 
    // notice how the number passed in flows into the two methods you just created. Their returns help you determine if they are even AND single. 
    private static void isEvenAndSingle(int x) { 
     if (isEven(x) && isSingle(x)) { 
      JOptionPane.showMessageDialog(null, x + "is Even and Single!"); 
     } 
    } 

    // same as above method but now it's checking to see if it's even and NOT single. 
    private static void isEvenAndNotSingle(int x) { 
     if (isEven(x) && !(isSingle(x))) { 
      JOptionPane.showMessageDialog(null, x + "is Even but NOT single!"); 
     } 
    } 

    // your main running method 
    // here you actually need to call the isEvenAndSingle() and isEvenAndNotSingle() methods with the retrieved x value from the JOptionPane so that you can actually print the appropriate messages in the responding JOptionPane 
    public static void main(String[] args) { 
     x = Integer.parseInt(JOptionPane.showInputDialog(null, "What is your favorite number?")); 
     isEvenAndSingle(x); 
     isEvenAndNotSingle(x); 
    } 

} 

您的問題布爾,如前所述,是您檢索的方式來自您的JOptionPane的價值。你把它當作一個int。但是你將它作爲布爾值傳遞給你的方法。你實際需要做的是將值作爲int傳遞(參見isEven()和isSingle()方法),並讓它們返回一個布爾值。

然後你被要求創建與您剛剛創建的兩個方法進行了比較。一個用於數字是偶數和單數,另一個用於數字是偶數而不是單數。

我註釋的代碼註釋,以幫助您更好地瞭解!祝你未來的java編碼順利!:)

+0

非常感謝你!!!!!!!! –

+0

不用擔心! :D希望你有更多的樂趣編碼!給我留言,如果你需要更多的解釋! – blaytenshi

2
與數字

您比較布爾:

boolean x; 

if (x%2==0) { //Error here 
    b=true; 
} 

if (x > 0 && x < 10){  //Error here 
     b=true; 

你不能做到這一點。

如果x是truefalse您不能檢查它是否大於0或者對它進行算術運算。

0

看起來你有算法部分想通了 從你的方法,你需要返回,而不是無效

public static boolean isEven(int x){ 
    return (x % 2) == 0; 
}