专业的JAVA编程教程与资源

网站首页 > java教程 正文

6.使用Java进行数学计算(java使用while循环计算1到100的和)

temp10 2024-10-11 18:29:08 java教程 17 ℃ 0 评论

进行数学计算

您可以使用常见的数学运算符来执行计算。

1.加法(+)

例如:

6.使用Java进行数学计算(java使用while循环计算1到100的和)

class Demo {
 public static void main(String[] args) {
  int x = 6;
  int y = 3;

  System.out.println(x+y);
 }
}

这将输出两个变量的总和。

结果可以分配给另一个变量,像这样:

class Demo {
 public static void main(String[] args) {
  double price1 = 24.99;
  double price2 = 19.45;

  double sum = price1 + price2;

  System.out.println(sum);
 }
}

以下这个代码的输出是什么?

int a = 3;

int b = 2;

int z = a+b;

System.out.println(z+a);

2.减法(-)

在Java语言中,同样可以使用"-"来表示减法操作。

class Demo {
 public static void main(String[] args) {
  int salary = 10;
  int tax = 5;
  int result = salary - tax;  

  System.out.println(result);
 }
}

3.乘法(*)

在Java语言中,* 操作符用于两个值的乘法运算。让我们使用它来计算给定值的20%是多少:

class Demo {
 public static void main(String[] args) {
  int price = 50;

  System.out.println(price*0.2);
 }
}

0.2代表20%


例如:使用Java语言填空来计算并输出一个9层楼高度的代码。假设每层楼的高度为3米。

int numOfFloors = 9; // 楼层数量
int floorHeight = 3; // 每层楼高度

int buildingHeight = numOfFloors * floorHeight; // 计算总体高度

System.out.println("The height of the building is " + buildingHeight + " meters.");

4.除法(/)

在Java语言中,“/” 运算符用于将一个值除以另一个值。

以下是一个程序示例,用于计算给定金额可以购买多少个给定价格的产品:

class Demo {
 public static void main(String[] args) {
  int amount = 10;
  int price = 2;
  int result = amount/price;

  System.out.print(result);
 }
}

在上面的示例中,由于使用 int 作为数据类型,除法运算的结果将是一个整数。

以下代码的结果是什么?

int x = 15; int y = 4;

int result = x / y;

System.out.println(result);

您可以使用 double 来获取小数点除法的结果。

class Demo {
 public static void main(String[] args) {
  double amount = 9000;
  double price = 49;
  double result = amount/price;

  System.out.print(result);
    //输出:183.6734693877551
 }
}

两个 double 数相除的结果是一个double类型的数。

5.取余操作(%)

取模运算符 % (或取余运算)返回除法的余数。

例如,让我们使用它来找出如果我们将给定的物品放入5个一组的盒子中,剩下多少个物品:

class Demo {
 public static void main(String[] args) {
  int items = 23;
  int res = items % 5; 

  System.out.print(res);
 }
}

result变量中存储了什么值?

int x = 8;

int y = 3;

int result = x % y;

6.字符串(Strings)

Java允许使用 + 运算符将字符串加在一起。结果是字符串的组合。

例如,让我们结合名字和姓氏,得出用户的全名:

class Demo {
 public static void main(String[] args) {
  String firstname = "李";
  String lastname = "四";
  String fullname = firstname+lastname;

  System.out.print(fullname);
 }
}

请注意,结果将在没有任何分隔符的情况下组合给定的字符串。我们可以在它们之间添加一个空格“”:

class Demo {
 public static void main(String[] args) {
  String firstname = "James";
  String lastname = "Smith";
  String fullname = firstname+" "+lastname; 

  System.out.print(fullname);
    //输出:李 四
 }
}

将字符串连接在一起的过程称为字符串拼接(concatenation)。 您还可以将字符串与其他类型(如整数和双精度浮点数)进行拼接。

以下哪个语句将字符串a和b串联?

a%b;
a+b;
a/b;
a-b;

课程要点:

数学很有趣!以下是一些关键要点: 您可以使用基本的 数学运算符 对值和变量进行计算。

  • +表示加法
  • -表示减法
  • / 表示除法
  • % 表示取余运算。
  • 您可以使用 + 运算符对字符串进行拼接,这个过程称为字符串拼接(concatenation)。

思考:国际象棋锦标赛得分

您正在编写一个为国际象棋锦标赛制作的程序,需要计算选手所获得的分数。 胜利值得1分,而平局值得0.5分。 给定的程序声明了两个变量:wins(胜利次数)和ties(平局次数)以及相应的值。

创建一个程序来计算并输出选手所获得的分数。

将平局值乘以0.5,以获得平局获得的积分。

public class Program {
    public static void main(String[] args) {
        int wins = 54;
        int ties = 31;

        
    }
}

思考2:你把鸡蛋装在篮子里,每个篮子里装着8个鸡蛋。所有包装好的篮子都需要装满。

填空创建一个程序,计算剩余的鸡蛋数量。

public class Program {
    public static void main(String[] args) {
        int eggs = 52;
        int count = 8;
        int result = ?  ? count;
        Systme.out.println();
        
    }
}

思考3:酒吧库存

一名酒保卖出了64瓶果汁和23瓶冰茶。总共卖出了多少瓶?

任务:编写代码以计算并输出已售瓶子的总数。

class Demo
{
    public static void main(String[] args) {
        int juice = 64;
        int iceTea= 23;
        //calculate the sum and output it
        System.out.println(juice+iceTea);
            
 }
}

使用 + 运算符来计算总和。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表