gpt4 book ai didi

java - 无法在java中计算乘积、商和差

转载 作者:行者123 更新时间:2023-12-01 19:25:25 25 4
gpt4 key购买 nike

我正在尝试在一个简单的学校java应用程序中计算一些东西。用户必须输入 2 个整数,一旦您输入这些变量,它就会输出总和、乘积、商和差。我只能让程序吐出总和,但不能吐出乘积、商和差。有什么建议么?这是我的代码:

// This is for HW problem #3
// An application that asks the user to enter two
// integers, obtains them from the user and prints their sum,
// product, difference and quotient (division).

import java.utl.Scanner; //program uses class Scanner

public class Solution
{
// main method begins execution of Java application
public static void ( String ags [] )
{
// create Scanner to obtain input from command window
Scanner input = new Scanner ( System.in );

int number1; //first integer to add
int number2; //second integer to add
int sum; // sum of number1 and number2
int product; //product of number 1 and number2
int differnec; //difference of number1 and number2
int quotient; //quotient of number1 and number2

System.out.print( "Enter first integer: " ); // prompt
number1 = input.nextInt(); // read first number from user

System.out.print( "Enter second integer: " ); // prompt
number2 = input.nextInt(); // read second number from user

sum = number1 + number 2; // add numbers
product = number1 * number2; // multiply numbers
difference = number1 - number2; // subtract numbers
quotient = number1 / number2; // divide numbers

System.out.printf( " Sum is %d\n ", sum ); //display sum

System.out.printf( " Product is %d\n ", product ); //display prouduct

System.out.printf( " Difference is %d\n ", difference ); //display difference

System.out.printf( " Quotient is %d\n ", quotient ); //display quotient

} // end method main

} // end class Addition

最佳答案

除了语法错误之外:

import java.utl.Scanner;
public static void ( String ags [] )
int differnec;
difference = number1 - number2;
sum = number1 + number 2;

这会阻止该代码编译,看起来没问题。

您得到的实际输出是什么?是错误还是不存在

第 1 步,修复语法错误,然后重试。确保您在此处粘贴的代码与您正在测试的代码完全相同。

警告:如果您曾经想成为一名优秀的开发人员,请不要阅读下面的内容,除非您花费了大量时间尝试使用以下方法解决问题只是上面的建议。

我自己,无论如何,我都不在乎,因为由于地理隔离、技能差异和我即将退休的年龄,你不太可能与我竞争:-)

如果您在尝试后仍然遇到困难,这是我的解决方案,但我强烈建议不要使用它(假设这是家庭作业而不仅仅是练习),而不是修复您自己的代码,原因如下:

  • 如果您从错误中吸取教训,您将成为更好的问题解决者和开发者。
  • 您的教育 worker 肯定会关注此网站,如果您逐字提交作业,您将会失败。

这里是:

import java.util.Scanner;
public class Solution {
// main method begins execution of Java application
public static void main (String ags []) {
// create Scanner to obtain input from command window
Scanner input = new Scanner ( System.in );

int number1; //first integer to add
int number2; //second integer to add
int sum; // sum of number1 and number2
int product; //product of number 1 and number2
int difference; //difference of number1 and number2
int quotient; //quotient of number1 and number2

System.out.print( "Enter first integer: " ); // prompt
number1 = input.nextInt(); // read first number from user

System.out.print( "Enter second integer: " ); // prompt
number2 = input.nextInt(); // read second number from user

sum = number1 + number2; // add numbers
product = number1 * number2; // multiply numbers
difference = number1 - number2; // subtract numbers
quotient = number1 / number2; // divide numbers

System.out.printf (" Sum is %d\n ", sum );
System.out.printf (" Product is %d\n ", product );
System.out.printf (" Difference is %d\n ", difference );
System.out.printf (" Quotient is %d\n ", quotient );
} // end method main
} // end class Addition

其中吐出:

Enter first integer: 5
Enter second integer: 2
Sum is 7
Product is 10
Difference is 3
Quotient is 2

关于java - 无法在java中计算乘积、商和差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1452620/

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