package lab10;
import java.util.*;
public class Lab10 {
class QuadraticEquation{
double a,b,c;
QuadraticEquation(){
Random number1 = new Random();
a = (int) (number1.nextDouble() * 8 + 1.0);
Random number2 = new Random();
b = (int) (number2.nextDouble() * 8 + 1.0);
Random number3 = new Random();
c = (int) (number3.nextDouble() * 8 + 1.0);
}
double getA(){
return a;
}
double getB(){
return b;
}
double getC(){
return c;
}
double getDiscriminant(){
double discriminant;
discriminant = (Math.pow(b, 2)- 4 *(a *c));
return discriminant;
}
}
public static void main(String[] args) {
QuadraticEquation equation = new QuadraticEquation(); //<---- error
System.out.println("Three randomized coefficients are: ");
System.out.println("a = " + equation.getA());
System.out.println("b = " + equation.getB());
System.out.println("c = " + equation.getC());
}
}
我讀了一些文章的這裏計算器這個錯誤,但即使他們閱讀後,我似乎並沒有參考真正明白,爲什麼我在做了。我引用以前的項目,我不明白爲什麼我不能宣佈一元二次方程公式=新的一元二次方程()的Java:非靜態變量不能從靜態上下文
這裏是我引用的項目:
package InClass05;
public class TestSimpleSquare {
public static void main(String[] args) {
SimpleSquare square1 = new SimpleSquare();
System.out.println("The area of the square of side lengths " + square1.length + " is " + square1.getArea());
System.out.println("The perimeter of the square of side lengths " + square1.length + " is " + square1.getPerimeter());
System.out.println("The diagonal of the square of side lengths " + square1.length + " is " + square1.getDiagonal());
System.out.println("");
SimpleSquare square2 = new SimpleSquare(25);
System.out.println("The area of the square of side lengths " + square2.length + " is " + square2.getArea());
System.out.println("The perimeter of the square of side lengths " + square2.length + " is " + square2.getPerimeter());
System.out.println("The diagonal of the square of side lengths " + square2.length + " is " + square2.getDiagonal());
System.out.println("");
square2.length = 500;
System.out.println("The area of the square of side lengths " + square2.length + " is " + square2.getArea());
System.out.println("The perimeter of the square of side lengths " + square2.length + " is " + square2.getPerimeter());
System.out.println("The diagonal of the square of side lengths " + square2.length + " is " + square2.getDiagonal());
}
}
class SimpleSquare{
double length;
SimpleSquare(){
length = 10;
}
SimpleSquare(double newlength){
length = newlength;
}
double getArea(){
return length * length;
}
double getPerimeter(){
return length + length + length + length;
}
double getDiagonal(){
return Math.sqrt(Math.pow(length, 2) + Math.pow(length, 2));
}
void setlength(double newlength){
length = newlength;
}
}
在這其中我'能夠聲明SimpleSquare square1 = new SimpleSquare(); 我不明白爲什麼我不能在我現在正在處理的項目中做到這一點。 任何幫助,將不勝感激。謝謝。
我明白了。但是如何在我的參考項目中沒有將其聲明爲靜態類的情況下工作。我能夠做SimpleSquare square1 = new SimpleSquare();沒有發生錯誤。 – DeathWish
它在不同的層次上定義,不在你的課堂內。這被稱爲包級別。這兩種情況與Java的觀點截然不同。 SimpleSquare的情況就像在單獨的文件中定義的一樣。 –
@DeathWish您的類QuadraticEquation在類Lab10中定義。你的類SimpleSquare不在另一個類中(你在定義它之前關閉了TestSimpleSquare類) – rjsang