以下是我的代码。当我试图调用正三角形或负三角形时,它会抛出错误。请帮忙!
以下是一些错误 方法中的negativeTriangle方法不能应用于给定的类型; required:String,int,int 发现:没有争论 原因:实际和正式的参数列表长度不同
方法中的positiveTriangle方法不能应用于给定的类型; required:String,int 发现:没有争论 原因:实际和正式的参数列表长度不同
“)”预期 “;”预期 不是声明 “;”预期 找不到标志 symbol:变量String location:class 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
}
答案 0 :(得分:1)
你不能这样调用这个方法:
positiveTriangle(String aChar, int amount));
negativeTriangle(String aChar, int amount, int opposite);
实际上,您需要传递一个有效的参数(值)才能调用方法。
positiveTriangle("A", 5);
negativeTriangle("A", 6, 1);
此链接可帮助您了解Java Methods
<强> [编辑] 强>
您需要从已经声明的方法中获取nChar和amount值,因此您需要告诉您的方法返回生成值,如下所示:
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;
}
然后你可以简单地用你的主要方法做:
String aChar = gettingPrintCharacter();
int amount = gettingVarifyingInputs();
positiveTriangle(aChar, amount);
negativeTriangle(aChar, amount, 0);
答案 1 :(得分:0)
这不是调用方法的有效方法:
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);