Java 语言中 算术系统 怎么把计算结果写入文件

作者&投稿:南睿 (若有异议请与网页底部的电邮联系)
JAVA语言 算术计算结果写入~

public class Test{
public static void main(String[] args)
{
File file = new File("C:\esult.txt");
FileOutputStream out = null;
try {
if(!file.exists())
{
file.createNewFile();
}
out = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
System.out.println("创建文件失败");
e.printStackTrace();
}
int num1 = 0;
int num2 = 0;
Scanner scanner = new Scanner(System.in);
String oper = null;
while(oper == null)
{
printInfo();
oper = scanner.next();
if("e".equals(oper))
{
System.out.println("程序退出!");
System.exit(1);
}
else
{
try {
save(operator(oper), out);
} catch (IOException e) {
System.out.println("保存出错,程序退出");
oper = null;
}
oper = null;
}
}
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}


static String operator(String oper)
{
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int num1 = random.nextInt(101);
int num2 = random.nextInt(101);
int right = 0;
String answer = null;
String returnStr = null;
boolean isRight = false;
if("/".equals(oper))
{
System.out.print(num1 + " / " + num2 + " = ");
answer = scanner.next();
right = num1 / num2;
isRight = result(answer, right);
returnStr = num1 + "/" + num2 + "=" + answer + "" + right + "" + isRight;
}
else if("+".equals(oper))
{
System.out.print(num1 + " + " + num2 + " = ");
answer = scanner.next();
right = num1 + num2;
isRight = result(answer, right);
returnStr = num1 + "+" + num2 + "=" + answer + "" + right + "" + isRight;
}
else if("-".equals(oper))
{
System.out.print(num1 + " - " + num2 + " = ");
answer = scanner.next();
right = num1 - num2;
isRight = result(answer, right);
returnStr = num1 + "-" + num2 + "=" + answer + "" + right + "" + isRight;
}
else if("*".equals(oper))
{
System.out.print(num1 + " * " + num2 + " = ");
answer = scanner.next();
right = num1 * num2;
isRight = result(answer, right);
returnStr = num1 + "*" + num2 + "=" + answer + "" + right + "" + isRight;
}
else
{
System.out.print("不合法的输入,请重新输入:");
oper = scanner.next();
returnStr = null;
}
return returnStr;
}


private static boolean result(String answer, int rightResult) {
if(String.valueOf(rightResult).equals(answer))
{
System.out.println("正确");
return true;
}
else
{
System.out.println("错误! 正确答案为: " + rightResult);
return false;
}
}


static void printInfo()
{
System.out.println("-----------------------------------------");
System.out.print("运算符:
" +
" + ---加法
" +
" - ---减法
" +
" * ---乘法
" +
" / ---除法
" +
" e ---退出
" +
"请输入运算符:");
}
static void save(String str, OutputStream out) throws IOException
{
out.write((str + "
").getBytes());
}

java虚拟机用C些的,开玩笑= =java需要运行在jvm(java虚拟机上)jvm还需要运行在系统之上比如window,linux,mac都有java虚拟机,不可能编写系统,java不是系统级的语言 java代码产生的是一种中间码 类似VC那种东西 运行的时候由jvm解析然后执行, java不能编译成直接运行的2进制带代码, 楼上 android是linux系统好吧。上面装了个google自己的jvm 然后运行的java写的应用程序而已

try {
File file = new File("D:\\test.txt");
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);

s1 = new String(txtName.getText() + ";" + txtId.getText()
+ ";" + txtAge.getText() + ";" + txtSex.getText()
+ ";"+txtMobile.getText()+";"+txtHomeAddress.getText()+";");
bw.write(s1);
bw.newLine();

bw.flush();
bw.close();
osw.close();
fos.close();

}

catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}

不明白我给你解释,其实百度有的是这类方法

写方便使用FILEWRITE

给你举个例子

字符串[] ARR = {“A”,“B”,“C”};

FILEWRITE FW =的新FILEWRITE(“D :/ a.txt”);

字符串str =“”;

为(int i = 0;我arr.length,我+ +){ STR + = ARR [I] +“”;

}

fw.write(STR);

fw.flush();

fw.close();
>
写入的文件格式(分隔符,或保鲜膜,循环拼接海峡时加上去的,我在这里实现,以逗号分隔)

写一个输出流就好了


江永县17333593892: Java 语言中 算术系统 怎么把计算结果写入文件 -
紫瑞天诺: 写方便使用FILEWRITE 给你举个例子 字符串[] ARR = {“A”,“B”,“C”};FILEWRITE FW =的新FILEWRITE(“D :/ a.txt”); 字符串str =“”; 为(int i = 0;我arr.length,我+ +){ STR + = ARR [I] +“”; }fw.write(STR);fw.flush();fw.close();> 写入的文件格式(分隔符,或保鲜膜,循环拼接海峡时加上去的,我在这里实现,以逗号分隔)

江永县17333593892: 用Java语句 实现 算术练习题生成系统 -
紫瑞天诺: package aas; //主菜单 //请输入加减乘除运算符 // + ----加法 // - ----减法 // * ----乘法 // / ----除法 // e ----退出 //功能要求 // 1.生成算术表达式如下 // 23+3=? // 2.验证模块 // 输出 "正确" "错误" import java.util.Random; import java.util.Scanner; public...

江永县17333593892: 用Java语句 实现 算术练习题生成系统 -
紫瑞天诺: package aas;//主菜单//请输入加减乘除运算符/加法/减法/乘法/除法// ...

江永县17333593892: java中算术运算 -
紫瑞天诺: 10进制数(转)计算机中2进制数: 正数取原码; 负数按位取反末尾加1.-8: 8用二进制表示:1000;按位取反:0111;1000:末尾加1. -5: 5用二进制表示:0101;按位取反:1010;1011:末尾加1.将以上补码相加:1000 + 1011 --------------------10011计算机中2进制转10进制: 以0开头的,直接将2进制转换为10进制 以1开头的,需四部,按位取反,末尾加1,转换为10进制,加负号;10011: 按位取反:01100;末尾加1:01101;转换为10进制:13;加负号:-13够详细了吧!

江永县17333593892: 用java编写程序 课题:算术运算测试 -
紫瑞天诺: 你好,按照你的要求代码如下,可以直接运行import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;public class test { public static void main(String[] args) { int count = 0;BufferedReader in = new ...

江永县17333593892: Java语言中的"+"怎么用呢在输出语句中System.out -
紫瑞天诺: Java语言中的"+"在输出语句中System.out的用法如下: System.out.println()是可以直接打印对象类型的,默认调用Object类的toString()方法. String firstName = "Jeff ", String lastName = "Bosch"; System.out.print( firstName +" "+ lastName ); 打印结果:Jeff Bosch +表示连接两个字符串,或者连接一个常量加一个变量的值. 比如: int a = 100; System.out.print( "当前的数量是:"+ a); 结果是:当前的数量是:100

江永县17333593892: java编程的思路 -
紫瑞天诺: java基本编程思路:第一步:写框架(内层和外层):public class 类名{//外层 public static void main(String[ ] args){//内层 } } 第二步:看题目中是否有“接收”“输入”“录入”等字眼,则导入util包,创建Scanner扫描仪对象:import java.util....

江永县17333593892: 九九乘法表怎么用JAVA语言编写 -
紫瑞天诺: 如果把九九乘法表中如“1*1=1”等式全部看作一个个整体的话,九九乘法表可看作一个直角三角形,实现直角三角形可用两个for循环嵌套来实现,那么我们最后输出应为System.out.print(变量1+"*"+变量2+"="+(变量1*变量2)+" ")...

江永县17333593892: 用java语言从键盘输入一个算术表达式在控制台打印算出结果 -
紫瑞天诺: 朋友,我来告诉你答案!这个要用到堆栈数据结构先将表达式转换成后缀式,然后求值请参考,堆栈应用,表达式求值

江永县17333593892: 想用java实现一个程序,可以把字符串转化为算数表达式. -
紫瑞天诺: 空异常,估计是你operator数组那里使用了还没赋值的值.你要确保你operator使用都是不为null的值,或者先判断一下是否为null在操作,20行那里

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网