如何在Java编程中添加两个数字

时间:2013-10-23 01:20:39

标签: java

import java.util.*;
public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int x, y, sum;

        Scanner input = new input(system.in);

        input = parseInt32();


    }

}

这是例外:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    input cannot be resolved to a type
    input cannot be resolved to a type
    system cannot be resolved to a variable
    The method parseInt32() is undefined for the type Test

    at Test.main(Test.java:12)

2 个答案:

答案 0 :(得分:2)

您需要使用nextInt();,例如:

public static void main(String[] args) {
    // TODO Auto-generated method stub


    System.out.println("First number: ");
    Scanner input = new Scanner(System.in);

    int x = input.nextInt(); 

    System.out.println("Second number: ");
    int y = input.nextInt();  

    int sum = x+y;

    System.out.println("Result: "+sum); 
}

答案 1 :(得分:1)

Scanner input = new input(system.in); 应该 Scanner input = new Scanner(System.in);

它正在给您一个错误,因为您正在尝试将输入设置为int转换。它看起来应该是这样的:

x = input.nextInt();