无法计算java中的产品,商和差异

时间:2009-09-21 02:34:01

标签: java

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

1 个答案:

答案 0 :(得分:4)

除语法错误外:

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

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

您获得的实际产量是多少?是错误还是不存在

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

警告:如果希望成为一名体面的开发者,请不要在下面阅读以下内容,直到您花费大量时间尝试使用问题解决问题只是上面的建议。

我自己,我不在乎,因为你不太可能与我竞争,因为地理分离,技能差异以及我快速退休的年龄: - )

如果你在尝试之后仍然陷入困境,这是我的解决方案,但我强烈反对使用它(假设这是家庭作业而不仅仅是练习),而不是修复自己的代码,原因如下:

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

这是:

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