我正在制作一个基本的计算器。当我尝试比较字符串并使用next()
时,它工作正常,但是如果我使用nextLine()
,它就不起作用。怎么会这样?不是next
和nextLine
实际上是一回事,只是一个跳过一行而一个不跳过?
这是我的代码:
import java.util.Scanner;
class apples{
public static void main(String args[]){
Scanner Max = new Scanner(System.in);
int num1, num2, answer;
String plumi;
System.out.println("enter your first number");
num1 = Max.nextInt();
System.out.println("enter your second number");
num2 = Max.nextInt();
System.out.println("enter if you want to use plus or minus");
plumi = Max.next();
if(plumi.equals("plus")){
answer = num1 + num2;
System.out.println("the answer is " + answer);
}
if(plumi.equals("minus")){
answer = num1 - num2;
System.out.println("the answer is " + answer);
}
}
}