JAVA四则运算(读写文件)

完成时间:17:10

package 四则运算试题;

import java.io.BufferedReader;
import java.io.PrintStream;
import java.util.Scanner;
import java.io.FileReader;
import java.io.IOException;
public class jisuanti {

	public static void main(String[] args) throws IOException {
		int a1,a2;
		int b;
		int num=0;
		int i=0;
		//Scanner sc=new Scanner(System.in);
        String f[]=new String[4];
        f[0]="+";
        f[1]="-";
        f[2]="*";
        f[3]="/";
        int a[]=new int[100];
        //System.out.println("请选择100以内或1000以内");
        //m=sc.nextInt()+1;
        //System.out.println("请输入要产生的题数");
        //n=sc.nextInt();
        PrintStream out = System.out;
        PrintStream ps = new PrintStream("Test.txt");
        System.setOut(ps);
        for(i=0;i<3;i++)
        { 
        a1=(int)(Math.random()*100);
        a2=(int)(Math.random()*100);
        b=(int)(Math.random()*4);
        System.out.println("第"+(i+1)+"题:"+a1+f[b]+a2+"=");
        System.out.println("*");
        switch(b) {
        case 0:a[i]=(a1+a2);break;
        case 1:a[i]=(a1-a2);break;
        case 2:a[i]=(a1*a2);break;
        case 3:a[i]=(a1/a2);break;
        }
        }
        ps.close();
        
        System.setOut(out);
        System.out.println("题目:");
        BufferedReader br = new BufferedReader(new FileReader("Test.txt"));
        String line = "";
        Scanner sc=new Scanner(System.in);
        int answer[]=new int[100];
        int m=0;
        while((line = br.readLine()) != null){
        		if(line.charAt(0)=='*') {
        			System.out.print("请输入答案:");
        		//	System.out.println(a[m]);
        		    int n;
        		    n=sc.nextInt();
        		    answer[m]=n;
        		    if(answer[m]==a[m])num++;
        		    m++;
        		}
        		else {
        			System.out.println(line);
        		}
        			
        		
		}
        sc.close();
        br.close();
        System.out.println("共答对"+num+"题");
	}
}

  

问题:问题卡在对比结果。

 

转载于:https://www.cnblogs.com/zmh-980509/p/9966327.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用`RandomAccessFile`和`ByteBuffer`来实现文件读写操作。下面是一个示例代码: ```java import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class FileReadWriteExample { public static void main(String[] args) { try { // 打开一个 RandomAccessFile 对象,以读写模式打开文件 RandomAccessFile file = new RandomAccessFile("example.txt", "rw"); FileChannel channel = file.getChannel(); // 创建一个 ByteBuffer,设置容量为 1024 字节 ByteBuffer buffer = ByteBuffer.allocate(1024); // 从文件读取数据到 ByteBuffer int bytesRead = channel.read(buffer); while (bytesRead != -1) { buffer.flip(); // 切换为读模式 while (buffer.hasRemaining()) { System.out.print((char) buffer.get()); // 读取字节并输出 } buffer.clear(); // 清空 ByteBuffer bytesRead = channel.read(buffer); // 继续从文件读取数据 } // 向文件写入数据 String data = "Hello, World!"; buffer.clear(); buffer.put(data.getBytes()); buffer.flip(); // 切换为写模式 channel.write(buffer); // 关闭文件流 file.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的示例,首先创建了一个`RandomAccessFile`对象,并通过`getChannel()`方法获取了`FileChannel`对象。然后,创建了一个`ByteBuffer`对象,并设置了容量为1024字节。接下来,使用`channel.read(buffer)`方法从文件读取数据到`ByteBuffer`,并通过`buffer.flip()`方法切换为读模式。然后,使用`buffer.get()`方法逐个读取字节并输出。当`buffer`没有剩余字节时,使用`buffer.clear()`方法清空`ByteBuffer`。重复执行以上步骤,直到读取到文件末尾。最后,使用`buffer.put(data.getBytes())`方法将数据写入`ByteBuffer`,并通过`buffer.flip()`方法切换为写模式。最后,使用`channel.write(buffer)`方法将数据写入文件。最后,记得关闭文件流。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值