无法计算 Java 中的乘积、商和差

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1452620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:35:24  来源:igfitidea点击:

Unable to compute product, quotient, and difference in java

java

提问by paxdiablo

I'm trying to computing a few things in a simple java application for school. A user must into 2 integers and once you input those variables in it will spit out the sum, product, quotient and difference. I am only able to get the program to spit out sum but not product, quotient and difference. any suggestions? here is my code:

我正在尝试在一个简单的学校 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

回答by paxdiablo

Aside from the syntax errors:

除了语法错误:

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

which would stop that code from even compiling, it looks okay.

这将阻止该代码甚至编译,它看起来没问题。

What is the actual output you're getting? Is it wrongor is it not there?

你得到的实际输出是多少?是错误还是不存在

Step 1, fix the syntax errors then try again. Make sure the code you paste here is exactlythe same as the code you're testing.

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

Warning:If you everwant to be a decent developer, don't read below until you've spent quite a bit of time trying to fix your problems using just the suggestions above.

警告:如果你曾经想成为一个体面的开发商,不低于看,直到你已经花了相当多的时间试图修复只使用上述建议你的问题。

Myself, I don't care either way since you're unlikely to ever compete with me, due to geographical separation, skill difference and my fast-closing-on-retirement age :-)

我自己,我不在乎任何一种方式,因为由于地域分离、技能差异和我即将退休的年龄,你不太可能与我竞争 :-)

If you're still stuck after trying that, here's my solution but I strongly suggest against using it (assuming this is homework rather than just practice) rather than fixing your own code for the following reasons:

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

  • you'll be a better problem solver and developer if you learn by your mistakes.
  • your educators will certainly be watching this site and, if you hand in verbatim-copied work, you willfail.
  • 如果您从错误中学习,您将成为更好的问题解决者和开发者。
  • 你的教育工作者肯定会关注这个网站,如果你提交逐字复制的工作,你失败。

Here it is:

这里是:

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

which spits out:

吐出:

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