求大神指教!!!
在进行Java学习时要写一个实现计算器功能的代码,由于没有学习到可视化so…
计算器代码1.0
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;
public class Mathwork {
public static void main(String[] args) {
System.out.println("请进行您的运算:(以数字、运算符之间以空格隔开)");
Scanner sc=new Scanner(System.in);//键入第一个参数
String s=sc.next();//将第一个参数赋予s
double s1=Double.valueOf(s);//进行字符串转化为double
Scanner ac=new Scanner(System.in);//键入第二个参数
String a=sc.next();//将参数输入字符串a
Scanner bc=new Scanner(System.in);//键入第三个参数
String b=sc.next();//将参数赋值字符串b
double b1=Double.valueOf(b);//字符串转化为double
sc.close();//结束输入
switch (a){
case "+":
System.out.println(add(s1,b1));//若第二个参数为+则返回s1+b1
break;
case "-":
System.out.println(sub(s1,b1));//若第二个参数为-则返回s1-b1
break;
case "*":
System.out.println(mul(s1,b1));
break;
case "/":
System.out.println(div(s1,b1));
break;
default:
System.out.println("您输入的运算符不合法!");
}
}
public static double add(double a,double b){
return a+b;
}
public static double sub(double a,double b){
return a-b;
}
public static double mul(double a,double b){
return a*b;
}
public static double div(double a,double b){
return a/b;
}
}
实现了一个两个数据之间的运算,还必须在每次输入后以空格或回车结尾,所以进行了改进。
计算器代码2.0
import java.util.Scanner;
public class Mathwork2 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
for (int i = 0; i < str.length(); i++) {
char chr=str.charAt(i);//循环提取字符串中的字符
if('+'==chr){//判断如果为+则进行
double a=Double.valueOf(str.substring(0,str.indexOf("+")));
//将字符串中从头到运算符的字符变成double赋值给a
double b=Double.valueOf(str.substring(str.indexOf("+")+1,str.length()));
//将字符串中从运算符到结束的字符串double并赋值给b
System.out.println(add(a,b));
} else if('-'==chr){
double a=Double.valueOf(str.substring(0,str.indexOf("-")));
// String b= str.substring(str.indexOf("-")+1,str.length());
// String b= str.substring(str.indexOf("-")+1),;
double b=Double.valueOf(str.substring(str.indexOf("-")+1,str.length()));
System.out.println(sub(a,b));
}else if ('*'==chr){
double a=Double.valueOf(str.substring(0,str.indexOf("*")));
// String b= str.substring(str.indexOf("*")+1,str.length());
// String b= str.substring(str.indexOf("*")+1),;
double b=Double.valueOf(str.substring(str.indexOf("*")+1,str.length()));
System.out.println(mul(a,b));
}else if ('/'==chr){
double a=Double.valueOf(str.substring(0,str.indexOf("/")));
// String b= str.substring(str.indexOf("/")+1,str.length());
// String b= str.substring(str.indexOf("/")+1),;
double b=Double.valueOf(str.substring(str.indexOf("/")+1,str.length()));
System.out.println(div(a,b));
}
}
}
public static double add(double a,double b){
return a+b;
}
public static double sub(double a,double b){
return a-b;
}
public static double mul(double a,double b){
return a*b;
}
public static double div(double a,double b){
return a/b;
}
}
实现了两个数据之间的加减乘除运算,但是还是不能进行多个数据间的操作,所以进行了改进。
计算机代码3.0
在3.0 中发现了一处bug无法改正,如果有大佬的话希望能提出些修改意见,下面是我的思路:
1.对键入的字符串进行挨个字符的提取(str.charAt(i))。
2.对该字符是否为运算符做判别,若为运算符,则count+1,并且将该运算符的位置赋值给数组arr[]。
3.创建一个数组int b[]=new b[count+1],数组长度为运算符个数加一。for (i=0;i<count ;count++),分别将第一个运算符位置前的字符、第一个到第二个运算符间的…一直到最后一个运算符到整个字符串长度的字符串分别放入数组b中;
4.从第一个运算符一直往后循环运算(运算后将值先放入一个临时变量中进行下一次的运算符运算)
import java.util.Scanner;
public class Mathwork3 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
// String sc1=sc.nextLine();
String sc1="9*9*9*9*";
int count=0;
int place=0;
int[] arr= new int[100];
arr[0]=0;
for (int i = 0; i < sc1.length(); i++) {
char chr=sc1.charAt(i);
if(chr=='*'||chr=='+'||chr=='-'||chr=='/'){
if (count==0){
count=1;
}
arr[count]=sc1.indexOf(chr,arr[count-1]+1);
count++;
}else {
continue;
}
}System.out.println(count);
for (int i = 0; i < count; i++) {
System.out.println("第"+(i+1)+"个位置是"+arr[i]);
}
}
public static double add(double a,double b){
return a+b;
}
public static double sub(double a,double b){
return a-b;
}
public static double mul(double a,double b){
return a*b;
}
public static double div(double a,double b){
return a/b;
}
}
但进行到提取运算符位置的时候,就不行了,一旦前一个运算符与一个运算符相同的话,就一直会输出前一个运算符的位置,因为indexof提取从一开始到当前第一个的位置,所以我将第一个运算符位置加入indexof代码中,表示在第一个运算符的后面出现的第一个运算符的位置,记录到数组的下一个元素中,结果输出时还是一直是第一个运算符位置,为什么啊,求大神指教。。。。。。
Thanks vary much…
更新代码(10/19)
import java.util.Scanner;
public class Mathwork3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String sc1 = sc.nextLine();
int count = 0;
int[] arr = new int[100];
arr[0] = 0;
int place1 = 0;
loop:
for (int i = 0; i < sc1.length(); i++) {
char chr = sc1.charAt(i);
if (chr == '1' || chr == '2' || chr == '3' || chr == '4' || chr == '5' || chr == '6' || chr == '7' || chr == '8' || chr == '9' || chr == '0' || chr == '+' || chr == '-' || chr == '*' || chr == '/') {
if (chr == '*' || chr == '+' || chr == '-' || chr == '/') {
arr[count] = sc1.indexOf(chr, place1 + 1);
place1 = arr[count];
count++;
}
} else {
System.out.println("输入非法字符!");
break loop;
}
}
double[] brr = new double[count + 1];
double[] crr = new double[count];
brr[0] = Double.valueOf(sc1.substring(0, arr[0]));
for (int i = 1; i < count; i++) {
brr[i] = Double.valueOf(sc1.substring((arr[i])+1, arr[i+1])+1);
}
brr[count + 1] = Double.valueOf(sc1.substring((arr[count])+1 , sc1.length()+1));
if (count == 1) {
if (sc1.indexOf(arr[0])=='+'){
crr[0]=add(brr[0],brr[1]);
}
if (sc1.indexOf(arr[0])=='-'){
crr[0]=sub(brr[0],brr[1]);
}
if (sc1.indexOf(arr[0])=='*'){
crr[0]=mul(brr[0],brr[1]);
}
if (sc1.indexOf(arr[0])=='/'){
crr[0]=div(brr[0],brr[1]);
}
}else
for (int i = 1; i < count; i++) {
if (sc1.indexOf(arr[i])=='+'){
brr[i]=crr[i-1];
crr[i]=add(brr[i],brr[i+1]);
}
if (sc1.indexOf(arr[i])=='-'){
brr[i]=crr[i-1];
crr[i]=sub(brr[i],brr[i+1]);
}
if (sc1.indexOf(arr[i])=='*'){
brr[i]=crr[i-1];
crr[i]=mul(brr[i],brr[i+1]);
}
if (sc1.indexOf(arr[i])=='/'){
brr[i]=crr[i-1];
crr[i]=div(brr[i],brr[i+1]);
}
}
System.out.println(crr[count]);
}
public static double add(double a,double b){
return a+b;
}
public static double sub(double a,double b){
return a-b;
}
public static double mul(double a,double b){
return a*b;
}
public static double div(double a,double b){
return a/b;
}
}
new problem:
8*8*8
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 4, end 0, length 5
at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3734)
at java.base/java.lang.String.substring(String.java:1903)
at Mathwork3.main(Mathwork3.java:30)
Process finished with exit code 1
记录一下。。
日常更新加学习
import java.util.Scanner;
public class Mathwork3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String sc1 = sc.nextLine();
int count = 0;
int[] arr = new int[100];
arr[0] = 0;
int place1 = 0;
loop:
for (int i = 0; i < sc1.length(); i++) {
char chr = sc1.charAt(i);
if (chr == '1' || chr == '2' || chr == '3' || chr == '4' || chr == '5' || chr == '6' || chr == '7' || chr == '8' || chr == '9' || chr == '0' || chr == '+' || chr == '-' || chr == '*' || chr == '/') {
if (chr == '*' || chr == '+' || chr == '-' || chr == '/') {
arr[count] = sc1.indexOf(chr, place1 + 1);
place1 = arr[count];
count++;
}
} else {
System.out.println("输入非法字符!");
break loop;
}
}
System.out.println(count);
double[] brr = new double[count + 2];//brr个数为0到count+1,所以应该为count+2个
double[] crr = new double[count+1];
brr[0] = Double.valueOf(sc1.substring(0, arr[0]));
for (int i = 1; i < count; i++) {
brr[i] = Double.valueOf(sc1.substring((arr[i - 1]) + 1, arr[i]));
}
brr[count + 1] = Double.valueOf(sc1.substring(sc1.length()-1));
if (count == 1) {
if (sc1.indexOf(arr[0]) == '+') {
crr[0] = add(brr[0], brr[1]);
}
if (sc1.indexOf(arr[0]) == '-') {
crr[0] = sub(brr[0], brr[1]);
}
if (sc1.indexOf(arr[0]) == '*') {
crr[0] = mul(brr[0], brr[1]);
}
if (sc1.indexOf(arr[0]) == '/') {
crr[0] = div(brr[0], brr[1]);
}
}
else {
for (int i = 1; i < count; i++) {
if (sc1.charAt(arr[i]) == '+') {
brr[i] = crr[i - 1];
crr[i] = add(brr[i], brr[i + 1]);
}
if (sc1.charAt(arr[i]) == '-') {
brr[i] = crr[i - 1];
crr[i] = sub(brr[i], brr[i + 1]);
}
if (sc1.charAt(arr[i]) == '*') {
brr[i] = crr[i - 1];
crr[i] = mul(brr[i], brr[i + 1]);
}
if (sc1.charAt(arr[i]) == '/') {
brr[i] = crr[i - 1];
crr[i] = div(brr[i], brr[i + 1]);
}
}
}
System.out.println(crr[count-1]);
}
public static double add(double a, double b) {
return a + b;
}
public static double sub(double a, double b) {
return a - b;
}
public static double mul(double a, double b) {
return a * b;
}
public static double div(double a, double b) {
return a / b;
}
}
以下test代码
import java.util.Scanner;
public class test {
public static void main(String[] args) {
String a="200";
String b="2"+new String("00");
System.out.println(a==b);
String a1="12345" ;
String a2=a1.substring(0+1,0+2);
System.out.println(a2);
/* Scanner sc=new Scanner(System.in);
String sc1=sc.nextLine();
double arr[]={1,2};
String a3=sc1.substring((int)arr[0],(int)arr[1]+1);//substring中的起始要用int型
System.out.println(a3);
*/
int arr[]={4};
System.out.println(a1.substring(arr[0]));
}
}
import java.util.Scanner;
public class test2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String sc1 = sc.nextLine();
int count = 0;
int[] arr = new int[100];
arr[0] = 0;
int place1 = 0;
loop:
for (int i = 0; i < sc1.length(); i++) {
char chr = sc1.charAt(i);
if (chr == '1' || chr == '2' || chr == '3' || chr == '4' || chr == '5' || chr == '6' || chr == '7' || chr == '8' || chr == '9' || chr == '0' || chr == '+' || chr == '-' || chr == '*' || chr == '/') {
if (chr == '*' || chr == '+' || chr == '-' || chr == '/') {
arr[count] = sc1.indexOf(chr, place1 + 1);
place1 = arr[count];
count++;
}
} else {
System.out.println("输入非法字符!");
break loop;
}
}
System.out.println(count);
for (int i = 0; i < count; i++) {
System.out.println(arr[i]);
}
}
}
总结,定义数组长度要准确。差一个计算实现,赋值数组方面没问题了,先学习新课程了。。。溜溜球