英文:
How to make my method read user input from another method?
问题
我仍在尝试让文本编辑器在命令行中运行,但我陷入了困境。
import java.util.Scanner;
public class TextEd {
Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
Editor editor = new Editor();
editor.copiedText();
}
}
class Editor {
Scanner scan = new Scanner(System.in);
String text;
public void copiedText() {
System.out.println("在此粘贴您的文本");
text = scan.nextLine();
menu();
}
public void menu() {
System.out.println("欢迎使用文本编辑器。\n"
+ "您想要做什么?\n"
+ "1. 统计字符数");
int choice = scan.nextInt();
if (choice == 1) {
counting();
}
}
public void counting() {
System.out.println(text.length());
}
}
问题是:每次我尝试执行时,都会出现错误 "无法找到符号 'text'"。我知道我需要从另一个方法中调用它,但我该如何做呢?
英文:
I'm still trying to make the text editor to run with cmd but I'm stuck.
import java.util.Scanner;
public class TextEd {
Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
Editor editor = new Editor();
editor.copiedText();
}
}
class Editor {
Scanner scan = new Scanner(System.in);
public void copiedText() {
System.out.println("Paste your text here");
String text = scan.nextLine();
menu();
}
public void menu() {
System.out.println("Welcome to the text editor.\n"
+ "What do you want to do?\n"
+ "1. count characters"?;
int choice = scan.nextInt();
if (choice == 1) {
counting();
}
}
public void counting() {
System.out.println(text.length());
}
}
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
需要将其变成类字段:
class Editor {
private Scanner scan = new Scanner(System.in);
private String text = "";
public void copiedText() {
System.out.println("Paste your text here");
text = scan.nextLine();
menu();
}
public void menu() {
System.out.println("Welcome to the text editor.\n"
+ "What do you want to do?\n"
+ "1. count characters");
int choice = scan.nextInt();
if (choice == 1) {
counting();
}
}
public void counting() {
System.out.println(text.length());
}
}
另外,类TextEd
中的字段scan
似乎没有用途,应该被移除。
英文:
Yoy need to make it a class field:
class Editor {
private Scanner scan = new Scanner(System.in);
private String text = "";
public void copiedText() {
System.out.println("Paste your text here");
text = scan.nextLine();
menu();
}
public void menu() {
System.out.println("Welcome to the text editor.\n"
+ "What do you want to do?\n"
+ "1. count characters"?;
int choice = scan.nextInt();
if (choice == 1) {
counting();
}
}
public void counting() {
System.out.println(text.length());
}
}
Also field scan
in class TextEd
seems to have no purpose and should therefore be removed.
答案3
得分: 0
你的代码出现了编译错误,因为menu
方法中System.out.println()
的括号没有正确闭合。
你想在另一个方法中调用text
字符串的方法,所以你可以将text
传递给该方法,或者将其定义为类中的字段,以便其他方法可以访问该变量。
import java.util.Scanner;
public class TextEd {
Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
Editor editor = new Editor();
editor.copiedText();
}
}
class Editor {
Scanner scan = new Scanner(System.in);
String text;
public void copiedText() {
System.out.println("Paste your text here");
text = scan.nextLine();
menu();
}
public void menu() {
System.out.println("Welcome to the text editor.\n"
+ "What do you want to do?\n"
+ "1. count characters?");
int choice = scan.nextInt();
if (choice == 1) {
counting();
}
}
public void counting() {
System.out.println(text.length());
}
}
注意代码块,其他块或方法中定义的每个变量,在其他块或方法中都无法访问。
英文:
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.
import java.util.Scanner;
public class TextEd {
Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
Editor editor = new Editor();
editor.copiedText();
}
}
class Editor {
Scanner scan = new Scanner(System.in);
String text;
public void copiedText() {
System.out.println("Paste your text here");
text = scan.nextLine();
menu();
}
public void menu() {
System.out.println("Welcome to the text editor.\n"
+ "What do you want to do?\n"
+ "1. count characters?");
int choice = scan.nextInt();
if (choice == 1) {
counting();
}
}
public void counting() {
System.out.println(text.length());
}
}
be aware of blocks, every variable that is defined in other blocks or methods, is not reachable in other blocks or methods.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论