2013-10-19 34 views
0

我想創建我的第一個Java應用程序與if語句,將採取一個整數(實例22),並找出它的總和是否相等時乘以和減(例如2 * 2 = 4和2 + 2 = 4)或者不是。Java應用程序整數

雖然我無法弄清楚如何做決定。有人可以指出如何做到這一點?

感謝ü

package 1; 

import java.util.Scanner; 

public class 1 
{ 

    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 

    int x; 
    int y; 
    System.out.print("Enter a number from 10 to 99: "); 
    x = input.nextInt(); 

    if (x >= 10 && x <= 99) 
    { 
      x= x % 10; 
      x= x/10; 

    } 
    else 
    { 
     System.out.println("you must enter a number from 10 to 99"); 
    } 


    } 

} 
+3

開始從'identifiers'和'variables'主題中任何Java教程。 –

回答

0

嘗試

import java.util.Scanner; 
public class One { 
    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     int x; 
     int y; 
     System.out.print("Enter a number from 10 to 99: "); 
     x = input.nextInt();   
     if (x >= 10 && x <= 99) { 
      y = x % 10; 
      x = x/10 ; 
     if(x* y== x+ y)){ 
      System.out.println("Sum and product are equal"); 
     } 
     else 
      System.out.println("Sum and product are not equal"); 

     } else { 
      System.out.println("you must enter a number from 10 to 99"); 
     } 
     input.close(); 
    } 
} 
+0

* if(x == y){*這種情況是完全錯誤的。再次閱讀問題。 – SudoRahul

+0

@ R.J是的,你的答案正確答案+1,謝謝你指出,已經更新了代碼 – upog

1

你只需將它們分配到不同的變量,並檢查條件

if (x >= 10 && x <= 99) { 
    int first = x/10; // Take out the first digit and assign it to a variable first 
    int second = x % 10; // Take out the second digit and assign it to a variable second 

    if (first * second == first + second) { // Check for your condition, which you mentioned in your question 
     System.out.println("Yep, they match the condition"); // If it satisfies the condition 
    } else { 
     System.out.println("Nope, they don't match the condition"); // If it doesn't satisfy the condition 
    } 

} 

PS:您提問時表示相乘,並減去但例如剛之後的例子是(ex 2 x 2 = 4和2 + 2 = 4)。我去了ex