2014-10-16 106 views
-4
import java.util.Scanner; 

public class HelloWorldJavaMain 
{ 

    public static void main(String[] args) 
    { 

     Scanner userInputScanner = new Scanner(System.in); 

     System.out.println("Enter the first number."); 
     int inputA = userInputScanner.nextLine(); 

     System.out.println("Enter the second number."); 
     int inputB = userInputScanner.nextLine(); 

     int sumOfInputs = inputA + inputB; 

     System.out.println(inputA + " + " + inputB + " = " + sumOfInputs); 

    } 

} 

有人能告诉我我哪里出错了吗?基本Java加法计算器

+1

INT inputA = userInputScanner.nextLine(); – 2014-10-16 20:36:14

+1

这甚至不会编译。如果你尝试,你会得到一个错误信息,将指向你正确的方向。 – dimo414 2014-10-16 20:38:04

+0

多数民众赞成在写什么... – user3832004 2014-10-16 20:38:20

回答

1
int inputA = userInputScanner.nextLine(); 

应更改为

int inputA = userInputScanner.nextInt(); 

因为nextline不返回INT。或者你可以使用

Integer.parseInt(userInputScanner.nextLine())

同样的事情inputB

0

使用userInputScanner.nextInt();

userInputScanner.nextLine()读取一个String行。

0
Scanner userInputScanner = new Scanner(System.in); 
int inputA = userInputScanner.nextInt(); 
int inputB = userInputScanner.nextInt(); 
int sumOfInputs = inputA + inputB; 
System.out.println(inputA + " + " + inputB + " = " + sumOfInputs); 

检查此http://ideone.com/0eR41r