decode()方法在Java中是如何工作的?

kmbjn2e3  于 2022-12-21  发布在  Java
关注(0)|答案(1)|浏览(142)

我需要做一个计算器,它可以处理最多10个阿拉伯数字和罗马数字。我决定使用 String 方法 .compareTo() 来帮助我的程序判断它是一个还是另一个。现在,我想把数值型的 * String * 转换成 Integers。我found了一个 int 方法,用来把 * String * 解码成 * int *,但它似乎不起作用:它会返回一个错误

Introduction.java:52: error: int cannot be dereferenced
      firstNum = firstNum.decode(firstNumS);
                         ^
Introduction.java:90: error: int cannot be dereferenced
      secondNum = secondNum.decode(secondNumS);

这是我的代码(请耐心听我说,它可能有点笨拙)

import java.util.Scanner;
import java.io.IOException;

public class Introduction {
  static int firstNum = 0;
  static int secondNum = 0;
  static double result;
  public static void main(String[] args) {
    String firstNumS = "1";
    String secondNumS = "2";
    String operationS = "+";
    Scanner scanner = new Scanner(System.in);
    firstNumS = scanner.next();
    operationS = scanner.next();
    secondNumS = scanner.next();
    if(firstNumS.compareTo(operationS) > 15){
      switch(firstNumS){
      case "I":
      firstNum = 1;
      break;
      case "II":
      firstNum = 2;
      break;
      case "III":
      firstNum = 3;
      break;
      case "IV":
      firstNum = 4;
      break;
      case "V":
      firstNum = 5;
      break;
      case "VI":
      firstNum = 6;
      break;
      case "VII":
      firstNum = 7;
      break;
      case "VIII":
      firstNum = 8;
      break;
      case "IX":
      firstNum = 9;
      break;
      case "X":
      firstNum = 10;
      break;
      default:
      System.out.println("I don't know the first number!");
    }
    } else {
      firstNum = firstNum.decode(firstNumS);
    }
    if(secondNumS.compareTo(operationS) > 15){
      switch(secondNumS){
      case "I":
      secondNum = 1;
      break;
      case "II":
      secondNum = 2;
      break;
      case "III":
      secondNum = 3;
      break;
      case "IV":
      secondNum = 4;
      break;
      case "V":
      secondNum = 5;
      break;
      case "VI":
      secondNum = 6;
      break;
      case "VII":
      secondNum = 7;
      break;
      case "VIII":
      secondNum = 8;
      break;
      case "IX":
      secondNum = 9;
      break;
      case "X":
      secondNum = 10;
      break;
      default:
      System.out.println("I don't know the second number!");
    }
    } else {
      secondNum = secondNum.decode(secondNumS);
    }
    if(operationS == "+"){
      result = firstNum + secondNum;
    } else if(operationS == "-") {
      result = firstNum - secondNum;
    } else if(operationS == "*"){
      result = firstNum * secondNum;
    } else if(operationS == "/"){
      result = firstNum / secondNum;
    } else {
      System.out.println("I don't know such an operation!");
    }

    System.out.println(result);
  }
}
r3i60tvu

r3i60tvu1#

decode是一个static方法,它属于 Package 类Integer,而不是原语int(原语根本没有方法):

firstNum = Integer.decode(firstNumS);

相关问题