如何在另一种方法中使用来自一种方法的用户输入字符串?
How can I use a user input String from one method in another?
本程序的这一点是将用户名的字符长度限制为 20 个字符。它是目前仅包含 Main 方法的较大程序的一部分。为了清理和阐明我的代码,我想将各种功能分成不同的方法。
目前,我正在尝试设置 class 变量,以便它们可以在多种方法中使用。这是我目前所拥有的:
public class Program
{
Scanner read = new Scanner(System.in);
String firstName = read.nextLine();
String lastName = read.nextLine();
public void main(String[] args) {
domainCharLimit();
}
public void domainCharLimit() {
String firstNameNew = firstName.replace("'", "");
String lastNameNew = lastName.replace("'", "");
String domainUsername = firstNameNew + "." + lastNameNew;
if (domainUsername.length()>20) {
String cutName = domainUsername.substring(0, 20);
domainUsername = cutName;
}
System.out.print(domainUsername);
}
}
我尝试将一个或两个方法设置为静态,但未能解决问题。在这种状态下,当 运行 时,程序不会 return 报错,而是给出“无输出”
Main 方法必须是静态的!它是您程序的入口,它的签名必须是这样的。
为了在其中调用非静态方法,您需要实例化一个对象并在该对象上调用它。在你的情况下是
public static void main(String[] args) {
Program p = new Program();
p.domainCharLimit();
}
首先: Main Method 应该总是static.
第二个: 因为你是从 Main(static)
调用 domainChatLimit()
所以它也应该是静态的
第三:因为你在静态方法中使用了firstName
、lastName
属性domainChatLimit()
那么它们也应该是静态的
第四: Scanner
应该也是静态的,因为你用它来得到 firstName
, lastName
并且它们都是静态的。
注意:不需要定义这个class的新实例来调用内部方法
解决方案如下(测试成功):
import java.util.Scanner;
public class Program{
// variables below should be defined as static because they are used in static method
static Scanner read = new Scanner(System.in);
static String firstName = read.nextLine();
static String lastName = read.nextLine();
// Main method is static
public static void main(String[] args) {
//There is no need to define a new instance of this class to call an internal method
domainCharLimit();
}
// calling from main static method so it should be static
public static void domainCharLimit() {
String firstNameNew = firstName.replace("'", "");
String lastNameNew = lastName.replace("'", "");
String domainUsername = firstNameNew + "." + lastNameNew;
if (domainUsername.length()>20) {
String cutName = domainUsername.substring(0, 20);
domainUsername = cutName;
}
System.out.print(domainUsername);
}
}
如果您想为该功能创建通用实用程序,您可以执行以下逻辑:
PogramUtil.java
import java.util.Scanner;
public class ProgramUtil {
Scanner read = new Scanner(System.in);
String firstName = read.nextLine();
String lastName = read.nextLine();
public void domainCharLimit() {
String firstNameNew = firstName.replace("'", "");
String lastNameNew = lastName.replace("'", "");
String domainUsername = firstNameNew + "." + lastNameNew;
if (domainUsername.length()>20) {
String cutName = domainUsername.substring(0, 20);
domainUsername = cutName;
}
System.out.print(domainUsername);
}
}
现在你可以这样调用了:
Program.java
public class Program{
// Main method is static
public static void main(String[] args) {
ProgramUtil programUtil = new ProgramUtil();
programUtil.domainCharLimit();
}
}
本程序的这一点是将用户名的字符长度限制为 20 个字符。它是目前仅包含 Main 方法的较大程序的一部分。为了清理和阐明我的代码,我想将各种功能分成不同的方法。
目前,我正在尝试设置 class 变量,以便它们可以在多种方法中使用。这是我目前所拥有的:
public class Program
{
Scanner read = new Scanner(System.in);
String firstName = read.nextLine();
String lastName = read.nextLine();
public void main(String[] args) {
domainCharLimit();
}
public void domainCharLimit() {
String firstNameNew = firstName.replace("'", "");
String lastNameNew = lastName.replace("'", "");
String domainUsername = firstNameNew + "." + lastNameNew;
if (domainUsername.length()>20) {
String cutName = domainUsername.substring(0, 20);
domainUsername = cutName;
}
System.out.print(domainUsername);
}
}
我尝试将一个或两个方法设置为静态,但未能解决问题。在这种状态下,当 运行 时,程序不会 return 报错,而是给出“无输出”
Main 方法必须是静态的!它是您程序的入口,它的签名必须是这样的。
为了在其中调用非静态方法,您需要实例化一个对象并在该对象上调用它。在你的情况下是
public static void main(String[] args) {
Program p = new Program();
p.domainCharLimit();
}
首先: Main Method 应该总是static.
第二个: 因为你是从 Main(static)
调用 domainChatLimit()
所以它也应该是静态的
第三:因为你在静态方法中使用了firstName
、lastName
属性domainChatLimit()
那么它们也应该是静态的
第四: Scanner
应该也是静态的,因为你用它来得到 firstName
, lastName
并且它们都是静态的。
注意:不需要定义这个class的新实例来调用内部方法
解决方案如下(测试成功):
import java.util.Scanner;
public class Program{
// variables below should be defined as static because they are used in static method
static Scanner read = new Scanner(System.in);
static String firstName = read.nextLine();
static String lastName = read.nextLine();
// Main method is static
public static void main(String[] args) {
//There is no need to define a new instance of this class to call an internal method
domainCharLimit();
}
// calling from main static method so it should be static
public static void domainCharLimit() {
String firstNameNew = firstName.replace("'", "");
String lastNameNew = lastName.replace("'", "");
String domainUsername = firstNameNew + "." + lastNameNew;
if (domainUsername.length()>20) {
String cutName = domainUsername.substring(0, 20);
domainUsername = cutName;
}
System.out.print(domainUsername);
}
}
如果您想为该功能创建通用实用程序,您可以执行以下逻辑:
PogramUtil.java
import java.util.Scanner;
public class ProgramUtil {
Scanner read = new Scanner(System.in);
String firstName = read.nextLine();
String lastName = read.nextLine();
public void domainCharLimit() {
String firstNameNew = firstName.replace("'", "");
String lastNameNew = lastName.replace("'", "");
String domainUsername = firstNameNew + "." + lastNameNew;
if (domainUsername.length()>20) {
String cutName = domainUsername.substring(0, 20);
domainUsername = cutName;
}
System.out.print(domainUsername);
}
}
现在你可以这样调用了:
Program.java
public class Program{
// Main method is static
public static void main(String[] args) {
ProgramUtil programUtil = new ProgramUtil();
programUtil.domainCharLimit();
}
}