【发布时间】:2016-01-30 10:22:02
【问题描述】:
我对 java 还很陌生,所以不要认为这是一些白痴。无论如何,我一直在尝试制作一个程序,它可以从控制台读取某个字母,然后决定使用哪个操作,比如说添加。但是,我无法获得一个 If 循环来读取决定使用哪个运算符的变量,这是代码,请帮忙。
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner user_input = new Scanner( System.in );
int number;
String function;
System.out.println("What Do You Want to Do? (a to add; s to" +
" subrtact; d to divited; m to multiply, and sq to square your nummber.)" );
function = user_input.next();
if (function == "sq"){
System.out.print("Enter your number: ");
number = user_input.nextInt();
System.out.print(number * number);
} else {
System.out.println("Unidentified Function!");
}
}
}
(我把描述写得更短以便合适)。
【问题讨论】: