Netbeans 11.1 以 Windows 的默认语言打印数字 [英] Netbeans 11.1 print numbers in Windows's default language

查看:64
本文介绍了Netbeans 11.1 以 Windows 的默认语言打印数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在使用 Netbeans 11.1 学习 Java.

我的问题是当我尝试运行一个应该显示数字的程序时,我得到的数字是用阿拉伯语打印的,这是我的 Windows 默认语言.

这是我得到的结果:

我添加了以下行:-J-Duser.language=en -J-Duser.region=USnetbeans.conf 文件,但这并没有解决问题.

建议使用 scanner.useLocale(Locale.ENGLISH); 的另一种解决方案但我不明白如何以及在哪里使用它.这是我的代码:

包教程02;公共类提供的变量OneStatement {公共静态无效主(字符串 [] args){字符串名称 = "Khalid";名称整数年龄 = 24;双 gpa = 3.40;System.out.printf("%s 是 %d 岁.%s 有 %f gpa.\n", name, age, name, gpa);}}

name用英文字母打印没有任何问题,但agegpa用阿拉伯数字打印.输出为:

Khalid 今年 ٢٤ 岁.哈立德有 ٣٫٤٠٠٠٠٠ gpa.

解决方案

要使用西方阿拉伯数字呈现输出中的数字,您只需要在应用程序中适当地显式设置 Locale.

这是对您的程序稍加修改的版本,它首先使用东方阿拉伯数字显示信息,然后使用西方阿拉伯数字显示相同的信息.

package javaapplication18;导入 java.text.NumberFormat;导入 java.util.Locale;公共类 JavaApplication18 {公共静态无效主(字符串 [] args){字符串名称 = "Khalid";整数年龄 = 24;双 gpa = 3.40;Locale arLocale = new Locale("ar");NumberFormat nf = NumberFormat.getInstance(arLocale);System.out.println("国家:" + arLocale.getCountry() + ",语言:" + arLocale.getLanguage());System.out.printf("%s 是 %s 岁.%s 有 %s gpa.\n",姓名,nf.format(年龄),姓名,nf.format(gpa));Locale usLocale = new Locale("us", "EN");nf = NumberFormat.getInstance(usLocale);System.out.println("国家:" + usLocale.getCountry() + ", 语言:" + usLocale.getLanguage());System.out.printf("%s 是 %s 岁.%s 有 %s gpa.\n",姓名,nf.format(年龄),姓名,nf.format(gpa));}}

这是在 NetBeans 的 输出 窗口中显示的内容:

国家: ,语言: ar哈立德 ٢٤ 岁.哈立德有 ٣٫٤ gpa.国家: CN, 语言: us哈立德今年 24 岁.哈立德有 3.4 gpa.

注意事项:

  • 请参阅 Oracle Java 教程,了解有关区域设置.
  • 有关区域设置,请参阅 Javadoc和 NumberFormat 了解更多详情.立>
  • 您还应该能够在 netbeans.conf 中设置区域设置,但我选择以编程方式进行设置以显示动态切换的效果.
  • NetBeans 中的输出 窗口必须配置为使用字体来支持您用于输出的语言.显然,这对您来说不是问题.

I am still learning Java using Netbeans 11.1.

My problem is when I try to run a program that should display numbers, I get the numbers printed in Arabic which is my Windows default language.

This is what I get as a result:

I added the line: -J-Duser.language=en -J-Duser.region=US to the netbeans.conf file, but that did not solve the problem.

Another solution suggested using scanner.useLocale(Locale.ENGLISH); but I could not understand how and where to use it. This is my code:

package lesson02;

public class ProvidedVariablesOneStatement {
    public static void main(String[] args) {
        String name = "Khalid"; name
        int age = 24;
        double gpa = 3.40;
        System.out.printf("%s is %d years old. %s has %f gpa. \n", name, age, name, gpa);
    }
}

The name is printed in English letters without any problem, but the age and gpa are printed in Arabic numbers. The output is:

Khalid is ٢٤ years old. Khalid has ٣٫٤٠٠٠٠٠ gpa.

解决方案

To render the numbers in your output using Western Arabic Numerals you just need to explicitly set the Locale appropriately within your application.

Here is a slightly modified version of your program which first displays the information using Eastern Arabic numerals, and then displays the same information using Western Arabic numerals.

package javaapplication18;

import java.text.NumberFormat;
import java.util.Locale;

public class JavaApplication18 {

    public static void main(String[] args) {

        String name = "Khalid";
        int age = 24;
        double gpa = 3.40;

        Locale arLocale = new Locale("ar");
        NumberFormat nf = NumberFormat.getInstance(arLocale);
        System.out.println("Country: " + arLocale.getCountry() + ", Language: " + arLocale.getLanguage());
        System.out.printf("%s is %s years old. %s has %s gpa. \n",
                name, nf.format(age), name, nf.format(gpa));

        Locale usLocale = new Locale("us", "EN");
        nf = NumberFormat.getInstance(usLocale);
        System.out.println("Country: " + usLocale.getCountry() + ", Language: " + usLocale.getLanguage());        
        System.out.printf("%s is %s years old. %s has %s gpa. \n",
                name, nf.format(age), name, nf.format(gpa));
    }
}

This is what is displayed in the Output window in NetBeans:

Country: , Language: ar
Khalid is ٢٤ years old. Khalid has ٣٫٤ gpa. 
Country: EN, Language: us
Khalid is 24 years old. Khalid has 3.4 gpa. 

Notes:

  • See the Oracle Java Tutorial for an introduction to locales.
  • See the Javadocs for Locale and NumberFormat for more details.
  • You should also be able to set locale within netbeans.conf, but I chose to do it programmatically to show the effect of switching it dynamically.
  • The Output window in NetBeans must be configured to use a font to support the language you are using for output. Obviously this is not an issue for you.

这篇关于Netbeans 11.1 以 Windows 的默认语言打印数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆