我的代码编译但不执行.我哪里做错了?如何在未来的代码中避免这种情况?

 宁艺汉先生 发布于 2023-02-13 21:15

我正在尝试将3,999以下的任何罗马数字转换为十进制数.代码编译但从不提示我输入罗马数字,就像扫描仪要求的那样.

//necessary for the scanner  
import java.util.Scanner; 
/**
* conversion of roman numerals into decimals
* 
* @Annika Helverson 
* period 5
*/

public class Numeral
{

private static Scanner scanner = new Scanner( System.in );
public static int decimalValue (String romanNumeral) {
    int decimal = 0;
    int count = 0;
    int i = 1;
  //tell user what to do
System.out.print( "Please enter a valid roman numeral: ");
//read the submitted number
String input = scanner.nextLine();
//display input back to user
System.out.println( "input =" + input );

    while (count < romanNumeral.length ()-1 || i < romanNumeral.length ()){
    if (romanNumeral.substring (count, i).equals ("M")){
        decimal = decimal + 1000;
    }
    if (romanNumeral.substring (count, i).equals ("D")) {
        decimal = decimal + 500;
    }
    if (romanNumeral.substring (count, i).equals ("C")) {
        decimal = decimal + 100;
    }
    if (romanNumeral.substring (count, i).equals ("L")) {
        decimal = decimal + 50;
    }
    if (romanNumeral.substring (count, i).equals ("X")) {
        decimal = decimal + 10;
    }
    if (romanNumeral.substring (count, i).equals ("V")) {
        decimal = decimal + 5;
    }
    if (romanNumeral.substring (count, i).equals ("I")) {
        decimal = decimal + 1;
    }
    count = count + 1;
    i = i + 1;
    } 
    {
    if (romanNumeral.contains ("CM")){
    decimal = decimal - 200;

这里有更多罗马数字中所有"例外"的代码.} return decimal; }}}

1 个回答
  • 您需要一个main方法作为程序的入口点.只有在创建对象并在新创建的对象上调用它的方法时,此类才可用.

    你需要的是主要方法:

    public static void main(String args[]) {
        // Your code that should be executed on start, here
    }
    

    但是,不要将所有代码都放在main方法中.这是不好的做法.

    2023-02-13 21:21 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有