首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Java -用户输入错误中创建MadLib

在Java -用户输入错误中创建MadLib
EN

Stack Overflow用户
提问于 2017-02-02 16:29:09
回答 1查看 999关注 0票数 0

我对Java非常陌生,我正在尝试创建一个madlib,它接受用户输入并将其插入句子中。到目前为止,我运气不好。当我运行程序时,它会提示我输入第一个输入,但之后它会出错。你知道为什么吗?我认为这与数据类型(int、string等)有关。

附加问题:一旦插入段落,我将如何使输入显示为粗体?假设我一开始就能让这段代码正常工作。

这是我的代码:

代码语言:javascript
复制
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.");
   }
}
EN

回答 1

Stack Overflow用户

发布于 2017-02-24 17:58:14

您有此错误,因为

代码语言:javascript
复制
firstName, childAge, colorToy, typeToy

都不应该是整数。它们应该是你应该做的字符串

代码语言:javascript
复制
firstName = input.nextLine();
childAge = input.nextLine();
colorToy = input.nextLine();
typeToy = input.nextLine();

而且,这不是你的问题的一部分,但是,当你这样做的时候

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42007402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档