我找不到right.GetText()生成的错误;靠近底部。错误是上面的标题。从我所读到的,成员变量必须用Java初始化,所以我经历了所有这些并无济于事。我想我已经在我的默认构造函数中完成了所有操作,但我可能错了,这是我的第一个java程序。感谢您的帮助,我只是不想再花一个小时了。
import java.io.*;
import java.lang.*;
//class Shapes
//{
class Triangle
{
private int rowNum;
private String text;
Triangle() {
rowNum=0;
text="";
}
public void GetText()
{
int flag=0;
String trialText="";
while(flag==0){
BufferedReader reader= new BufferedReader(new InputStreamReader(System.in));
System.out.println("\nPlease enter a word to display in the triangle(ie bob, tom, etc.)");
try{
trialText = reader.readLine();
System.out.println("Your entered "+ trialText); //print the data entered by the user
}
catch (IOException ioe){ //statement to execute if an input/output exception occurs
System.out.println("An unexpected error occured.");
continue;
}
char[] newText=trialText.toCharArray();
for(int i=0;i<trialText.length();i++){
if(!Character.isLetter(newText[i])){
System.out.println(newText[i]+"is not a letter, please enter a real word.");
flag=0;
break;
}
flag=1;
}
}//While(flag==0)
text=trialText;
text=text.toLowerCase(); //Converting all input text to Lower Case
}
public void ShowText()
{
System.out.println(text);
}
public void ShowRowNum()
{
System.out.println(rowNum);
}
/*
public boolean testText(String new_Text)
{
try{
String trialText = new_Text;
System.out.println("You entered "+trialText); //print the data entered by the user
}
catch (IOException ioe){ //statement to execute if an input/output exception occurs
System.out.println("An unexpected error occured.");
return false;
}
return true;
}
*/
public boolean testRowNum(String new_Text)
{
try{
Integer.parseInt(new_Text);
System.out.println("You entered "+ new_Text); //print the data entered by the user
}
catch (NumberFormatException nfe){ //statement to execute if an input/output exception occurs
System.out.println("You have entered a non-integer. ");
return false;
}
return true;
}
public void GetRows()
{
System.out.println("Please enter the number of rows the triangle will contain");
BufferedReader newRow= new BufferedReader(new InputStreamReader(System.in));
try{
if(testRowNum(newRow.readLine().trim())==true)
rowNum=Integer.parseInt(newRow.readLine().trim());
}
catch (IOException ioe) {//statement to execute if an input/output exception occurs
System.out.println("You have entered a non-integer. ");
}
}
public void DisplayTriangle()
{
int numSpaces=0;
String spaces="";
String bricks="";
for(int i=0; i<rowNum; i++){
if(rowNum%2==0)
numSpaces=(rowNum/2);
else if(rowNum%2==1)
numSpaces=((rowNum/2)-1);
for(int j=numSpaces; j>0; j--){
spaces=spaces+" ";
}
for(int k=0; k<rowNum; k++){
bricks=bricks+"=";
}
System.out.println(spaces+bricks);
}
}
public static void main(String args[])
{
Triangle right;
right.GetText();
right.GetRows();
right.ShowText();
right.ShowRowNum();
right.DisplayTriangle();
}
}
//}
答案 0 :(得分:2)
在你的主要fn中试试这个:
Triangle right = new Triangle();
您必须初始化Triangle
答案 1 :(得分:0)
试试这个:
public static void main(String args[])
{
Triangle right = new Triangle();
right.GetText();
right.GetRows();
right.ShowText();
right.ShowRowNum();
right.DisplayTriangle();
}
你试图在它有任何内容之前调用right
,它只是声明了它。