用java定义两个变量相乘输出 比如:a=100,b=10
时间: 2024-02-23 13:02:12 浏览: 22
可以使用如下Java代码定义两个变量并相乘输出结果:
```java
public class Main {
public static void main(String[] args) {
int a = 100;
int b = 10;
int c = a * b;
System.out.println(c); // 输出结果为 1000
}
}
```
首先定义了两个整型变量 `a` 和 `b` 并分别赋值为 100 和 10,然后将它们相乘并将结果赋值给整型变量 `c`,最后使用 `System.out.println()` 函数输出变量 `c` 的值,即得到了两个变量相乘的结果。
相关问题
用java写一个矩阵相乘
下面是一个简单的Java程序,用于计算两个矩阵的乘积:
```java
public class MatrixMultiplication {
public static void main(String[] args) {
int[][] a = { {1, 2, 3}, {4, 5, 6} };
int[][] b = { {7, 8}, {9, 10}, {11, 12} };
int[][] c = multiply(a, b);
printMatrix(c);
}
public static int[][] multiply(int[][] a, int[][] b) {
int m = a.length;
int n = b[0].length;
int[][] c = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int sum = 0;
for (int k = 0; k < b.length; k++) {
sum += a[i][k] * b[k][j];
}
c[i][j] = sum;
}
}
return c;
}
public static void printMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
```
该程序首先定义了两个矩阵a和b,然后调用multiply()方法计算它们的乘积,并将结果存储在矩阵c中。最后,程序调用printMatrix()方法打印出矩阵c。
在multiply()方法中,我们首先确定了结果矩阵c的大小,然后使用三重嵌套循环计算每个元素的值。对于每个元素c[i][j],我们计算矩阵a的第i行与矩阵b的第j列之间的乘积,并将结果累加到sum变量中。最后,我们将sum的值赋给c[i][j]。
在printMatrix()方法中,我们使用两重循环遍历矩阵并打印每个元素的值。
用Java创建一个Fraction类执行分数运算。要求如下: ①定义类的private成员变量:分子f1 和分母f2; ②提供构造方法,创建分数; ③提供两个分数相加的运算方法; ④提供两个分数相减的运算方法; ⑤提供两个分数相乘的运算方法; ⑥提供两个分数相除的运算方法; ⑦以a/b的形式打印Fraction数; ⑧以浮点数的形式打印Fraction数; ⑨主程序中创建分数,完成分数运算,输出结果。
以下是一个简单的Fraction类实现:
```java
public class Fraction {
private int f1; // 分子
private int f2; // 分母
public Fraction(int f1, int f2) {
this.f1 = f1;
this.f2 = f2;
}
// 求最大公约数
private int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
// 将分数进行约分
private void simplify() {
int gcd = gcd(f1, f2);
f1 /= gcd;
f2 /= gcd;
}
// 分数相加
public Fraction add(Fraction other) {
int newF1 = this.f1 * other.f2 + other.f1 * this.f2;
int newF2 = this.f2 * other.f2;
Fraction result = new Fraction(newF1, newF2);
result.simplify(); // 对结果进行约分
return result;
}
// 分数相减
public Fraction subtract(Fraction other) {
int newF1 = this.f1 * other.f2 - other.f1 * this.f2;
int newF2 = this.f2 * other.f2;
Fraction result = new Fraction(newF1, newF2);
result.simplify(); // 对结果进行约分
return result;
}
// 分数相乘
public Fraction multiply(Fraction other) {
int newF1 = this.f1 * other.f1;
int newF2 = this.f2 * other.f2;
Fraction result = new Fraction(newF1, newF2);
result.simplify(); // 对结果进行约分
return result;
}
// 分数相除
public Fraction divide(Fraction other) {
int newF1 = this.f1 * other.f2;
int newF2 = this.f2 * other.f1;
Fraction result = new Fraction(newF1, newF2);
result.simplify(); // 对结果进行约分
return result;
}
// 以a/b的形式打印分数
public String toString() {
return f1 + "/" + f2;
}
// 以浮点数的形式打印分数
public double toDouble() {
return (double) f1 / f2;
}
public static void main(String[] args) {
Fraction f1 = new Fraction(2, 3);
Fraction f2 = new Fraction(1, 4);
System.out.println("f1 = " + f1);
System.out.println("f2 = " + f2);
System.out.println("f1 + f2 = " + f1.add(f2));
System.out.println("f1 - f2 = " + f1.subtract(f2));
System.out.println("f1 * f2 = " + f1.multiply(f2));
System.out.println("f1 / f2 = " + f1.divide(f2));
System.out.println("f1 as double = " + f1.toDouble());
}
}
```
输出结果如下:
```
f1 = 2/3
f2 = 1/4
f1 + f2 = 11/12
f1 - f2 = 5/12
f1 * f2 = 1/6
f1 / f2 = 8/3
f1 as double = 0.6666666666666666
```