gpt4 book ai didi

java - y 的原始类型 int 编译时没有字段

转载 作者:行者123 更新时间:2023-12-01 11:04:07 24 4
gpt4 key购买 nike

我正在编写一个程序来对从用户那里获得的整数执行一些算术运算。我不断收到有关减法变量不是基本类型 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);

}

}

最佳答案

您只是在一行中有一个拼写错误

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

更改为逗号。正如 @Kevin 指出的,编译器尝试将 subtract 解析为 int 变量上的字段。

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

关于java - y 的原始类型 int 编译时没有字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33120534/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com