高手指点啊 关于数组的调用问题

xuxq126 2007-10-23 08:25:55
import java.io.*;
class lianlian
{
int i=0;
int num[];
public void set(int count)
{
String str;
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
for( i=0;i<=count-1;i++)
{
System.out.print("请输入第"+(i+1)+"个学生的成绩:");

try {
str=buf.readLine();
num[i]=Float.parseFloat(str);
}
catch (NumberFormatException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally {}
}

}
public void shuchu(int count){

for( i=0;i<= count-1;i++)
{
System.out.println(+num[i]);
}
}
class mm
{
public static void main(String[] args) throws IOException
{
lianlian num = new lianlian();
num.set(4);
num.shuchu(4);
}
}



怎么调试不过啊 高手指点啊
...全文
97 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hivvyeah 2007-10-24
  • 打赏
  • 举报
回复
--- 高人 给说说为什么 改成 int num[]=new int[10] 就好了啊

因为单是 intnum[]只是声明了个引用的变量, new int[10]才是真正在内存申请了10个放int的位置.
lihaifeng0412 2007-10-24
  • 打赏
  • 举报
回复
java类里边定义的类成员可以不初始化,系统会自动初始化,但类外面定义变量时一定要初始化
jiaozi226 2007-10-24
  • 打赏
  • 举报
回复
num[i]=Float.parseFloat(str);
一方面你的数组没有初始化,另一方面你给int型一个浮点型的值当然是不行了
可以这样:
num = new int[count];

for(){
num[i] = (int)Float.parseFloat(str);
}
hhhhan 2007-10-24
  • 打赏
  • 举报
回复
没有分配内存。4楼正解
intergameover 2007-10-24
  • 打赏
  • 举报
回复
int[] num; //这里只声明了一个变量num,可以指向一个int型的数组,但现在并没有初始化这个数组,所以使用num[0]会出错。
数组使用前必须先初始化,
int[] num = new num[10];
或者
int[] num = {1,3,5};
Maojm 2007-10-24
  • 打赏
  • 举报
回复
也可以在构造函数里分配。
在类 lianlian里加一个构造函数。
public lianlian(int n){
num=new int[n];
}

然后在main里:lianlian num = new lianlian(4);

这样可以达到动态的效果。
xuxq126 2007-10-24
  • 打赏
  • 举报
回复
谢谢 各位了
xierangh 2007-10-23
  • 打赏
  • 举报
回复
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class lianlian
{

private static double num[]=new double[100];
public static void set(int count)
{
String str;
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i <=count-1;i++)
{
System.out.print("请输入第"+(i+1)+"个学生的成绩:");

try {
str=buf.readLine();
num[i]=java.lang.Double.parseDouble(str);
}
catch (NumberFormatException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}finally
{
}
}
}

public void shuchu(int count){

for(int i=0;i<count;i++)
{
System.out.println(+num[i]);
}
}
}
class mm
{
public static void main(String[] args)
{
lianlian num = new lianlian();
num.set(4);
num.shuchu(4);
}
}

xuxq126 2007-10-23
  • 打赏
  • 举报
回复
高人 给说说为什么 改成 intnum[]=new[10] 就好了啊
koj5201314 2007-10-23
  • 打赏
  • 举报
回复
package test;

import java.io.*;

class lianlian {
int i = 0;
int[] num = new int[10];

public void set(int count) {
String str;
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
for (i = 0; i <= count - 1; i++) {
System.out.print("请输入第" + (i + 1) + "个学生的成绩:");
try {
str = buf.readLine();
num[i] = Integer.parseInt(str);
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}

public void shuchu(int count) {
for (i = 0; i <= count - 1; i++) {
System.out.println(i + "的成绩为:" + num[i]);
}
}
}

public class MM {
public static void main(String[] args) {
lianlian num = new lianlian();
num.set(4);
num.shuchu(4);
}
}

62,623

社区成员

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

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