我正在尝试将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;
Run Code Online (Sandbox Code Playgroud)
这里有更多罗马数字中所有"例外"的代码.} return decimal; }}}
您需要一个main方法作为程序的入口点.只有在创建对象并在新创建的对象上调用它的方法时,此类才可用.
你需要的是主要方法:
public static void main(String args[]) {
// Your code that should be executed on start, here
}
Run Code Online (Sandbox Code Playgroud)
但是,不要将所有代码都放在main方法中.这是不好的做法.
| 归档时间: |
|
| 查看次数: |
64 次 |
| 最近记录: |