【问题标题】:How to get a variable to be used as parameter for a method如何获取要用作方法参数的变量
【发布时间】:2021-10-25 23:59:38
【问题描述】:

因此,在这段代码中,我想创建一个方法,使用户能够将字符附加到诸如 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
}

【问题讨论】:

    标签: java string variables methods stringbuilder


    【解决方案1】:

    问题 1;

    变量officeLocation1 是StringBuilder 类型。
    但是方法appendLetterToStringObject接受第一个参数为类型String

    您可能需要的是接受StringBuilder 而不是String 的方法,对方法定义执行此操作;

    public void appendLetterToStringObject(StringBuilder name, String letter)
    

    问题 2;

    您的方法的返回类型是 void。
    因此,您应该删除方法 appendLetterToStringObject 的返回语句


    最终解决方案
    两个修复的最终方法应该是这样的;

    public void appendLetterToStringObject(StringBuilder name, String letter) {
        name.append(letter);
    }
    

    【讨论】:

    • 如果您忘记了 1 班轮方法并直接使用officeLocation1.append("whatever"),那就更好了。 (当然,如果您打算稍后添加更多登录信息)
    【解决方案2】:
    1. 该方法打算使用StringBuilder,而不是String。 (您将StringBuilder 的实例传递给它,并在方法调用.append() 中将其传递给StringBuilder。)所以让它接受StringBuilder 而不是String
    2. 该方法不打算返回任何内容。 (它的返回类型是void,你的代码不会对任何返回值做任何事情。)所以不要返回任何东西。

    public void appendLetterToStringObject(StringBuilder name, String letter) {
        name.append(letter);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-12
      • 1970-01-01
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      相关资源
      最近更新 更多