我对Java非常陌生,我正在尝试创建一个用户输入并将其插入句子的madlib。到目前为止,我没有太多运气。当我运行程序时,它会提示我输入第一个输入,但之后会出错。你知道为什么吗?我认为它与数据类型(int,string等)有关。
额外问题:如果插入段落后,如何将输入显示为粗体?假设我可以在第一时间使用此代码。
这是我的代码:
import java.util.Scanner;
public class Madlib
{
public static void main(String[] args)
{
int firstName, childAge, colorToy, typeToy;
Scanner input = new Scanner(System.in);
System.out.println("Enter someone's first name:");
firstName = input.nextInt();
System.out.println("Enter a child's age:");
childAge = input.nextInt();
System.out.println("Enter a color:");
colorToy = input.nextInt();
System.out.println("Enter a toy:");
typeToy = input.nextInt();
System.out.println("\"I am" + childAge + "years old, so that means I get to have the" + colorToy + typeToy + "!\" exclaimed the little girl.");
System.out.println("\"Share with your sister,\"" + firstName + "grovelled, barely peering over their large, Sunday newspaper.");
}
}
答案 0 :(得分:0)
您有此错误,因为
firstName, childAge, colorToy, typeToy
应该都不应该是一个整数。它们应该是String你应该做的
firstName = input.nextLine();
childAge = input.nextLine();
colorToy = input.nextLine();
typeToy = input.nextLine();
此外,这不是你问题的一部分;但是,当你这样做时
System.out.println("\"I am " + childAge + " years old, so that means I get to have the " + colorToy + " " + typeToy + "!\" exclaimed the little girl.");
System.out.println("\"Share with your sister,\"" + " " + firstName + " grovelled, barely peering over their large, Sunday newspaper.");
看起来应该看起来像那些没有它们的地方包含空格。我希望这能回答你的问题!