问题描述
我必须使用Java
Part1:使用字符原语数据类型输出€188。使用Unicode作为欧元符号€
Part1: Output €188 using the character primitive data type . Use a Unicode for the Euro Symbol €
Part2:将以下char变量'j''o''e'更改为大写JOE并输出结果。
Part2: Change the following char variables ‘j’’o’’e’ to upper case JOE and output the result.
我用过这段代码,我错过了什么吗?
I've used this code, am I missing something?
public class Test27 {
public static void main (String args[]){
System.out.println("\u20ac" +"188");
String changeCase= "joe";
String result;
result=changeCase.toUpperCase();
System.out.println( result);
}
}
干杯
推荐答案
如果问题只是欧元符号变得混乱 - 即程序
If the question is just about the Euro sign getting garbled—that is, the program
import java.io.*;
public class Foo {
public static void main (String args[])
throws Exception
{
System.out.println("\u20ac");
}
}
然后,首先你必须阅读。
Then, first you must read The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!).
然后你需要使Java发出的编码与显示Java输出的东西所期望的编码相匹配。我假设你正在命令行工作。
Then you need to make the encoding that Java sends out match the encoding expected by the thing displaying Java’s output. I’m assuming you’re working at a command line.
-
在Linux上这应该可行。默认情况下,一切都是UTF-8。
On Linux this should just work. Everything is UTF-8 by default.
在Mac上,在Terminal.app中,这不起作用,因为出于某些疯狂的原因,默认的文本编码为Java是没有欧元的。但是Terminal.app完全支持UTF-8。从技术上讲,您可以在终端→首选项→设置→高级→国际中关闭它,但默认情况下它是UTF-8。
On the Mac, in Terminal.app, this won’t work because for some crazy reason the default text encoding for Java is the ancient MacRoman character set which doesn’t have the Euro. But Terminal.app totally supports UTF-8. Technically you can turn that off in Terminal → Preferences → Settings → Advanced → International, but it’s UTF-8 by default.
要设置java使用UTF-8输出,你可以传递命令行参数
To set java to use UTF-8 output, you can pass the command line argument
java -Dfile.encoding=UTF-8 Foo
但这只有在你可以控制你的程序启动时才有效。如果您要发送JAR或 .class
文件供其他人运行,那将无效。您可以通过创建一个将使用不同编码写入 System.out
的对象来自行设置编码:
But that only works if you can control the startup of your program. If you’re sending JARs or .class
files for others to run, that won’t work. You can set up the encoding yourself by creating an object that will write to System.out
with a different encoding:
import java.io.*;
public class Foo {
public static void main (String args[])
throws Exception
{
PrintWriter out = new PrintWriter(
new OutputStreamWriter(System.out, "UTF-8"), true);
out.println("\u20ac");
}
}
只要你记得永远使用你的新 out
用于打印的变量而不是 System.out
。
So long as you remember to always use your new out
variable for printing instead of System.out
.
在Windows上它变得更加疯狂。命令提示符下的默认编码因Windows的不同语言版本而异。在英文版的Windows上,它是。在俄语版Windows上,它是。欧元符号都没有!您可以使用 chcp
命令更改编码,但即使您将其更改为具有欧元符号的编码,默认命令提示字体也没有欧元符号!
On Windows it gets crazier. The default encoding at the command prompt varies between different language versions of Windows. On the English version of Windows, it’s Cp850. On Russian Windows, it’s Cp866. Neither has the Euro symbol! You can change the encoding with the chcp
command, but even if you change it to an encoding that does have the Euro symbol, the default command prompt font doesn’t have the Euro symbol!
您可以从Java检测到您在Windows命令提示符下运行,以编程方式更改编码和字体,然后输出您的字符串,但是 - 这是很多工作。你可能最好只使用上面的代码来强制输出UTF-8,并在你的代码中加入指令(如果要在Windows命令提示符下运行),用户首先需要:
You may be able to detect from Java that you are running at the Windows command prompt, change the encoding and font programmatically, and then output your string, but—that’s a lot of work. You’re probably better off just using the above code to force UTF-8 output, and include instructions with your code that if it is to be run at the Windows command prompt, that the user will first need to:
- 运行
chcp 65001
将命令提示符编码切换为UTF-8 - 通过单击左上角的图标,选择属性,然后转到字体选项卡,将字体切换到Lucida控制台。
- Run
chcp 65001
to switch the command prompt encoding to UTF-8 - Switch the font to Lucida Console by clicking the icon in the upper left corner, selecting Properties, and going to the Font tab.
为了方便您,但为了增加您编写的代码仅在您的计算机上运行的机会,您还可以。
To make things easier for you, but to increase the chances that the code you write will work on your computer only, you can also change the default command prompt code page to UTF-8.
这篇关于使用unicode显示欧元符号并将字符更改为大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!