且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

请解释一下答案

更新时间:2023-11-10 08:00:28

消息

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[^]