0
我找不到在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();
}
}
//}
三角形是否正確;只能在C++中工作? – ChadM 2011-03-30 19:30:03
笑UR問題是不是'C++'盡我的答案,然後回來給我(是/否/有時 - 視情況而定) – Neal 2011-03-30 19:30:52
這是丟臉的,我知道, – ChadM 2011-03-30 19:31:12