开关不兼容类型错误 [英] Switch Incompatible Type Error

查看:90
本文介绍了开关不兼容类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的课编写一个罗马数字程序.我正在使用switch语句将字符串转换为整数.但是,我在运行它时遇到了不兼容的类型错误.我正在运行Java 7,所以这不是问题.这是我的代码:

I'm writing a roman numeral program for my class. I'm using a switch statement to convert strings to integers. However I'm getting an incompatible type error when I run it. Im running java 7 so that's not the issue. Here's my code:

public static void main()
{
    // Declare local variables
    Scanner input = new Scanner(System.in);
    String rNum;
    int i;
    int[] rArray;

    // Display program purpose
    System.out.println("This Program Converts Roman numerals to decimals");
    System.out.println("The Roman numerals are I (1), V (5), X (10), L (50), C (100), D (500) and M (1000).");
    System.out.print("Enter your roman numeral: ");

    rNum = input.next().toUpperCase();

    rArray = new int[rNum.length()];

    for(i = 0; i < rNum.length(); i++){
      switch(rNum.charAt(i)){
          case "I": rArray[i] = 1;
          break;
      }
      }

推荐答案

"I"是一个字符的字符串. 'I'是字符I,键入char,这是您在大小写块中所需要的.

"I" is a one character String. 'I' is the character I, type char, which is what you need in your case block.

这篇关于开关不兼容类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆