Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 没有为我的代码获取输出_Java_Java.util.scanner - Fatal编程技术网

Java 没有为我的代码获取输出

Java 没有为我的代码获取输出,java,java.util.scanner,Java,Java.util.scanner,我将罗马数字转换为十进制形式的代码似乎是正确的,但由于某些原因,我无法理解如何获得任何类型的输出。我想在结尾说“罗马数字”+罗马数字+“是”+十进制数字。如何,我该把它放在哪里 import java.util.Scanner; public class RomantoInteger { public static void main(String[] args) { Scanner input = new Scanner(System.in); System

我将罗马数字转换为十进制形式的代码似乎是正确的,但由于某些原因,我无法理解如何获得任何类型的输出。我想在结尾说“罗马数字”+罗马数字+“是”+十进制数字。如何,我该把它放在哪里

 import java.util.Scanner;
    public class RomantoInteger {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.println("Enter an Roman numeral with an equivalent between 0 and 3999:");
    }

    public class Roman {

    private final char[] romanNumerals = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };
    private final int[] romanEquivalent = { 1000, 500, 100, 50, 10, 5, 1 };
    private final String romanNum;   
    private int decimalNum; 

    public Roman(String input) {
        romanNum = input.toUpperCase();
    }

    //public void checkRoman(){
    //  for(int x=romanNum.length()-1;x<romanNum.length(); x++){
    //      if(romanNum.charAt(x)=='M')
    //  }
    //}

    /*
     * Method to convert roman number to decimal
     */
    public int convertToDecimal() {
        int sum[];                                     //Create a integer array
        sum = new int[romanNum.length()];               //set the size of sum array to the length of romanNum

        for (int y = 0; y < romanNum.length(); y++) {
            for (int i = 0; i < romanNumerals.length; i++) {
                if ((romanNum.charAt(y) == romanNumerals[i])       
                        && (romanNum.length() > 1)) {             //and if length of romanNum is greater than 1
                    sum[y] = romanEquivalent[i];                       //store the value of the matching romanNumeral to an indexed variable of array sum
                }
                if ((romanNum.charAt(y) == romanNumerals[i])           //if
                        && (romanNum.length() == 1)) {       //and if length of romanNum is just 1
                    System.out.println(romanEquivalent[i]);     //print out the value of the matching romanNumeral
                    break;                                       //terminate looping (for loop)
                }
            }
        }
        return add(sum);                                            //call method add
    }

    public int add(int[] sum) {
        int lastIndex = sum.length - 1;
        int decimalNum = sum[lastIndex];
        int count = 0;
        for (int i = lastIndex; count < sum.length - 1; count++) {
            if ((sum[i - 1] <= sum[i])) {                 
                decimalNum = decimalNum - sum[i - 1];
                --i;
            } else {
                decimalNum = decimalNum + sum[i - 1];
                --i;
            }
        }
        return decimalNum;
    }

}

}
import java.util.Scanner;
公共级浪漫主义者{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入一个罗马数字,其等效值介于0和3999之间:”);
}
公营罗马人{
私有final char[]罗马数字={'M','D','C','L','X','V','I'};
私有最终整数【】等价物={1000,500,100,50,10,5,1};
私有最终字符串romanNum;
私有整数小数;
公共罗马(字符串输入){
romanNum=input.toUpperCase();
}
//公共无效校验码(){
//对于(int x=romanum.length()-1;x 1)){//如果romanum的长度大于1
sum[y]=romanEquivalent[i];//将匹配的romanumeric的值存储到数组sum的索引变量中
}
if((romanNum.charAt(y)=罗马数字[i])//if
&&(romanNum.length()==1)){//若romanNum的长度仅为1
System.out.println(romanEquivalent[i]);//打印出匹配的罗马数字的值
break;//终止循环(用于循环)
}
}
}
返回add(sum);//调用方法add
}
公共整数相加(整数[]和){
int lastIndex=sum.length-1;
int decimalNum=和[lastIndex];
整数计数=0;
for(int i=lastIndex;countif((sum[i-1]很好地遵循了需要明确的概念

关于最终变量的事实

最终变量应在减速时初始化,如

private final romanNum = "SomeRomanNumber";

所以你的解决方案是

import java.util.Scanner;


    public class RomantoInteger {
        public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter an Roman numeral with an equivalent between 0 and 3999:");
        int value = input.nextInt();
        RomantoInteger rn = new RomantoInteger();
        Roman roman = rn.new Roman(""+value); //Roman is subclass of RomantoInteger and it is been called from static method so it should be call like this rn.new Roman(""+value);

        // Here you can call any function of Roman class
        }

        public class Roman {

        private final char[] romanNumerals = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };
        private final int[] romanEquivalent = { 1000, 500, 100, 50, 10, 5, 1 };
        private final String romanNum = "";   
        private int decimalNum; 

        public Roman(String input) {
            romanNum = input.toUpperCase();
        }

        //public void checkRoman(){
        //  for(int x=romanNum.length()-1;x<romanNum.length(); x++){
        //      if(romanNum.charAt(x)=='M')
        //  }
        //}

        /*
         * Method to convert roman number to decimal
         */
        public int convertToDecimal() {
            int sum[];                                     //Create a integer array
            sum = new int[romanNum.length()];               //set the size of sum array to the length of romanNum

            for (int y = 0; y < romanNum.length(); y++) {
                for (int i = 0; i < romanNumerals.length; i++) {
                    if ((romanNum.charAt(y) == romanNumerals[i])       
                            && (romanNum.length() > 1)) {             //and if length of romanNum is greater than 1
                        sum[y] = romanEquivalent[i];                       //store the value of the matching romanNumeral to an indexed variable of array sum
                    }
                    if ((romanNum.charAt(y) == romanNumerals[i])           //if
                            && (romanNum.length() == 1)) {       //and if length of romanNum is just 1
                        System.out.println(romanEquivalent[i]);     //print out the value of the matching romanNumeral
                        break;                                       //terminate looping (for loop)
                    }
                }
            }
            return add(sum);                                            //call method add
        }

        public int add(int[] sum) {
            int lastIndex = sum.length - 1;
            int decimalNum = sum[lastIndex];
            int count = 0;
            for (int i = lastIndex; count < sum.length - 1; count++) {
                if ((sum[i - 1] <= sum[i])) {                 
                    decimalNum = decimalNum - sum[i - 1];
                    --i;
                } else {
                    decimalNum = decimalNum + sum[i - 1];
                    --i;
                }
            }
            return decimalNum;
        }

    }

    }
import java.util.Scanner;
公共级浪漫主义者{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入一个罗马数字,其等效值介于0和3999之间:”);
int value=input.nextInt();
RomantoInteger rn=新的RomantoInteger();
Roman Roman=rn.new Roman(“+value);//Roman是RomantoInteger的子类,它是从静态方法调用的,所以应该像这样调用它,rn.new Roman(“+value”);
//这里可以调用Roman类的任何函数
}
公营罗马人{
私有final char[]罗马数字={'M','D','C','L','X','V','I'};
私有最终整数【】等价物={1000,500,100,50,10,5,1};
私有最终字符串romanNum=“”;
私有整数小数;
公共罗马(字符串输入){
romanNum=input.toUpperCase();
}
//公共无效校验码(){
//对于(int x=romanum.length()-1;x 1)){//如果romanum的长度大于1
sum[y]=romanEquivalent[i];//将匹配的romanumeric的值存储到数组sum的索引变量中
}
if((romanNum.charAt(y)=罗马数字[i])//if
&&(romanNum.length()==1)){//若romanNum的长度仅为1
System.out.println(romanEquivalent[i]);//打印出匹配的罗马数字的值
break;//终止循环(用于循环)
}
}
}
返回add(sum);//调用方法add
}
公共整数相加(整数[]和){
int lastIndex=sum.length-1;
int decimalNum=和[lastIndex];
整数计数=0;
for(int i=lastIndex;count如果((总和[i-1]我看到的第一个问题是,您的程序在请求输入后没有做任何事情。类
Roman
没有在任何地方实例化。我引入了一个扫描仪,并提示了一个请求输入的问题,您能解释一下您的意思吗?我仍然很困惑。
main
函数的最后一行是提示用户f或者输入。一旦那一行显示出来,后面什么也不会发生,因为后面没有代码。你需要实例化你的
Roman
类并调用它的方法。对不起,我是java初学者,实例化类意味着什么?实例化类意味着这样的东西
Roman=new Roman(value);
。我尝试插入它,但它给了我一条错误消息:“非静态变量无法从静态上下文引用”答案已更新。请检查它。如果您从我的答案获得帮助,请将其标记为正确。
import java.util.Scanner;


    public class RomantoInteger {
        public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter an Roman numeral with an equivalent between 0 and 3999:");
        int value = input.nextInt();
        RomantoInteger rn = new RomantoInteger();
        Roman roman = rn.new Roman(""+value); //Roman is subclass of RomantoInteger and it is been called from static method so it should be call like this rn.new Roman(""+value);

        // Here you can call any function of Roman class
        }

        public class Roman {

        private final char[] romanNumerals = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };
        private final int[] romanEquivalent = { 1000, 500, 100, 50, 10, 5, 1 };
        private final String romanNum = "";   
        private int decimalNum; 

        public Roman(String input) {
            romanNum = input.toUpperCase();
        }

        //public void checkRoman(){
        //  for(int x=romanNum.length()-1;x<romanNum.length(); x++){
        //      if(romanNum.charAt(x)=='M')
        //  }
        //}

        /*
         * Method to convert roman number to decimal
         */
        public int convertToDecimal() {
            int sum[];                                     //Create a integer array
            sum = new int[romanNum.length()];               //set the size of sum array to the length of romanNum

            for (int y = 0; y < romanNum.length(); y++) {
                for (int i = 0; i < romanNumerals.length; i++) {
                    if ((romanNum.charAt(y) == romanNumerals[i])       
                            && (romanNum.length() > 1)) {             //and if length of romanNum is greater than 1
                        sum[y] = romanEquivalent[i];                       //store the value of the matching romanNumeral to an indexed variable of array sum
                    }
                    if ((romanNum.charAt(y) == romanNumerals[i])           //if
                            && (romanNum.length() == 1)) {       //and if length of romanNum is just 1
                        System.out.println(romanEquivalent[i]);     //print out the value of the matching romanNumeral
                        break;                                       //terminate looping (for loop)
                    }
                }
            }
            return add(sum);                                            //call method add
        }

        public int add(int[] sum) {
            int lastIndex = sum.length - 1;
            int decimalNum = sum[lastIndex];
            int count = 0;
            for (int i = lastIndex; count < sum.length - 1; count++) {
                if ((sum[i - 1] <= sum[i])) {                 
                    decimalNum = decimalNum - sum[i - 1];
                    --i;
                } else {
                    decimalNum = decimalNum + sum[i - 1];
                    --i;
                }
            }
            return decimalNum;
        }

    }

    }