当前位置: 代码迷 >> Java相关 >> 一些很简单的题目
  详细解决方案

一些很简单的题目

热度:201   发布时间:2007-06-29 23:19:21.0
以下是引用渐渐成为高手在2007-6-28 10:22:29的发言:
//1-20的阶乘
class Factorial
{
void fn()
{
int i=1;j=1;
long sum=0;
for(i=1;i<=20;i++)
{
j=*i;
sum=+j;
}
System.out.println(sum);
try
{
if(sum>1000)
System.out.println("Exception");
}
catch(Exception e)
{
System.out.println(sum);
}
}
}
class Test
{
public static void main(String[] args)
{
Factorial f=new Factoral();
f.fn();
}
}

不对
----------------解决方案--------------------------------------------------------

我最痛恨的是什么,就是程序都没在电脑上运行过,看了题目有点头绪就以为自己肯定能写出程序来的人。
学编程语言必须:看书!-> 看程序!-> 写程序!-> 出错要会改程序!
看书懂不代表你就会写```总之你要想掌握这门语言必须学会写代码,不断写```


----------------解决方案--------------------------------------------------------
支持楼上的,看程序跟写程序天壤之别啊。
----------------解决方案--------------------------------------------------------

晕死
我敲了
第三题有点问题!
//利用FileInputStream和FileOutputSteam编写一个文件拷贝的程序
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.*;
import java.nio.channels.FileChannel;
class Copy
{
String filename = null;
String path = "456";
File file =null;
String objFilename = null;
File objfile = null;
FileOutputStream output = null;
FileInputStream input = null;
FileChannel inputchannel = null;
FileChannel outputchannel = null;
File pathfile=null;
File yuanfile = null;
File bojectfile = null;
Copy(String yuan,String obj)
{
pathfile = new File(path);
yuanfile = new File(yuan);
bojectfile = new File(obj);
filename = yuan;
objFilename = obj;
file = new File(path,filename);
objfile = new File(path,objFilename);
judge(pathfile,file);
judge(pathfile,objfile);
read();
write();
}
public void judge(File pathfile,File file) //判确定文件存在
{
if(pathfile.isDirectory()==false)
{
pathfile.mkdirs();
}
if(file.isFile()==false)
{
try
{
file.createNewFile();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public void read() //读文件
{
try
{
input = new FileInputStream(file);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
ByteBuffer buf = ByteBuffer.allocate(200);
inputchannel = input.getChannel();
try
{
inputchannel.read(buf);
}
catch (IOException e)
{
System.out.println(e.toString());
}
buf.flip();
System.out.println("文件内容:");
//System.out.println(buf.get());
try //关闭流
{
input.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void write() //写文件
{
try
{
output = new FileOutputStream(objfile);
}
catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
ByteBuffer buf = ByteBuffer.allocate(200);
outputchannel = output.getChannel();
try
{
inputchannel.write(buf);
}
catch (IOException e)
{
System.out.println(e.toString());
}
buf.flip();
System.out.println("文件写入完成");
try
{
output.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public String toString()
{
return "完成";
}
public static void main(String args[]) //主方法
{
String yuan="123.txt";
String obj="456.txt";
Copy copy = new Copy(yuan,obj);
//System.out.println(copy.toString());
}
}


----------------解决方案--------------------------------------------------------
看看,一个代码就这么长!~~
大家给我看看,结果有点问题!
----------------解决方案--------------------------------------------------------
你不写,永远不会进步
----------------解决方案--------------------------------------------------------
以下是引用千里冰封在2007-6-30 11:51:54的发言:
你不写,永远不会进步

谁说我没写
能帮我看看错哪了吗?!


----------------解决方案--------------------------------------------------------

写了就好,写了别人才好帮你,


----------------解决方案--------------------------------------------------------
你用NIO?

用IO不就可以了
----------------解决方案--------------------------------------------------------
其实我很多都写过
这回是帮别人做
自己写太累了
我写了前4题
要是谁给我点想法也好,写的头都疼
----------------解决方案--------------------------------------------------------
  相关解决方案