「java并运算」java实现算术运算

博主:adminadmin 2022-11-22 02:09:05 57

今天给各位分享java并运算的知识,其中也会对java实现算术运算进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java string变double 并运算 求大神帮修正 图一是我自己写的 图二是助教给的正确答案 。

for循环里的第一个if判断如果不满足,你直接就break出循环了,这里肯定不对啊

你输入3.6+4.4,当遍历到+的时候,发现不是数字,就不继续往下判断了

import java.util.Scanner;

public class Calculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

String inputString = scanner.next();

String num1 = "", num2 = "";

char opt = 0;

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

if (inputString.charAt(i) == '+'

|| inputString.charAt(i) == '-'

|| inputString.charAt(i) == '*'

|| inputString.charAt(i) == '/') {

// 遍历字符串,当读取到字符+,-,*,/中的任何一个,将运算符opt赋值为这个字符,并按照这个字符所在的位置i来分割整个字符串

// 从开头到位置i的内内容赋值给 计算数1, 从i往后的内容赋值给 计算数2

opt = inputString.charAt(i);

num1 = inputString.substring(0, i);

num2 = inputString.substring(i + 1);

}

}

double result = 0;

switch (opt) {

case '+':

result = Double.parseDouble(num1) + Double.parseDouble(num2);

break;

case '-':

result = Double.parseDouble(num1) - Double.parseDouble(num2);

break;

case '*':

result = Double.parseDouble(num1) * Double.parseDouble(num2);

break;

case '/':

result = Double.parseDouble(num1) / Double.parseDouble(num2);

break;

}

System.out.println(num1 + opt + num2 + "=" + result);

}

}

「java并运算」java实现算术运算

如何得到java文本框中的数字并加以运算

得到Java文本中的数值并加以运算的方法分两种情况:

1、在Java中获取程序里面定义的变量的值,用对象.属性名调用即可;

2、调用外部文件里面存储的数据,则需要用到文件操作对象File。

用FileReader或者FileInputStream()来读取文件内容,然后分别做运算即可。

Java编写一个矩形类,并计算面积和周长?

class Rectangle{

private int width = 2;

private int length = 1;

public int getWidth(){

return this.width;

}

public void setWidth(int w){

this.width = w;

}

public int getLength(){

return this.length;

}

public void setLength(int l){

this.length = l;

}

public int getArea(){

return this.length * this.width;

}

public int getCircumference(){

return (this.length + this.width) * 2;

}

public Rectangle(){}

public Rectangle(int l, int w){

this.length = l;

this.width = w;

}

}

public class demo{

public static void main(String[] args) {

Rectangle rect = new Rectangle(30, 8);

System.out.print("长方形的面积是:");

System.out.println(rect.getArea());

System.out.printf("长方形的周长是:%d\n", rect.getCircumference());

}

}

如何利用Java应用程序编写要求输入两个整数并计算两个数据之和?

利用Java应用程序编写要求输入两个整数并计算两个数据之和具体操作步骤如下:

import java.util.Scanner

public class Test {

public static void main(String[] args) {

System.out.print("请输入第一个整数:")

Scanner s=new Scanner(System.in)

int a=s.nextInt()

System.out.print("请输入第二个整数:")

int b=s.nextInt()

System.out.println(a+b)

}

}

java并运算的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java实现算术运算、java并运算的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。