2013-10-19 4 views
0

정수 (예 22)를 취해 곱셈 및 뺄셈 (ex 2 * 2 = 4 및 2)의 합이 동일한 지 알아내는 if 문으로 주먹 Java 응용 프로그램을 만들려고합니다. + 2 = 4) 또는 그렇지 않은 경우.Java 응용 프로그램 정수

if 결정을 내리는 방법을 알 수는 없지만. 누군가 그 일을하는 방법을 지적 할 수 있습니까?

당신은 단지 다른 변수에 할당하고 조건을 확인해야 할

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

시작을하려고합니다. –

답변

0

는 자바 튜토리얼에서 identifiers`와`variables` 항목 '에서

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

U 감사

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 : 당신의 질문은 가 곱을 빼기하지만, 예를 말한 예를 들어 (예 2 x 2 = 4 및 2 + 2 = 4) 인 경우입니다. 나는 으로 갔다.