如何使用Ä,Ö& Å在java输入?

时间:2013-08-17 14:15:17

标签: java equals

因此。我想知道我怎么能这样做

  1. 系统打印包含a-z +å,ä和ö字母的单词。 (目前å,ä和ö以奇怪的方式印刷。我很确定你知道它的样子:D)
  2. 用户输入一个单词并将其与第一个单词进行比较。目前,如果上面的单词^包含ä,ö或å,我输入该单词..它将看不到那些2之间的匹配..
  3. 所以问题是:我怎样才能做到这一点,如果你把å,ä或ö输入它会注意到它刚刚打印出来的那个å,ä,ö完全一样?我正在使用

    answer.equals(rightanswer)

    这是我的全部代码:D主要是任务和答案:)

    import java.io.*;
    import java.awt.*;
    public class sanaopisto {
    public static int quanity;
    public static String rightanswer;
    public static String question;
    public static int right;
    public static int wrong;
    public static double ratio;
    public static void main(String[] args) {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    try{
    System.out.print("Moneenko sanaan tahdot vastata? ");
    quanity = Integer.parseInt(in.readLine());
    for(int x=0; x<quanity; x++){
    System.out.println(x+1 +". kysymys");
    getquestion(quanity);
    }
    System.out.println("Oikeita vastauksia " +right +" ja v\u201e\u201eri\u201e " +wrong +".");
    }catch(Exception e) {
            System.out.println("Tapahtui virhe.");}}
     public static void getquestion(int quanity) {
     try{
     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
     int[] done = new int[100];//create array but everything is null
    for(int i = 0; i<done.length; i++)
    {
    done[i] = 0;//need default values else wise it'll just be NULL!!!
    }
    //must be done before the do-while loop starts
     boolean allDone = false;
     String answer;
     int ran;
    if (!areAllQuestionsComplete(done)){ //Changed (!areAllQuestionsComplete(done)) thingy like this..
    do{ //And made this work properly etc.
    ran = (int)(Math.random() * 53 + 1);
    } while (done[ran] == 1);
    
    if(done[ran] != 1)
    {
        //ask random question
        //if answer is correct, set done[ran] = 1
        //else just let do-while loop run
        if (ran == 1) { //1
        question = "ruotsalainen";
        rightanswer = "svensk, -t, -a";}
        if (ran == 2) { //2
        question = "suomalainen";
        rightanswer = "finländsk, -t, -a";}
        //.
        //. Took some code away from here.. Because too many questions.. In real version I have all the 1-84 questions :D
        //.
        if (ran == 83) { //15
        question = "globalisoitunut";
        rightanswer = "globaliserad, -at, -ade";}
        if (ran == 84) { //15
        question = "maailma";
        rightanswer = "en värld, -en, -ar, -arna";}
        }
        System.out.println(question);
        System.out.print("Vastaus?: ");
        answer = in.readLine();
        if (answer.equals(rightanswer)){           
        right++;
        System.out.println("Oikein!\n");
        done[ran] = 1;}
        else{wrong++;
        System.out.println("Oikea vastaus on: " +rightanswer +"\n");}
        //check if all questions are answered} 
    else { 
    System.out.println("You have answered every question!"); //I know that this is useless.. :D
    }
        }catch(Exception e) {
    System.out.println("You made a mistake.");}
    }
     private static boolean areAllQuestionsComplete(int[] list)
    {
    for(int i = 0; i<list.length; i++)
    {
        if(list[i] != 1)
        {
            return false;//found one false, then all false
        }
    }
    return true;//if it makes it here, then you know its all done
    }
    }
    

    编辑添加完整代码'将一些问题带走'我正在使用 CMD

2 个答案:

答案 0 :(得分:0)

UTF-8应该可以让你使用它们。

你是芬兰人吗? :)

如果您尝试:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));

如何为你工作?

编辑: 不知怎的,应该工作的UTF-8似乎没有做到这一点。我尝试使用-Dfile.encoding=UTF8作为JVM属性,但对我不起作用 所以我基本上尝试了所有可用的字符集,其中很少有正确的字符,这里是charset名称: x-ISO-2022-CN-GB,x-ISO-2022-CN-CNS,x-IBM922,windows-1258,windows-1254,windows-1252, ISO-8859-9,ISO-8859-4,ISO-8859-1,ISO-2022-KR和ISO-2022-CN

因此,如果您尝试例如:

BufferedReader in = new BufferedReader(new InputStreamReader(System.in, "x-ISO-2022-CN-GB"));

应该有效

答案 1 :(得分:0)

我猜你正在使用System.out和System.in,它们使用系统默认编码。

这是Windows命令行中的某种DOS编码,具体取决于您的计算机设置。

因此,要允许读取和打印任何类型的Unicode字符(如äöü等),您必须更改命令行编码(例如告诉DOS使用不同的编码)和java使用相同的编码

要正确回答如何完成此操作,需要有关操作系统的更多信息。

在java端你可以使用InputStreamReader并将字符集(编码)赋予它的构造函数来读取和PrintStream(也给出相同的编码)来编写。