Java是一門常用的編程語言,它可以實現(xiàn)各種功能,包括輸入兩個數(shù)和一個運算符。
import java.util.Scanner; public class Calculator { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("請輸入第一個數(shù):"); double num1 = scan.nextDouble(); System.out.print("請輸入第二個數(shù):"); double num2 = scan.nextDouble(); System.out.print("請選擇運算符(+、-、*、/):"); String operator = scan.next(); if (operator.equals("+")) { System.out.println(num1 + " + " + num2 + " = " + (num1 + num2)); } else if (operator.equals("-")) { System.out.println(num1 + " - " + num2 + " = " + (num1 - num2)); } else if (operator.equals("*")) { System.out.println(num1 + " × " + num2 + " = " + (num1 * num2)); } else if (operator.equals("/")) { System.out.println(num1 + " ÷ " + num2 + " = " + (num1 / num2)); } else { System.out.println("運算符輸入有誤!"); } } }
以上代碼是一個簡單的輸入兩個數(shù)和一個符號程序,通過Scanner類的nextDouble方法可以輸入兩個數(shù),通過next方法可以輸入運算符,然后使用if語句判斷運算符并進行相應的計算。開發(fā)者還可以根據(jù)需要擴展代碼實現(xiàn)更多的功能。