使用Java-Eclipse SDK,如何将2个数字作为输入

时间:2010-07-30 10:29:34

标签: eclipse

我是初学者。 使用Java-Eclipse SDK,如何将2个数字作为输入,打印总和? 我打不开项目!请有人帮助我,告诉我一步一步做什么?

谢谢.. 坦维尔

1 个答案:

答案 0 :(得分:3)

您是否尝试创建Java项目,将两个数字作为输出,并打印总和?如果这是真的,你应该阅读一些基本的Java / Eclipse教程。我建议Eclipse and Java Tutorial for Total Beginners - 它应该让你开始如何创建/打开一个项目以及如何使用eclipse。

至于获取两个数字并打印出总和,你应该真正学习Java IO并自己动手,但快速的谷歌搜索正是你想要的:

import java.io.*; //imports java.io class 

/* 
 * Adds 2 integers given by the user, then prints out the sum. 
 * Directly copied from http://www.dreamincode.net/code/snippet492.htm, as I 
 * am too lazy to write this myself :)
 */ 

public class Add2number { 

  //main(): application entry point 
  public static void main(String[] args) throws IOException { 

        //set input stream 
        BufferedReader stdin = new BufferedReader( 
              new InputStreamReader(System.in)); 

        //get numbers from user input 
        //asks user for 1st number, then converts it to an integer 
        System.out.print("Enter first integer: "); 
        int x = Integer.parseInt(stdin.readLine()); 
        //asks user for 2nd number, than converts it to an integer 
        System.out.print("Enter second integer: "); 
        int y = Integer.parseInt(stdin.readLine()); 

        //add x,y 
        int sum = x + y; 

        //Display sum of x,y 
        System.out.println("The sum is: " + sum); 
  }//ends main 

}//ends addReadLine