English 中文(简体)
我的教 par为什么不工作?
原标题:Why my parsing doesn t work?
  • 时间:2011-08-07 12:59:20
  •  标签:
  • java
  • parsing

I was asked for my homework to make a program wherein the user inputs a Roman numerals between 1-10 and outputs the decimal equivalent. Since I ll be getting a string in the input and an integer in the output, I parsed it, but it won t work. Any ideas why?

import java.util.Scanner ;

class Romans {

    static Scanner s = new Scanner(System.in) ;
    static String val = null ;

    public static void main (String [] args)
    {
        System.out.print ("Enter a roman numeral between I to X: ");
        String val = s.nextLine();

        int e = Integer.parseInt(val);
    }
    static int getRoman (int e)
    {
        if (val = "I"){
            System.out.print ("1") ;
        }else if (val = "II" ){
            System.out.print ("2") ;
        }else if (val = "III") {
            System.out.print ("3") ;
        } else if (val = "IV") {
            System.out.print ("4") ;
        } else if (val = "V"){
            System.out.print ("5");
        } else if (val = "VI") {
            System.out.print ("6");
        } else if (val = "VII") {
            System.out.print ("7");
        } else if (val = "VIII") {
            System.out.print ("8");
        } else if (val = "IX") {
            System.out.print ("9");
        } else if (val = "X") {
            System.out.print ("10") ;
        }
        return val ;
    }
}
问题回答

Two points:

  • = is the assignment operator, not the equality-testing operator (==)
  • You shouldn t use == to test for string equality anyway, as it will only test for reference equality; use equals to test whether two string references refer to equal (but potentially distinct) string objects.

Additionally, you re trying to return a String variable as an int, and you re not even calling getRoman...

我认为,我们可以告诉你,比较“优势”的正确方式是采用平等的方法。

您重新做任务,比较你手头所用的原始类型=,比较 采用平等的方法。

例:

如果(等值)

但是,这种方法也不存在阀门。

你们正试图把val平成一个 in子,而不是它的性质。

对于这种小的果园,它可能最容易简单地建立一个看表,将其编入果园。

你们是否犯过错误?

在你的法典中,你从来不称“老板”。 此外,你还重新使用转让操作人=而不是比较操作者“I”等。

String comparsion should be done with equals(String str) method instead of == comparison. PS. You have = instead of == anyway.

声明如下:

val = "I"

这无疑是而不是你希望在此做的事情。

与双重平等进行比较(=)比较了参考文献,但你不想这样做。

You want to use the equals method.

if (val.equals("I")) ...

在任何地方作出这些改变,看它如何为你们工作。

估计你的主要麻烦来自细致的比较。 在java, = 系指将数值分配给变量, = 旨在比较原始类型和等值,是比较物体,特别是插图的方法。

采用等同办法的一种替代办法是利用联合后勤系统内部的护卫池,在这种情况下,你可以使用=作为比较器。

In your case of parsing roman language numbers, you could also consider using a hashmap to store and retrieve effectively the parsed values of numbers. If you have thousands of comparisons like this to make, then go for identityhashmap.

And last, if you want to do real parsing for all roman numbers, not only the first ones, then you should considering using an automata, i.e. a state machine to parse numbers in a somewhat recursive way, that would be the more efficient model to apply to your problem.

最后两句更面向软件算法,头两句更面向java syntax。 在达到更高层次的优化之前,你应先了解yn。

Regards, Stéphane

除了上述关于您的“优势比较”应如何利用平等(......)职能之外,例如,

if ( val.equals("VII") )

您还须为您称为“SerRoman”的职能提供回报价值。 这项职能被宣布为一种职能,即把愤怒价值回报给打电话者,但在执行你所提供的职能时,没有回归价值(只有系统.out. )。

另外,你也支持正确的参数类型——从你认为的角度来看,你的职能是检查一项努力,看它是否是某种罗马数字。 因此,正确的职能负责人希望这样做:

public static int getRoman(String val)

Also, make sure you are actually calling this function in your main() - from what it looks like right now, you aren t even using the getRoman() function.

希望这一帮助!





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签