简体   繁体   English

y的原始类型int在编译期间没有字段

[英]The Primitive type int of y does not have a field during Compiling

I am writing a program to perform some arithmetic operations on integers gotten from user. 我正在编写一个程序,对从用户获得的整数执行一些算术运算。 I keep getting an error concerning the subtract variable not being a primitive type int. 我不断收到一个错误,该错误涉及减去变量不是原始类型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);

    }

}

You just have a typo in line 你刚typo

 System.out.printf("The difference of %d and %d is %d", x , y. subtract);

Change your dot to comma . dot更改为comma As @Kevin pointed the compiler tries to resolve subtract as a field on the int variable. 正如@Kevin所指出的,编译器尝试将subtract解析为int变量上的一个字段。

System.out.printf("The difference of %d and %d is %d", x , y, subtract);

The variable quotient is not an integer type. 变量商不是整数类型。 As it is the the result of the remainder of an integer division. 因为这是整数除法运算余数的结果。 It should be defined as a type double 应该定义为double类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM