0

我正在尝试使用 if 语句创建我的第一个 java 应用程序,该语句将采用整数(例如 22),并在乘法和减法时找出其总和是否相等(例如 2*2=4 和 2+2=4)或如果不是。

虽然我不知道如何做 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" );
    }


   } 

}
4

2 回答 2

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 and 2+2=4)。我和前任一起去了。

于 2013-10-19T16:51:53.973 回答
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();
    }
}
于 2013-10-19T16:56:11.587 回答