我试图创建一个程序,(1)提示输入用户的全名,然后生成用户名; (2)提示输入一个数字,然后确定该数字是奇数还是偶数。我写出了用户名和奇数/偶数类的代码,并希望从主类中调用它们。但是,当从主类调用时,用户名方法在生成用户名之前提示用户两次,而奇数/偶数方法实际上并不确定用户输入的数字是奇数还是偶数。当我从用户名类中删除扫描仪对象时,我得到一个越界编译错误,因此我被迫将其重新插入,以便程序运行。我应该使用return语句吗?
用户名
/**
* Class to generate the username based on user's first name and randomly generated numbers
*/
public void username()
{
Scanner inputReader = new Scanner(System.in);
String fullName = inputReader.nextLine();
// create random object and variable to store it in
Random randomizer = new Random();
int randomNumber = randomizer.nextInt(1000);
// create variable to store lowercase username
String lowercase = (fullName.toLowerCase());
// create string variable to format username to first three characters in lowercase
String firstThreeLetters = (lowercase.substring(0, 3));
// concatenate lowercase characters and random number
String usernameFinal = (firstThreeLetters + randomNumber);
// print out final username
System.out.println("Your username is " + usernameFinal);
}
奇/偶
/**
* Class to determine if a user inputted value is odd or even
*/
public void OddEven1()
{
Scanner inputReader = new Scanner(System.in);
int userInteger = 0;
// if/else to determine if number is odd or even
if (userInteger % 2 == 0)
{
System.out.println(userInteger + " is an even number.");
}
else
{
System.out.println(userInteger + " is an odd number.");
}
}
主要方法
{
/**
* This class holds the main method through which all other classes are run.
*/
public static void main(String[] args)
{
// create objects
Username usernameGenerator = new Username();
OddEven oddeven = new OddEven();
Scanner inputReader = new Scanner(System.in);
// prompt for real name and print username
System.out.print("Name: ");
String fullName = inputReader.nextLine();
usernameGenerator.username();
// prompt for number
System.out.print("Give me a number: ");
// variable to store value
int userInteger = inputReader.nextInt();
oddeven.OddEven1();
}
答案 0 :(得分:1)
您应该更改以下代码
在main方法中
System.out.print("Give me a number: ");
// variable to store value
int userInteger = inputReader.nextInt();
oddeven.OddEven1(userInteger );
奇/偶
public void OddEven1(int userInteger )
{
// if/else to determine if number is odd or even
if (userInteger % 2 == 0)
{
System.out.println(userInteger + " is an even number.");
}
else
{
System.out.println(userInteger + " is an odd number.");
}
}
现在让我们讨论用户名。您已从主方法中捕获了用户名。所以你不需要再次从用户那里得到它。
String fullName = inputReader.nextLine();
usernameGenerator.username(fullName );
public void username(String fullName )
{
//Your logic
}
答案 1 :(得分:1)
1 - 您要求用户的名字两次,一次在这里
String fullName = inputReader.nextLine();
一个在这里
Scanner inputReader = new Scanner(System.in);
String fullName = inputReader.nextLine();
我建议保留第一种方法并将fullName
传递给username()
函数。举个例子:
/**
* Class to generate the username based on user's first name and
randomly generated numbers
*/
public void username(fullName)
{
// create random object and variable to store it in
Random randomizer = new Random();
int randomNumber = randomizer.nextInt(1000);
// create variable to store lowercase username
String lowercase = (fullName.toLowerCase());
// create string variable to format username to first three characters in lowercase
String firstThreeLetters = (lowercase.substring(0, 3));
// concatenate lowercase characters and random number
String usernameFinal = (firstThreeLetters + randomNumber);
// print out final username
System.out.println("Your username is " + usernameFinal);
}
2 - 您在第二个函数OddEven1()
中执行相同的操作。我建议也将参数传递给它。作为一个例子:
public void OddEven1(number)
{
int userInteger = number;
// if/else to determine if number is odd or even
if (userInteger % 2 == 0)
{
System.out.println(userInteger + " is an even number.");
}
else
{
System.out.println(userInteger + " is an odd number.");
}
}
3 - 所以你的主要功能变为:
public static void main(String[] args)
{
// create objects
Username usernameGenerator = new Username();
OddEven oddeven = new OddEven();
Scanner inputReader = new Scanner(System.in);
// prompt for real name and print username
System.out.print("Name: ");
String fullName = inputReader.nextLine();
usernameGenerator.username(fullName);
// prompt for number
System.out.print("Give me a number: ");
// variable to store value
int userInteger = inputReader.nextInt();
oddeven.OddEven1(userInteger);
}