如何获取要用作方法参数的变量
How to get a variable to be used as parameter for a method
所以在这段代码中,我想创建一个方法,使用户能够将一个字符附加到某个 stringbuilder 对象,例如 officelocation1,但是每当(第 158 行)我尝试执行 method(officelocation1, 'T' );有错误 1(方法名称是 appendLetterToStringObject),而方法有错误,因为每当我尝试 运行 这段代码时都会出现错误 2.
错误 1:
java main.java
main.java:158: error: incompatible types: StringBuilder cannot be converted to String
appendLetterToStringObject(officeLocation1, "Hello");
^
错误 2:
main.java:211: error: cannot find symbol
name.append(letter);
^
symbol: method append(String)
location: variable name of type String
main.java:212: error: incompatible types: unexpected return value
return name;
^
代码:
import java.util.Scanner;
public class main{
public static void main(String[] args){
//Scanner
Scanner input = new Scanner(System.in);
//variables
int repeat;
repeat = 0;
int repeat2;
repeat2 = 0;
int repeat2Input;
repeat2Input = 0;
int repeat3;
repeat3 = 0;
int repeat3Input;
repeat3Input = 0;
int repeat4;
repeat4 = 0;
int repeat4Input;
repeat4Input = 0;
int lengthOfOfficeLocationName1;
lengthOfOfficeLocationName1 = 0;
int lengthOfOfficeLocationName2;
lengthOfOfficeLocationName2 = 0;
int lengthOfOfficeLocationName3;
lengthOfOfficeLocationName3 = 0;
int lengthOfOfficeLocationName4;
lengthOfOfficeLocationName4 = 0;
String stringOfOfficeLocationName1;
String stringOfOfficeLocationName2;
String stringOfOfficeLocationName3;
String stringOfOfficeLocationName4;
//variables for the inputs
String [] stringsForStringBuilder = new String[4];
double [][] officeLocationCoords = new double[4][4];
double [] officeDistanceCenterOffice = new double[4];
//the first inout is for first office, second for second and etc. so i am going to make a single variable array
//heart of the code
do {
System.out.println("The purpose of the program is to find out the location of the 4 possible office locations and how their viablity");
System.out.println("***All inputs would be taken by an iterative process***");
//inputs
do {
System.out.println("---Inputs time---");
System.out.println("input for the coordinates of the 4 office locations (please eneter coordinates and the input will be entered iteratively). P.S. first input is for the first odffice second input is for the secodn office");
// -------
System.out.println("Please enter a value for office number so for example if you enter 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n the output would be\n1,2 \n3,4 \n5,6 \n7,8\n Please enter 8 numbers that will represent the coordinates such as the example given above: ");
for (int i = 0; i < 4; i++){
for (int j = 0; j < 2; j++) {
officeLocationCoords[i][j] = input.nextDouble();
}
}
System.out.println("Enter office locations from the center office");
System.out.println("the first number you enter for the coordinates will hopefully be the first office location second input second office and so on the units is meters");
for (int i = 0; i < officeDistanceCenterOffice.length; i++){
System.out.println("Please enter a value for office distnace from center office for office number " + (i+1) );
officeDistanceCenterOffice[i] = input.nextDouble();
}
System.out.println("please check if it is correct");
System.out.println("office location cords");
for (int i = 0; i < 4; i++) {
for (int j =0; j < 2; j++) {
System.out.print(officeLocationCoords[i][j] + " ");
}
System.out.println();
}
System.out.println("distance from central office");
for (int i = 0; i < officeDistanceCenterOffice.length; i++) {
System.out.println("Office number " + (i+1) + " is: " + officeDistanceCenterOffice[i]);
}
System.out.print("please enter 99 to repeat, 100 to break the circuit or 101 to continue");
repeat2Input = input.nextInt();
if (repeat2Input == 99) {
System.out.println("please enter 99 for your repeat to passthrough");
repeat2 = input.nextInt();
}
else if (repeat2Input == 100) {
System.out.println("please come back again");
break;
}
else if (repeat2Input == 101){
do {
//code
//take input for the strings required for string StringBuilder
for (int i = 0; i < stringsForStringBuilder.length; i++) {
System.out.println("Please enter the office's location that is under 15 words. (The input will be taken itteratively)");
stringsForStringBuilder[i] = input.next();
}
//check if it is more than 15 charecters long
for (int k = 0; k < stringsForStringBuilder.length; k++) {
if (stringsForStringBuilder[k].length() > 15) {
System.out.println("you have to enter a value less than 15 charecters for " + k + " th one. to restart eneter 99 or to break enter 100");
repeat3Input = input.nextInt();
if (repeat3Input == 99) {
System.out.print("Please eneter 99: ");
repeat3 = input.nextInt();
}
else if (repeat3Input == 100) {
System.out.println("please come back again");
break;
}
else {
System.out.print("Please eneter 99 since you have made a mistake: ");
repeat3 = input.nextInt();
}
}
}
StringBuilder officeLocation1 = new StringBuilder(stringsForStringBuilder[0]);
StringBuilder officeLocation2 = new StringBuilder(stringsForStringBuilder[1]);
StringBuilder officeLocation3 = new StringBuilder(stringsForStringBuilder[2]);
StringBuilder officeLocation4 = new StringBuilder(stringsForStringBuilder[3]);
//----
StringBuilder oldOfficeLocation1 = new StringBuilder(stringsForStringBuilder[0]);
StringBuilder oldOfficeLocation2 = new StringBuilder(stringsForStringBuilder[1]);
StringBuilder oldOfficeLocation3 = new StringBuilder(stringsForStringBuilder[2]);
StringBuilder oldOfficeLocation4 = new StringBuilder(stringsForStringBuilder[3]);
System.out.print("If you made a mistake and want to repeat press 99 if you want to break press 100 and if you have not made a mistake enter 101: ");
repeat3Input = input.nextInt();
if (repeat3Input == 99) {
System.out.print("press 99 to restart: ");
repeat3 = input.nextInt();
}
else if (repeat3Input == 100) {
System.out.println("Please come back soon!");
break;
}
else if (repeat3Input == 101) {
do {
for (int i = 0; i < officeDistanceCenterOffice.length; i++){
if (officeDistanceCenterOffice[i] > 800 || officeDistanceCenterOffice[i] < 100) {
System.out.println("Please enter a value for office distnace from center office for office number " + (i+1) + " (That is less than 800 and greater than 100)");
officeDistanceCenterOffice[i] = input.nextDouble();
}
}
//code
//output distanace between office, coordinates, string obejects (stringsforstringbuilder[i]), old string object and manipulated string object
//get input for what they want to do
lengthOfOfficeLocationName1 = officeLocation1.length();
lengthOfOfficeLocationName2 = officeLocation2.length();
lengthOfOfficeLocationName3 = officeLocation3.length();
lengthOfOfficeLocationName4 = officeLocation4.length();
stringOfOfficeLocationName1 = officeLocation1.toString();
stringOfOfficeLocationName2 = officeLocation2.toString();
stringOfOfficeLocationName3 = officeLocation3.toString();
stringOfOfficeLocationName4 = officeLocation4.toString();
if (lengthOfOfficeLocationName1 < 14) {
officeLocation1.append('T');
}
if (lengthOfOfficeLocationName2 < 14) {
officeLocation2.append('B');
}
if (lengthOfOfficeLocationName3 < 14) {
officeLocation3.append('C');
}
if (lengthOfOfficeLocationName4 < 14) {
officeLocation4.append('E');
}
appendLetterToStringObject(officeLocation1, "Hello");
//outputs
//HERE IS THE LINE 158 ISSUE
System.out.println("Outputs");
System.out.println("distance between office and center office");
for (int i = 0; i < officeDistanceCenterOffice.length; i++) {
System.out.println("distance between office " + i + " and center office is " + officeDistanceCenterOffice[i] + " meters");
}
System.out.println("Coordinates: ");
for (int i = 0; i < 4; i++) {
for (int j =0; j < 2; j++) {
System.out.print(officeLocationCoords[i][j] + " ");
}
System.out.println();
}
System.out.println("Old String Objects");
System.out.println(oldOfficeLocation1);
System.out.println(oldOfficeLocation2);
System.out.println(oldOfficeLocation3);
System.out.println(oldOfficeLocation4);
System.out.println("New String Objects");
System.out.println(officeLocation1);
System.out.println(officeLocation2);
System.out.println(officeLocation3);
System.out.println(officeLocation4);
System.out.println("Strings used for StringBuilder");
for (int i = 0; i < stringsForStringBuilder.length; i++) {
System.out.println(stringsForStringBuilder[i]);
}
System.out.println("Those are all the outputs of this program that are pertinent and distungishes the manipulated data and continues the theme of my course work well atleast in my opinion");
} while (repeat4 == 99);
}
else {
System.out.print("You have made a mistake type 99 to repeat");
repeat3 = input.nextInt();
}
} while (repeat3 == 99);
}
else {
System.out.println("you have made a mistake try again by pressing 99 or break the process by pressing 100");
repeat2Input = input.nextInt();
if (repeat2Input == 99) {
System.out.println("enter 99");
repeat2 = input.nextInt();
}
else if (repeat2Input == 100) {
System.out.println("come back agian");
break;
}
}
} while (repeat2 == 99);
} while (repeat == 99);
}
public void appendLetterToStringObject(String name, String letter) {
name.append(letter);
return name;
}
//HERE IS THE METHOD ISSUE
}
- 该方法打算使用
StringBuilder
,而不是 String
。 (您将 StringBuilder
的实例传递给它,并在 StringBuilder
上的方法调用 .append()
中传递给它。)所以让它接受 StringBuilder
而不是 String
.
- 该方法无意 return 任何内容。 (它的 return 类型是
void
并且您的代码不会对任何 returned 值做任何事情。)所以不要 return 任何事情。
public void appendLetterToStringObject(StringBuilder name, String letter) {
name.append(letter);
}
问题 1;
变量officeLocation1
是StringBuilder类型。
但是方法 appendLetterToStringObject
接受第一个参数作为类型 String
您可能需要的方法是接受 StringBuilder
而不是 String
对方法定义执行此操作;
public void appendLetterToStringObject(StringBuilder name, String letter)
问题2;
您方法的 return 类型无效。
因此,您应该删除方法 appendLetterToStringObject
的 return 语句
最终解决方案
两个修复的最终方法应该是这样的;
public void appendLetterToStringObject(StringBuilder name, String letter) {
name.append(letter);
}
所以在这段代码中,我想创建一个方法,使用户能够将一个字符附加到某个 stringbuilder 对象,例如 officelocation1,但是每当(第 158 行)我尝试执行 method(officelocation1, 'T' );有错误 1(方法名称是 appendLetterToStringObject),而方法有错误,因为每当我尝试 运行 这段代码时都会出现错误 2.
错误 1:
java main.java
main.java:158: error: incompatible types: StringBuilder cannot be converted to String
appendLetterToStringObject(officeLocation1, "Hello");
^
错误 2:
main.java:211: error: cannot find symbol
name.append(letter);
^
symbol: method append(String)
location: variable name of type String
main.java:212: error: incompatible types: unexpected return value
return name;
^
代码:
import java.util.Scanner;
public class main{
public static void main(String[] args){
//Scanner
Scanner input = new Scanner(System.in);
//variables
int repeat;
repeat = 0;
int repeat2;
repeat2 = 0;
int repeat2Input;
repeat2Input = 0;
int repeat3;
repeat3 = 0;
int repeat3Input;
repeat3Input = 0;
int repeat4;
repeat4 = 0;
int repeat4Input;
repeat4Input = 0;
int lengthOfOfficeLocationName1;
lengthOfOfficeLocationName1 = 0;
int lengthOfOfficeLocationName2;
lengthOfOfficeLocationName2 = 0;
int lengthOfOfficeLocationName3;
lengthOfOfficeLocationName3 = 0;
int lengthOfOfficeLocationName4;
lengthOfOfficeLocationName4 = 0;
String stringOfOfficeLocationName1;
String stringOfOfficeLocationName2;
String stringOfOfficeLocationName3;
String stringOfOfficeLocationName4;
//variables for the inputs
String [] stringsForStringBuilder = new String[4];
double [][] officeLocationCoords = new double[4][4];
double [] officeDistanceCenterOffice = new double[4];
//the first inout is for first office, second for second and etc. so i am going to make a single variable array
//heart of the code
do {
System.out.println("The purpose of the program is to find out the location of the 4 possible office locations and how their viablity");
System.out.println("***All inputs would be taken by an iterative process***");
//inputs
do {
System.out.println("---Inputs time---");
System.out.println("input for the coordinates of the 4 office locations (please eneter coordinates and the input will be entered iteratively). P.S. first input is for the first odffice second input is for the secodn office");
// -------
System.out.println("Please enter a value for office number so for example if you enter 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n the output would be\n1,2 \n3,4 \n5,6 \n7,8\n Please enter 8 numbers that will represent the coordinates such as the example given above: ");
for (int i = 0; i < 4; i++){
for (int j = 0; j < 2; j++) {
officeLocationCoords[i][j] = input.nextDouble();
}
}
System.out.println("Enter office locations from the center office");
System.out.println("the first number you enter for the coordinates will hopefully be the first office location second input second office and so on the units is meters");
for (int i = 0; i < officeDistanceCenterOffice.length; i++){
System.out.println("Please enter a value for office distnace from center office for office number " + (i+1) );
officeDistanceCenterOffice[i] = input.nextDouble();
}
System.out.println("please check if it is correct");
System.out.println("office location cords");
for (int i = 0; i < 4; i++) {
for (int j =0; j < 2; j++) {
System.out.print(officeLocationCoords[i][j] + " ");
}
System.out.println();
}
System.out.println("distance from central office");
for (int i = 0; i < officeDistanceCenterOffice.length; i++) {
System.out.println("Office number " + (i+1) + " is: " + officeDistanceCenterOffice[i]);
}
System.out.print("please enter 99 to repeat, 100 to break the circuit or 101 to continue");
repeat2Input = input.nextInt();
if (repeat2Input == 99) {
System.out.println("please enter 99 for your repeat to passthrough");
repeat2 = input.nextInt();
}
else if (repeat2Input == 100) {
System.out.println("please come back again");
break;
}
else if (repeat2Input == 101){
do {
//code
//take input for the strings required for string StringBuilder
for (int i = 0; i < stringsForStringBuilder.length; i++) {
System.out.println("Please enter the office's location that is under 15 words. (The input will be taken itteratively)");
stringsForStringBuilder[i] = input.next();
}
//check if it is more than 15 charecters long
for (int k = 0; k < stringsForStringBuilder.length; k++) {
if (stringsForStringBuilder[k].length() > 15) {
System.out.println("you have to enter a value less than 15 charecters for " + k + " th one. to restart eneter 99 or to break enter 100");
repeat3Input = input.nextInt();
if (repeat3Input == 99) {
System.out.print("Please eneter 99: ");
repeat3 = input.nextInt();
}
else if (repeat3Input == 100) {
System.out.println("please come back again");
break;
}
else {
System.out.print("Please eneter 99 since you have made a mistake: ");
repeat3 = input.nextInt();
}
}
}
StringBuilder officeLocation1 = new StringBuilder(stringsForStringBuilder[0]);
StringBuilder officeLocation2 = new StringBuilder(stringsForStringBuilder[1]);
StringBuilder officeLocation3 = new StringBuilder(stringsForStringBuilder[2]);
StringBuilder officeLocation4 = new StringBuilder(stringsForStringBuilder[3]);
//----
StringBuilder oldOfficeLocation1 = new StringBuilder(stringsForStringBuilder[0]);
StringBuilder oldOfficeLocation2 = new StringBuilder(stringsForStringBuilder[1]);
StringBuilder oldOfficeLocation3 = new StringBuilder(stringsForStringBuilder[2]);
StringBuilder oldOfficeLocation4 = new StringBuilder(stringsForStringBuilder[3]);
System.out.print("If you made a mistake and want to repeat press 99 if you want to break press 100 and if you have not made a mistake enter 101: ");
repeat3Input = input.nextInt();
if (repeat3Input == 99) {
System.out.print("press 99 to restart: ");
repeat3 = input.nextInt();
}
else if (repeat3Input == 100) {
System.out.println("Please come back soon!");
break;
}
else if (repeat3Input == 101) {
do {
for (int i = 0; i < officeDistanceCenterOffice.length; i++){
if (officeDistanceCenterOffice[i] > 800 || officeDistanceCenterOffice[i] < 100) {
System.out.println("Please enter a value for office distnace from center office for office number " + (i+1) + " (That is less than 800 and greater than 100)");
officeDistanceCenterOffice[i] = input.nextDouble();
}
}
//code
//output distanace between office, coordinates, string obejects (stringsforstringbuilder[i]), old string object and manipulated string object
//get input for what they want to do
lengthOfOfficeLocationName1 = officeLocation1.length();
lengthOfOfficeLocationName2 = officeLocation2.length();
lengthOfOfficeLocationName3 = officeLocation3.length();
lengthOfOfficeLocationName4 = officeLocation4.length();
stringOfOfficeLocationName1 = officeLocation1.toString();
stringOfOfficeLocationName2 = officeLocation2.toString();
stringOfOfficeLocationName3 = officeLocation3.toString();
stringOfOfficeLocationName4 = officeLocation4.toString();
if (lengthOfOfficeLocationName1 < 14) {
officeLocation1.append('T');
}
if (lengthOfOfficeLocationName2 < 14) {
officeLocation2.append('B');
}
if (lengthOfOfficeLocationName3 < 14) {
officeLocation3.append('C');
}
if (lengthOfOfficeLocationName4 < 14) {
officeLocation4.append('E');
}
appendLetterToStringObject(officeLocation1, "Hello");
//outputs
//HERE IS THE LINE 158 ISSUE
System.out.println("Outputs");
System.out.println("distance between office and center office");
for (int i = 0; i < officeDistanceCenterOffice.length; i++) {
System.out.println("distance between office " + i + " and center office is " + officeDistanceCenterOffice[i] + " meters");
}
System.out.println("Coordinates: ");
for (int i = 0; i < 4; i++) {
for (int j =0; j < 2; j++) {
System.out.print(officeLocationCoords[i][j] + " ");
}
System.out.println();
}
System.out.println("Old String Objects");
System.out.println(oldOfficeLocation1);
System.out.println(oldOfficeLocation2);
System.out.println(oldOfficeLocation3);
System.out.println(oldOfficeLocation4);
System.out.println("New String Objects");
System.out.println(officeLocation1);
System.out.println(officeLocation2);
System.out.println(officeLocation3);
System.out.println(officeLocation4);
System.out.println("Strings used for StringBuilder");
for (int i = 0; i < stringsForStringBuilder.length; i++) {
System.out.println(stringsForStringBuilder[i]);
}
System.out.println("Those are all the outputs of this program that are pertinent and distungishes the manipulated data and continues the theme of my course work well atleast in my opinion");
} while (repeat4 == 99);
}
else {
System.out.print("You have made a mistake type 99 to repeat");
repeat3 = input.nextInt();
}
} while (repeat3 == 99);
}
else {
System.out.println("you have made a mistake try again by pressing 99 or break the process by pressing 100");
repeat2Input = input.nextInt();
if (repeat2Input == 99) {
System.out.println("enter 99");
repeat2 = input.nextInt();
}
else if (repeat2Input == 100) {
System.out.println("come back agian");
break;
}
}
} while (repeat2 == 99);
} while (repeat == 99);
}
public void appendLetterToStringObject(String name, String letter) {
name.append(letter);
return name;
}
//HERE IS THE METHOD ISSUE
}
- 该方法打算使用
StringBuilder
,而不是String
。 (您将StringBuilder
的实例传递给它,并在StringBuilder
上的方法调用.append()
中传递给它。)所以让它接受StringBuilder
而不是String
. - 该方法无意 return 任何内容。 (它的 return 类型是
void
并且您的代码不会对任何 returned 值做任何事情。)所以不要 return 任何事情。
public void appendLetterToStringObject(StringBuilder name, String letter) {
name.append(letter);
}
问题 1;
变量officeLocation1
是StringBuilder类型。
但是方法 appendLetterToStringObject
接受第一个参数作为类型 String
您可能需要的方法是接受 StringBuilder
而不是 String
对方法定义执行此操作;
public void appendLetterToStringObject(StringBuilder name, String letter)
问题2;
您方法的 return 类型无效。
因此,您应该删除方法 appendLetterToStringObject
的 return 语句
最终解决方案
两个修复的最终方法应该是这样的;
public void appendLetterToStringObject(StringBuilder name, String letter) {
name.append(letter);
}