高分求救,关于byte转换char问题

limop 2007-01-11 12:00:07
package add;

import java.io.*;

/**
* <p>Title: addxy</p>
*
* <p>Description: addxy</p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: BNU</p>
*
* @author 苏君福
* @version 1.0
*/
public class Test_Add {
public Test_Add() {
}
static int ARRAY_MAX_VALUE = 10;
static int ADD_XY_COUNT = 10000;
static int x,y,z;
public static void main(String[] args) throws IOException {
Test_Add test_add = new Test_Add();
for(int i=0;i<ADD_XY_COUNT;i++){
byte[] number1 = new byte[ARRAY_MAX_VALUE];
byte[] number2 = new byte[ARRAY_MAX_VALUE];
char[] c = new char[ARRAY_MAX_VALUE];

/* 读第一个数 */
System.in.read( number1);
c = (char)number1;
System.out.println("number1 = " + c);
x = number1;
System.out.println("x = " + x);

/* 读第二个数 */
System.in.read(number2);
// c = (char)number2;
System.out.println("number2 = " + c);
// y = number2;
System.out.println("y = " + y);

/* 计算X+Y */
AddXY addxy = new AddXY();
z = addxy.AddXY(x, y);
System.out.println(" x + y = " + z);
}

}
}


为什么 c = (char)number1;
x = number1;
这两行会出现类型错误呢?请教高手!
...全文
1483 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
limop 2007-01-11
  • 打赏
  • 举报
回复
能帮我看看错误如何改吗?
c = (char)number1;
x = number1;
就这两行错误!
terry6394 2007-01-11
  • 打赏
  • 举报
回复
看你代码的意思是想从键盘输入字符。
数组是可以这样进行类型转换的。
写了两个类,希望对你有帮助。这是我从键盘获得字符的一般方法:
import java.io.*;
import java.util.Scanner;

class InputTest_1{
public static void main(String[] args) {
try {
int i = System.in.read();

System.out.println("Your Input is: " + (char)i);
} catch(IOException e){
e.printStackTrace();
}
}
}

如果你是用J2SE5.0 那应该用Scanner,这个类更方便
class InputTest_2{
public static void main(String... args) {
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
System.out.println("Your Input is: " + i);
}
}
limop 2007-01-11
  • 打赏
  • 举报
回复
那这句呢? x = number1;
这个怎么处理啊!在线等待!谢谢了,我今晚一定要完成这个任务,多谢大家了!
诗海 2007-01-11
  • 打赏
  • 举报
回复
你的c是和number1都是数组,不能这么赋值.
晨星 2007-01-11
  • 打赏
  • 举报
回复
首先,那两个东西都是数组类型的,而不是char类型的,即使你真想转换,也应该:
//c = (char[])number1;
可惜的是,这种“类型转换”是无法类型的;数组有点像对象类型,不好这么转换的,“byte[]”和“char[]”既不是内建类型,又没有继承关系,怎么可能这样转换呢?

只能一个一个拷贝了:
for(int i = 0; i < number1.length; ++i) {
c[i] = (char)number1[i];
}
商科程序员 2007-01-11
  • 打赏
  • 举报
回复
建议你看看JAVA解惑,里面关于这种问题讲得很详细.

要不留下邮箱, 给你发一个. 用站内联系
lanseliuying 2007-01-11
  • 打赏
  • 举报
回复
number1[i]=(byte)System.in.read( );
c[i] = (char)number1[i];
System.out.println("number1 = " + c);
x = number1[i];
System.out.println("x = " + x);

/* 读第二个数 */
number2[i]=(byte)System.in.read();
c[i] = (char)number2[i];
System.out.println("number2 = " + c);
y = number2[i];
System.out.println("y = " + y);
dracularking 2007-01-11
  • 打赏
  • 举报
回复
1楼似乎已经给出了的说。
servion 2007-01-11
  • 打赏
  • 举报
回复
byte[] number1 = new byte[ARRAY_MAX_VALUE];
byte[] number2 = new byte[ARRAY_MAX_VALUE];
char[] c = new char[ARRAY_MAX_VALUE];

number1 , number2 和 c 都是数组

强制转换 c = (char)number1; 是不可能转换的....

(字符) = (char)其他类型 仅对单一字符进行转换
c[i] = (char)number1[i];
x = number1[i];
这样就可以了.............


j45kp 2007-01-11
  • 打赏
  • 举报
回复
字符不能有低的强制转换为高的

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧