我正在编写一个程序来对从用户那里得到的整数执行一些算术运算。我不断收到有关减法变量不是原始类型int的错误。
import java.util.Scanner;
//This program is used to display the sum, product, difference and quotient of two numbers
public class test6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Create a scanner to obtain user input
Scanner input = new Scanner(System.in);
int x,y; //Declare the two integer variables
int sum, product;
double quotient;
long subtract;
System.out.println("Enter the first integer: "); //Prompt user for input
x = input.nextInt(); //Read first integer
System.out.println("Enter the second integer: "); //Prompt user for input
y = input.nextInt(); //Read second integer
sum = x + y; //Calculate the sum of the two integers
subtract = x - y; //Calculate the subtract of the two integer
product = x * y; //Calculate the product of the two integers
quotient = x % y; //Calculate the quotient of the two integers
System.out.printf("The sum of %d and %d is %d", x , y, sum);
System.out.printf("The difference of %d and %d is %d", x , y. subtract);
System.out.printf("The product of %d and %d is %d", x , y, product);
System.out.printf("The quotient of %d and %d is %d", x , y , quotient);
}
}
答案 0 :(得分:1)
你只有一个typo
System.out.printf("The difference of %d and %d is %d", x , y. subtract);
将您的dot
更改为comma
。
正如@Kevin所指出的那样,编译器会尝试将subtract
解析为int
变量上的字段。
System.out.printf("The difference of %d and %d is %d", x , y, subtract);
答案 1 :(得分:0)
变量商不是整数类型。因为它是整数除法的余数的结果。它应该被定义为类型double