public static void mul(Integer i ) { int val =i.intValue(); val*= 10; i = new Integer(val); } public static void main(String[] args) { // TODO Auto-generated method stub Integer i =new Integer(60); mul(i); System.out.println(i.intValue()); } 什么意思
时间: 2024-04-11 19:30:33 浏览: 31
这段代码定义了一个静态方法mul,它接受一个Integer对象作为参数。在方法内部,它将Integer对象转换为int类型的值,然后将该值乘以10,并将结果赋值给一个新的Integer对象i。
在main方法中,首先创建一个Integer对象i并赋值为60。然后调用mul方法并将i作为参数传入。在mul方法内部,会对传入的参数进行操作,但不会影响到外部的i对象。最后打印出i对象的值(即60)。
所以,输出结果为60,因为在mul方法中对参数进行操作并不会改变外部的i对象的值。
相关问题
问题 D: D:分数类的异常处理 时间限制: 1.000 Sec 内存限制: 128 MB 提交: 249 解决: 76 [提交] [状态] [讨论版] [命题人:外部导入]题目描述 根据已经编写好的Fraction类及输出案例, 请编写Main类,建立两个分数对象的数组f,采用Scanner类的nextInt方法,依次输入两个分数的分子和分母, 处理可能的异常输入情形:分子或分母不是整数(输出:java.util.InputMismatchException),分母是零(输出:java.lang.ArithmeticException: / by zero)。 求出并打印:((f[0]-f[1])/f[1] + f[0] ) * f[1],然后对以上结果约分,约分后再打印。 class Fraction { int numerator; int denominator; int gcd(int m, int n) { int mod = m % n; while(mod != 0) { m = n; n = mod; mod = m % n; } return n; } void print() { System.out.println(this.numerator + "/" + this.denominator); } void setFraction(int n, int d) { if(d == 0) throw new ArithmeticException("/ by zero"); this.numerator = n; this.denominator = d; } boolean equals(Fraction f) { return (this.denominator == f.denominator) && (this.numerator == f.numerator); } Fraction add(Fraction f) { Fraction r = new Fraction(); int n; int d; int g = gcd(this.denominator, f.denominator); d = f.denominator * this.denominator / g; n = this.numerator * f.denominator / g + f.numerator * this.denominator / g; r.setFraction(n, d); return r; } Fraction sub(Fraction f) { Fraction r = new Fraction(); int n; int d; int g = gcd(this.denominator, f.denominator); d = f.denominator * this.denominator / g; n = this.numerator * f.denominator / g - f.numerator * this.denominator / g; r.setFraction(n, d); return r; } Fraction mul(Fraction f) { Fraction r = new Fraction(); int n = this.numerator * f.numerator; int d = this.denominator * f.denominator; r.setFraction(n, d); return r; } Fraction div(Fraction f) { Fraction r = new Fraction(); int n = this.numerator * f.denominator; int d = this.denominator * f.numerator ; r.setFraction(n, d); return r; } Fraction simplify() { int g = gcd(this.numerator, this.denominator); this.denominator /= g; this.numerator /= g; if(this.denominator < 0) { this.denominator *= -1; this.num
这道题需要你对输入进行异常处理。你可以使用 try-catch 语句来捕获异常,并在出现异常时进行相应的操作。
具体来说,在输入分数时,你需要使用 Scanner.nextInt() 方法读取两个整数,同时使用 try-catch 语句来捕获可能出现的 InputMismatchException 异常。如果出现异常,直接输出 "java.util.InputMismatchException"。
在计算 ((f[0]-f[1])/f[1] + f[0] ) * f[1] 时,你还需要处理分母为零的情况。在 Fraction 类中已经定义了对分母为零的处理方式,即抛出 ArithmeticException 异常。因此,你需要在计算中使用 try-catch 语句来捕获可能出现的 ArithmeticException 异常,并在出现异常时输出 "/ by zero"。
最后,你还需要对计算结果进行约分。Fraction 类中已经定义了 simplify() 方法,可以直接使用。
用c语言程序编写求Mul=1*3*5*7….*99的值
以下是用C语言编写求Mul=1*3*5*7….*99的值的程序:
```c
#include <stdio.h>
int main() {
int i, mul = 1;
for (i = 1; i <= 99; i += 2) {
mul *= i;
}
printf("Mul = %d", mul);
return 0;
}
```
运行结果为:
```
Mul = 2725392139750729502980713245400918633290796330545803413734328823443106201171875
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)