请解释一下答案 [英] Please explain the answer

查看:80
本文介绍了请解释一下答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码在执行时会显示什么?

请解释为什么答案是:

 10/8 
17/12
3/2
15/9
线程main中的异常java.lang.ArrayIndexOutOfBoundsException:4 Fraction.main(Fraction.java:33)

  public   class  Fraction {
private int numerator;
private int 分母;

public 分数( int n, int d){
numerator = n; denominator = d;}

public Fraction add(Fraction f){
return newFraction(分子* f.denominator + f.numerator *分母,分母* f.denominator);}

public 分数add( int x){
return newFraction(numerator + x * denominator,denominator); }

public 分数添加( int n, int d){
return newFraction(numerator * d + n * denominator,denominator * d);}

public String toString(){ return numerator + / + denominator;}

<跨越ss =code-keyword> public static void main( String [] args){

Fraction f1 = newFraction( 1 2 );
分数f2 = newFraction( 2 3 );
分数f3 = newFraction( 3 4 );

分数[] sums = newFraction [ 4 ];
sums [ 0 ] = f1.add(f3);
sums [ 1 ] = f2.add(f3);
sums [ 2 ] = f1.add( 1 );
sums [ 3 ] = f2.add( 3 3 );

for int i = 0 ; i< = sums.length; i ++){
System.out.println(sums [i] .toString());
}
}
}





我尝试了什么:



.................................. ....................................

解决方案

消息

Quote:

线程main中的异常java.lang.ArrayIndexOutOfBoundsException:4 Fraction。 main(Fraction.java:33)

告诉你,你正试图超越数组的边界进行访问。具体来说,索引值 4 就是问题。

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

有错误,但我会让你仔细考虑。


不是你的问题,但是当你玩一个分数时,通常的做法是减少分数

 10/8 => 5/4 
17/12
3/2
15/9 => 5/3



通过将两个数字除以GCD来完成减少。

-----

学会正确缩进代码,显示其结构,有助于阅读和理解。它还有助于发现结构错误。

有些编码样式比其他代码更容易阅读,请参阅代码:

  public   class  Fraction {
private int numerator;
private int 分母;

public 分数( int n, int d){
numerator = n;分母= d;
}

public 分数添加(分数f){
return newFraction(分子* f.denominator + f.numerator *分母,分母* f.denominator);
}

public 分数添加( int x){
return newFraction(numerator + x * denominator,denominator);
}

public 分数添加( int n, int d){
return newFraction(numerator * d + n * denominator,denominator * d);
}

public String toString(){返回分子+ / +分母;
}

public static void main( String [] args){

Fraction f1 = newFraction( 1 2 );
分数f2 = newFraction( 2 3 );
分数f3 = newFraction( 3 4 );

分数[] sums = newFraction [ 4 ];
sums [ 0 ] = f1.add(f3);
sums [ 1 ] = f2.add(f3);
sums [ 2 ] = f1.add( 1 );
sums [ 3 ] = f2.add( 3 3 );

for int i = 0 ; i< = sums.length; i ++){
System.out.println(sums [i] .toString());
}
}
}



专业程序员的编辑器具有此功能,其他功能包括括号匹配和语法高亮。

Notepad ++ Home [ ^ ]

ultraedit [ ^ ]


what does the code display while it is executed ?
please explain why the answer is :

10/8
17/12
3/2 
15/9 
 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 Fraction.main(Fraction.java:33)


here is the program:

public class Fraction {
 private int numerator;
 private int denominator; 

public Fraction(int n, int d) {
numerator = n; denominator = d;} 

public Fraction add(Fraction f) { 
return newFraction(numerator ∗ f.denominator + f.numerator ∗ denominator, denominator ∗ f.denominator);} 

public Fraction add(int x) {
 return newFraction(numerator + x ∗ denominator, denominator);} 

public Fraction add(int n, int d) {
 return newFraction(numerator ∗ d + n ∗ denominator, denominator ∗ d);} 

public String toString() { return numerator + "/" + denominator;} 

public static void main(String[] args) { 

Fraction f1 = newFraction(1, 2);
Fraction f2 = newFraction(2, 3); 
Fraction f3 = newFraction(3, 4);

Fraction[] sums = newFraction[4]; 
sums[0] = f1.add(f3); 
sums[1] = f2.add(f3); 
sums[2] = f1.add(1); 
sums[3] = f2.add(3, 3);

 for (int i = 0; i <= sums.length; i++) { 
System.out.println(sums[i].toString());
}
}
}



What I have tried:

......................................................................

解决方案

The message

Quote:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 Fraction.main(Fraction.java:33)

is telling you that you're trying to access beyond the bounds of an array. Specifically, the index value 4 is the problem.
The line

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

has the error, but I'll leave you to think it through.


Not your question, but when one play with fractions, it is a common practice to reduce the fractions

10/8  => 5/4
17/12
3/2 
15/9 => 5/3


the reduction is done by dividing both numbers with their GCD.
-----
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
Some coding styles are easier to read than other, see your code:

public class Fraction {
	private int numerator;
	private int denominator;

	public Fraction(int n, int d) {
		numerator = n; denominator = d;
	}

	public Fraction add(Fraction f) {
		return newFraction(numerator ∗ f.denominator + f.numerator ∗ denominator, denominator ∗ f.denominator);
	}

	public Fraction add(int x) {
		return newFraction(numerator + x ∗ denominator, denominator);
	}

	public Fraction add(int n, int d) {
		return newFraction(numerator ∗ d + n ∗ denominator, denominator ∗ d);
	}

	public String toString() { return numerator + "/" + denominator;
	}

	public static void main(String[] args) {

		Fraction f1 = newFraction(1, 2);
		Fraction f2 = newFraction(2, 3);
		Fraction f3 = newFraction(3, 4);

		Fraction[] sums = newFraction[4];
		sums[0] = f1.add(f3);
		sums[1] = f2.add(f3);
		sums[2] = f1.add(1);
		sums[3] = f2.add(3, 3);

		for (int i = 0; i <= sums.length; i++) {
			System.out.println(sums[i].toString());
		}
	}
}


Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]


这篇关于请解释一下答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆