如何使我的方法从另一个方法中读取用户输入?

huangapple go评论82阅读模式
英文:

How to make my method read user input from another method?

问题

我仍在尝试让文本编辑器在命令行中运行,但我陷入了困境。

  1. import java.util.Scanner;
  2. public class TextEd {
  3. Scanner scan = new Scanner(System.in);
  4. public static void main(String[] args) {
  5. Editor editor = new Editor();
  6. editor.copiedText();
  7. }
  8. }
  9. class Editor {
  10. Scanner scan = new Scanner(System.in);
  11. String text;
  12. public void copiedText() {
  13. System.out.println("在此粘贴您的文本");
  14. text = scan.nextLine();
  15. menu();
  16. }
  17. public void menu() {
  18. System.out.println("欢迎使用文本编辑器。\n"
  19. + "您想要做什么?\n"
  20. + "1. 统计字符数");
  21. int choice = scan.nextInt();
  22. if (choice == 1) {
  23. counting();
  24. }
  25. }
  26. public void counting() {
  27. System.out.println(text.length());
  28. }
  29. }

问题是:每次我尝试执行时,都会出现错误 "无法找到符号 'text'"。我知道我需要从另一个方法中调用它,但我该如何做呢?

英文:

I'm still trying to make the text editor to run with cmd but I'm stuck.

  1. import java.util.Scanner;
  2. public class TextEd {
  3. Scanner scan = new Scanner(System.in);
  4. public static void main(String[] args) {
  5. Editor editor = new Editor();
  6. editor.copiedText();
  7. }
  8. }
  9. class Editor {
  10. Scanner scan = new Scanner(System.in);
  11. public void copiedText() {
  12. System.out.println("Paste your text here");
  13. String text = scan.nextLine();
  14. menu();
  15. }
  16. public void menu() {
  17. System.out.println("Welcome to the text editor.\n"
  18. + "What do you want to do?\n"
  19. + "1. count characters"?;
  20. int choice = scan.nextInt();
  21. if (choice == 1) {
  22. counting();
  23. }
  24. }
  25. public void counting() {
  26. System.out.println(text.length());
  27. }
  28. }

The problem is: everytime i try to execute i get an error "cannot find symbol 'text". I know I need to call it frim the other method, but hod do i do that?

答案1

得分: 0

你在 copiedText() 中将 text 声明为局部变量。局部变量无法在其他方法中访问。尝试设置一个字段变量(在 Editor 类中为 private 变量),可以被所有方法看到。

英文:

You have declared text as a local variable in copiedText(). Local variables cannot be seen in other methods. Try setting a field variable (a private variable in the Editor class) that can be seen by all methods

答案2

得分: 0

需要将其变成类字段:

  1. class Editor {
  2. private Scanner scan = new Scanner(System.in);
  3. private String text = "";
  4. public void copiedText() {
  5. System.out.println("Paste your text here");
  6. text = scan.nextLine();
  7. menu();
  8. }
  9. public void menu() {
  10. System.out.println("Welcome to the text editor.\n"
  11. + "What do you want to do?\n"
  12. + "1. count characters");
  13. int choice = scan.nextInt();
  14. if (choice == 1) {
  15. counting();
  16. }
  17. }
  18. public void counting() {
  19. System.out.println(text.length());
  20. }
  21. }

另外,类TextEd中的字段scan似乎没有用途,应该被移除。

英文:

Yoy need to make it a class field:

  1. class Editor {
  2. private Scanner scan = new Scanner(System.in);
  3. private String text = "";
  4. public void copiedText() {
  5. System.out.println("Paste your text here");
  6. text = scan.nextLine();
  7. menu();
  8. }
  9. public void menu() {
  10. System.out.println("Welcome to the text editor.\n"
  11. + "What do you want to do?\n"
  12. + "1. count characters"?;
  13. int choice = scan.nextInt();
  14. if (choice == 1) {
  15. counting();
  16. }
  17. }
  18. public void counting() {
  19. System.out.println(text.length());
  20. }
  21. }

Also field scan in class TextEd seems to have no purpose and should therefore be removed.

答案3

得分: 0

你的代码出现了编译错误,因为menu方法中System.out.println()的括号没有正确闭合。

你想在另一个方法中调用text字符串的方法,所以你可以将text传递给该方法,或者将其定义为类中的字段,以便其他方法可以访问该变量。

  1. import java.util.Scanner;
  2. public class TextEd {
  3. Scanner scan = new Scanner(System.in);
  4. public static void main(String[] args) {
  5. Editor editor = new Editor();
  6. editor.copiedText();
  7. }
  8. }
  9. class Editor {
  10. Scanner scan = new Scanner(System.in);
  11. String text;
  12. public void copiedText() {
  13. System.out.println("Paste your text here");
  14. text = scan.nextLine();
  15. menu();
  16. }
  17. public void menu() {
  18. System.out.println("Welcome to the text editor.\n"
  19. + "What do you want to do?\n"
  20. + "1. count characters?");
  21. int choice = scan.nextInt();
  22. if (choice == 1) {
  23. counting();
  24. }
  25. }
  26. public void counting() {
  27. System.out.println(text.length());
  28. }
  29. }

注意代码块,其他块或方法中定义的每个变量,在其他块或方法中都无法访问。

英文:

your code has compile error because the parentheses in System.out.println() in menu are not closed properly.

you want to call a method of a text String in another method, so you can pass text to that method or just define it as a field in your class so that other methods can reach that variable.

  1. import java.util.Scanner;
  2. public class TextEd {
  3. Scanner scan = new Scanner(System.in);
  4. public static void main(String[] args) {
  5. Editor editor = new Editor();
  6. editor.copiedText();
  7. }
  8. }
  9. class Editor {
  10. Scanner scan = new Scanner(System.in);
  11. String text;
  12. public void copiedText() {
  13. System.out.println("Paste your text here");
  14. text = scan.nextLine();
  15. menu();
  16. }
  17. public void menu() {
  18. System.out.println("Welcome to the text editor.\n"
  19. + "What do you want to do?\n"
  20. + "1. count characters?");
  21. int choice = scan.nextInt();
  22. if (choice == 1) {
  23. counting();
  24. }
  25. }
  26. public void counting() {
  27. System.out.println(text.length());
  28. }
  29. }

be aware of blocks, every variable that is defined in other blocks or methods, is not reachable in other blocks or methods.

huangapple
  • 本文由 发表于 2020年9月17日 20:43:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63938295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定