(java) найти ошибки в коде.
import java.util.Scanner:
private class Main {
import java.util.Scanner:
private class Main {
public static void main(String args) {
char operator;
Double number1, number2, result;
Scanner input = new Scanner(System.in);
System.out.print("Choose an operator: +, -, *, or /");
operator = input.next().charAt(0);
// ask users to enter numbers
System.out.print("Enter first number");
number1 = input.nextDouble();
System.out.println("Enter second number");
number2 = input.nextDouble();
switch (operator) {
// performs addition between numbers
case '+':
result = number1 + number2;
System.out.println(number1 + " + " + number2 + " = " + result);
break;
// performs subtraction between numbers
case '-':
result = number1 - number2;
Answers & Comments
Код не работает из-за того, что вы в аргумент подпрограммы main вызываете String аргумент.
Ваша программа должна получить доступ к этому аргументу из этого массива, а не строки.
Проще говоря, добавьте [] после String:
public static void main(String[] args)
Допишем программу, чтобы она работала корректно:
(Добавим остальные действия (- * /))
import java.util.Scanner;
public class manager {
public static void main(String[] args) {
char operator;
Double number1, number2, result;
Scanner input = new Scanner(System.in);
System.out.print("Choose an operator: +, -, *, or /");
operator = input.next().charAt(0);
// ask users to enter numbers
System.out.print("Enter first number");
number1 = input.nextDouble();
System.out.println("Enter second number");
number2 = input.nextDouble();
switch (operator) {
// performs addition between numbers
case '+':
result = number1 + number2;
System.out.println(number1 + " + " + number2 + " = " + result);
break;
// performs subtraction between numbers
case '-':
result = number1 - number2;
System.out.println(number1 + " - " + number2 + " = " + result);
break;
case '*':
result = number1 * number2;
System.out.println(number1 + " * " + number2 + " = " + result);
break;
case '/':
result = number1 / number2;
System.out.println(number1 + " / " + number2 + " = " + result);
break;
}
}
}