首页 文章

Java中的方法帮助

提问于
浏览
-1

以下是我的代码 . 当我试图调用正三角形或负三角形时,它会抛出错误 . 请帮忙!

下面是一些错误方法,类方法中的negativeTriangle不能应用于给定的类型; required:String,int,int found:无参数原因:实际和形式参数列表的长度不同

方法中的positiveTriangle方法不能应用于给定的类型; required:String,int found:无参数原因:实际和形式参数列表的长度不同

“)”预期“;”预计不会发表声明“;”预期找不到符号符号:变量字符串位置:类main


import java.util.Scanner;
public class main {


    public static void main(String[] args) {

        purpose();

        gettingPrintCharacter();

        gettingVarifyingInputs();

        positiveTriangle(String aChar, int amount));

        negativeTriangle(String aChar, int amount, int opposite);


    }

    public static void purpose() {  
        System.out.print("This program will print a right triangle. \nThe "
           + "character that is entered at the prompt will be used to print"
           + " a right triangle.  \nThe value that is entered at the prompt "
           + "will be the right triangle's height and width.  \nIf the value "
           + "is negative only an outline of the triangle will be printed "
           + "else a filled in triangle will be printed.\n\n");
        //displays purpose of program 
    }

    public static void gettingPrintCharacter(){
        Scanner keyboard = new Scanner (System.in);
        System.out.print("Enter the character to be used: ");
        String triangle = keyboard.next();
        //converts character(s) entered into string
        char aChar = triangle.charAt(0);
        //slects only the first character of the string
        //the string above is to "idiot" or accident prof if they enter 
        //more than one character
    }

    public static void gettingVarifyingInputs(){
        Scanner keyboard = new Scanner (System.in);
        int amount;
        boolean valid= false;
        while (!valid)//first loop unitl uers inputs correct value
        {// starts the main loop
            System.out.print("Enter a non-zero integer length (+/-1 through +/-16): ");
            amount = keyboard.nextInt();// length and width of triangle
            while(amount == 0 || amount >16 || amount <-16)// invalid input tester
            {// starts second loop for user to input correct value
                System.out.print("Input value outside of range");
                System.out.print("\nEnter a non-zero integer length (+/-1 through +/-16): ");
                amount = keyboard.nextInt();// length and width of triangle
            }// ends loop
            valid = true;// end user input loop
        }
    }

    public static void positiveTriangle(String aChar, int amount){

        int i = 1;
        int j = i;
        do { //first do loop
            do{//second do loop
                System.out.print(aChar);
                j++;
            }while (j<=i);//second while loop end
            System.out.println();
            i++;
            j=1;
        }while(i<=amount);//first while loop end
    }//ends last else 

    public static void negativeTriangle(String aChar, int amount, int opposite) {

        opposite = Math.abs(amount);//finds absolute value
        for (int i = 0; i <= opposite; i++) {// first negative loop
            for (int j = 0; j <= i; j++) { // second negative loop  
                if (i == opposite) { 
                    for (int k = 0; k<= opposite; k++)// third negative loop
                        System.out.print("*");//prints for right outside of triangle
                    break;//  ends third negative loop
                } 
                else if (i > 1) {
                    System.out.print("*");          
                    for (int k = 0; k < i - 1; k++)// fourth negative loop
                        System.out.print(" ");
                    System.out.print("*"); 
                    break;// end fouth negative loop                  
                } else //else to print outside of triangle
                    System.out.print("*");//prints for right outside of triangle
            } // restarts the negative  second loop
            System.out.println();
        }// restarts the negative first loop
    }// ends loop    
}

2 回答

  • 0

    你不能这样调用这个方法:

    positiveTriangle(String aChar, int amount));
    negativeTriangle(String aChar, int amount, int opposite);
    

    实际上,您需要传递有效的参数(值)才能调用方法 .

    positiveTriangle("A", 5);
    negativeTriangle("A", 6, 1);
    

    此链接将帮助您了解Java Methods

    [Edit]

    您需要从已经声明的方法中获取nChar和amount值,因此您需要告诉您的方法返回generate值,如:

    public static String gettingPrintCharacter() { <- here you need to specify the return type of your method.
        //your current code
        return aChar; <- this line should be the last line in you method
    }
    

    也:

    public static int gettingVarifyingInputs(){
        //your current code
        return amount;
    }
    

    然后你可以简单地在你的main方法中做:

    String aChar = gettingPrintCharacter();
    int amount = gettingVarifyingInputs();
    
    positiveTriangle(aChar, amount);
    negativeTriangle(aChar, amount, 0);
    
  • 1

    这不是调用方法的有效方法:

    positiveTriangle(String aChar, int amount)); //Also not extra ')' here
    
    negativeTriangle(String aChar, int amount, int opposite);
    

    你需要像这样打电话:

    positiveTriangle("SomeString", 0);   //Pass the actual value here, this is just a example
    
    negativeTriangle("SomeString", 0, 0);
    

相关问题