英文:
Explain why the Wrong Output Happened?
问题
import java.util.Scanner;
public class ArrayIntro
{
public static void main(String[] args)
{
int[] a;
a = new int[10];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 10; i++)
{
a[i] = sc.nextInt();
}
for (int e : a)
{
System.out.print(e + 32 + ' ');
}
}
}
Input: 1 2 3 4 5 6 7 8 9 10
Output: 33343536373839404142
英文:
public class ArrayIntro
{
public static void main(String[] args)
{
int[] a;
a=new int[10];
Scanner sc =new Scanner(System.in);
for(int i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
for(int e:a)
{
System.out.print(e+' ');
}
}
}
Input:1 2 3 4 5 6 7 8 9 10<br>
Output:33343536373839404142
Its mean it added 32 to each number
答案1
得分: 1
只需尝试以下代码。
由于您在循环内部打印sout
,它会打印数字和空格(' '
)的总和。空格的ASCII值为32,因此您能够看到每个元素都增加了32的值。
有关ASCII或Unicode,您可以参考此链接,它会对您有所帮助。
如果您像这样放置一些内容 System.out.print(new Integer(' '));
,它将打印32。
如果您只想添加空格,那么请使用双引号。
System.out.print(e+" ");
。
单引号被视为字符,并且与整数相加会将它们的ASCII代码相加。
public static void main(String[] args)
{
int[] a;
a=new int[10];
Scanner sc =new Scanner(System.in);
for(int i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
for(int e:a)
{
System.out.print(e+" ");
}
}
英文:
Just try with below code.
As you are printing sout inside for loop, it's printing sum of number and Space( ' '
). And space have ASCII value of 32, so you are able to see every element with added value of 32.
For ASCII Or Unicode you can refer this link, it will help you.
Simply you put something like this System.out.print(new Integer(' '));
, it will print 32.
If you want to add space only then go with double quote.
System.out.print(e+" ");
.
Single quote consider as character and charater added with Integer will summing up with their ASCII code.
public static void main(String[] args)
{
int[] a;
a=new int[10];
Scanner sc =new Scanner(System.in);
for(int i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
for(int e:a)
{
System.out.print(e+" ");
}
}
答案2
得分: 1
To put it simply, ' ' is a character in ASCII and the value of it is 32. So, as far as I know, you might want it the just print directly, so you can just replace ' ' with " ". Always remember that single quotes are for characters and double quotes are for strings.
For your answer, you can just do this: System.out.print(e+" ");
Instead of this: System.out.print(e+' ');
英文:
To put it simply, ' ' is a character in ASCII and the value of it is 32. So, as far as I know, you might want it the just print directly so you can just replace ' ' with " ". Always remember that single quotes are for characters and double quotes are for strings.
For your answer you can just do this:System.out.print(e+" ");
Instead of this: System.out.print(e+' ');
答案3
得分: 0
简而言之,要修复你的程序,你需要使用 `" "`(带有双引号),而不是 `' '`。
问题在于,在Java中,`+` 运算符有两个不同的含义:
- 如果操作数中的任何一个是 `String`,那么 `+` 表示“将这两个操作数连接在一起”。如果另一个操作数不是 `String`,那么它必须以某种方式进行转换,这涉及一些规则。
- 如果没有操作数是 `String`,那么 `+` 表示“将这两个操作数相加” - 也就是说,它们被视为数字。它们可以作为 `double` 值、`float` 值、`long` 值或 `int` 值相加,具体取决于数据类型。但如果它们是 `short`、`byte` 或 `char`,它们将被视为 `int`。这称为二进制数值提升,在Java语言规范中有详细解释。
在Java中,单引号用于界定 `char` 字面值。因此,当你写下 `e + ' '` 时,你正在将一个 `int` 添加到一个 `char` 上。这是加法运算 - `char` 被视为 `int`,操作数被作为数字相加。与本页上的其他答案相反,` ` 对应的数字是32,因为这是Java使用的字符编码中的数值,类似于UTF-16。与一些错误答案不同,它不是ASCII码。
双引号用于界定 `String` 字面值。因此,如果你写下 `e + " "`,操作数是一个 `int` 和一个 `String`。这意味着你不再进行加法,而是进行字符串连接。`int` 首先被转换为 `String`,然后与 `" "` 连接在一起,这将给出你预期的结果。
英文:
In short, to fix your program, you need to use " "
(with double quotes) instead of ' '
.
The issue is that the +
operator has two different meanings in Java.
- If either of the operands is a
String
, then+
means "concatenate these two operands together". If the other operand is not aString
, then it will have to be converted somehow, and there are some rules around how to do this. - If neither operand is a
String
, then+
means "add these two operands together" - that is, they are treated as numbers. They can be added asdouble
values,float
values,long
values orint
values, depending on the data types. But if they areshort
,byte
orchar
, they will be treated asint
. This is called binary numeric promotion, and is explained in detail in the Java Language Specification.
Now in Java, single quotes are used to delimit a char
literal. So when you write e + ' '
, you're adding an int
to a char
. This is addition - the char
is treated as an int
, and the operands are added as numbers. The number corresponding to
is 32, since that's the numeric value in the character encoding that Java uses, which is something like UTF-16. Contrary to some other answers on this page, it's not ASCII.
Double quotes are used to delimit a String
literal. So if you write e + " "
, the operands are an int
and a String
. That means you're not adding any more, but concatenating. The int
is converted to a String
first, then concatenated to " "
, which will give you the result you're expecting.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论