gpt4 book ai didi

Java:纠错码计算器

转载 作者:行者123 更新时间:2023-12-02 07:34:30 24 4
gpt4 key购买 nike

如何纠正行中的错误

char oper = entrada.next(); //ERROR HERE

在代码中:

/*
* Calculator
*/
package list01;

import java.util.Scanner;

public class ex04 {

public void metodoEx04() {
Scanner entrada = new Scanner(System.in);
System.out.print("First number: ");
int A = entrada.nextInt();
System.out.print("Second number: ");
int B = entrada.nextInt();
System.out.print("Operator +, -, *, ou /: ");
char oper = entrada.next(); //ERROR HERE
double res = calculadora(A, B, oper);
System.out.println(res);
}

static double calculadora(int n1, int n2, char operador) {
double res;
switch (operador) {
case '+':
res = soma(n1, n2);
return res;
case '-':
res = subtracao(n1, n2);
return res;
case '*':
res = multiplicacao(n1, n2);
return res;
default:
res = divisao(n1, n2);
return res;
}
}

static double soma(int num1, int num2) {
double res;
res = num1 + num2;
return res;
}

static double subtracao(int num1, int num2) {
double res;
res = num1 - num2;
return res;
}

static double multiplicacao(int num1, int num2) {
double res;
res = num1 * num2;
return res;
}

static double divisao(int num1, int num2) {
double res;
if (num2 != 0) {
res = num1 / num2;
return res;
} else {
System.out.println("Impossible.");
return 0.0;
}
}
}

最佳答案

next 给出一个字符串,你想要一个字符。你可以试试

char oper = entrada.next().charAt(0);

关于Java:纠错码计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12452147/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com