Java 应用程序整数



我正在尝试使用 if 语句创建我的第一个 java 应用程序,该语句将采用一个整数(例如 x 22),并找出其总和在乘减时是否相等(例如 x 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" );
    }

   } 
}

您只需将它们分配给不同的变量并检查条件

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:你质疑说乘减,但例子之后的例子是(例如 x 2 x 2=4 和 2+2=4)。我和前任一起去了。

试试

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();
    }
}

相关内容

最新更新