JAVA+2复习题库(5)

2019-08-01 22:41

System.out.println(\参数不正确,无法完成复制!正确用法:\System.out.println(\源文件名 目的文件名\System.exit(0); }

copyFile(args[0],args[1]); }

public static void copyFile(String src,String obj){ FileInputStream fis=null; FileOutputStream fos=null; try{

fis=new FileInputStream(src); fos=new FileOutputStream(obj); }catch(FileNotFoundException e){

System.out.println(\文件不存在,请检查您的输入:\}catch(IOException e){ e.printStackTrace(); } try{ int b;

while((b=fis.read())!=-1){ fos.write(b); }

fos.flush();

System.out.println(\文件复制成功!\}catch(IOException e){

System.out.println(\文件写入错误!\} } }

3、求一元二次方程ax2+bx+c=0的根,要求a、b、c从控制台输入。 答案(百度答案)

import java.util.Scanner; public class Compare {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

21

System.out.println(\请输入a\ int a = sc.nextInt();

System.out.println(\请输入b\ int b = sc.nextInt();

System.out.println(\请输入c\ int c = sc.nextInt();

if(b*b - 4*a*c < 0){

System.out.println(\对不起,无解!\ }else if(b*b - 4*a*c == 0){

System.out.println(\此方程解x=\ }else{

System.out.println(\此方程解x1=\b+ \- 4*a*c)) +\+ ((-b+ \√(\- 4*a*c)) +\ } } }

4、求前40个Fibonacci数列。

Fibonacci数列:1,1,2,3,5,8,13,21,34,? 从数列看到:F1=1 (n=1) F2=1 (n=2) Fn=Fn-1+Fn-2 (n≥3) 解: 答案略

public class Fibonacci {

public static void main(String args[]) {

long f1,f2; int i; f1=1; f2=1;

22

System.out.println (f1+\for (i=3;i<=20;i++) { f1=f1+f2; f2=f1+f2;

System.out.println (f1+\ } } }

5、已知公鸡5元1只,母鸡3元一只,小鸡1元3只,要求用100元刚好买100只鸡,问有多少种采购方案。 答案略

public class Loop_Loop3_20

{ public static void main(String args[]) { int I,J,K;

System.out.println(\for (I=0;I<=20;I++ ) // I 为公鸡数 {

for ( J=0;J<=33;J++) // J 为母鸡公鸡数 {

K=100-I-J; // K 为小鸡数 if (5*I+3*J+K/3.0==100)

System.out.println(\ } } } }

6、判断一个字符串是否是回文,例如“rotor“反转之后还是”rotor“,所以是回文。需查阅String类的一些方法。 解:

23

答案略

7、求2-1000内的所有素数 答案略

class prime

{ public static void main(String args[]) { int sum=0,i,j;

for( j=2;j<=1000;j++) //求50以内的素数 { for( i=2;i<=j/2;i++) {if(j%i==0) break; }

if(i>j/2)

{System.out.println(\是素数\ } } } }

8、将如下三组不同类型的数据利用DataInputStream和DataOutputStream写入文件,然后从文件中读出。三组数据如下 { 19.99, 9.99, 15.99,3.99, 4.99 }; { 12, 8, 13, 29, 50 };

{ \T-shirt\\Mug\\Juggling Dolls\\Pin\

解:import Java.io.*; public class DataIOTest {

24

public static void main(String[] args) throws IOException {

//创建数据输出流

DataOutputStream out = new DataOutputStream(new

FileOutputStream(\

//初始化输出数据

double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 }; int[] units = { 12, 8, 13, 29, 50 }; String[] descs = { \ };

//数据输出

for (int i = 0; i < prices.length; i ++) {

out.writeDouble(prices[i]); out.writeChar('\\t'); out.writeInt(units[i]); out.writeChar('\\t');

25

\

\ \ \


JAVA+2复习题库(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:亿以上数的认识教学设计

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: