我对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.");
}
}发布于 2017-02-24 17:58:14
您有此错误,因为
firstName, childAge, colorToy, typeToy都不应该是整数。它们应该是你应该做的字符串
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.");它应该是这样的,在你没有它们的地方包含了一些空格。我希望这回答了你的问题!
https://stackoverflow.com/questions/42007402
复制相似问题