gpt4 book ai didi

java - java中如何输入字符串?

转载 作者:行者123 更新时间:2023-12-02 01:19:12 25 4
gpt4 key购买 nike

我知道我必须使用 Scanner 类首先创建扫描仪对象来接收 java 中的用户输入,尽管我不知道如何将多个单词输入到一个字符串变量中。例如,我这里有这样的代码:

System.out.println("Please enter the following information");

System.out.print("Author:");
author = input.nextLine();

System.out.print("Title:");
title = input.nextLine();

System.out.print("Grade Level:");
gradeLevel = input.nextDouble();


System.out.print("Pages:");
pages = input.nextDouble();

根据许多论坛的说法,应该允许用户将整行输入到 String 变量中,但它完全跳过了作者提示。我做错了什么?

编辑:我添加了下面的完整循环:

do {

System.out.printf("What kind of book is this?\n"
+ "(0 = Textbook, 1 = Novel, 2 = Biography, 3 = Author Information, 4 = Other)");

switch(input.nextInt()) {
case 0:
System.out.println("Please enter the following information.");

System.out.print("Author:");
author = input.nextLine();

System.out.print("Title:");
title = input.nextLine();

System.out.print("Grade Level:");
gradeLevel = input.nextDouble();
input.readLine();

System.out.print("Pages:");
pages = input.nextDouble();

//Creation of textBooks
texts[i] = new Textbook(gradeLevel, author, pages, title);

boolText = true;
break;
case 1:
System.out.print("Please enter the following information.\n");

System.out.print("Author:");
author = input.nextLine();
System.out.print("Genre:");
genre = input.next();
System.out.print("Title:");
title = input.nextLine();
System.out.print("Pages:");
pages = input.nextDouble();

//Creation of Novels
novels[i] = new Novel(genre, title, author, pages);

boolNovel = true;
break;
case 2:
System.out.print("Please enter the following information.\n");

System.out.print("Author:");
author = input.nextLine();
System.out.print("Title:");
title = input.nextLine();
System.out.print("Subject:");
subject = input.nextLine();
System.out.print("Pages:");
pages = input.nextDouble();

//Creation of Biographies
bio[i] = new Biography(subject, title, author, pages);

boolBiography = true;
break;
case 3:
System.out.print("Please enter the following information.\n");

System.out.print("Author:");
author = input.nextLine();
System.out.print("Email:");
email = input.next();
System.out.print("Address:");
address = input.nextLine();
System.out.print("Number of Titles:");
numOfTitles = input.nextDouble();

//Creation of Author Infortmation
authors[i] = new Author(author, address, email, numOfTitles);

boolAuthor = true;
break;
case 4:
System.out.println("Please enter the following information.");

System.out.print("Title:");
title = input.nextLine();
System.out.print("Author:");
author = input.nextLine();
System.out.print("Pages:");
pages = input.nextDouble();

//Creation of Author Infortmation
other[i] = new Books(title, author, pages);

boolOther = true;
default:
System.out.println("Program will now terminate.");
System.exit(1);
}

i++;
}
while (i < t);

最佳答案

通常这确实有效,但您很可能在循环中使用此代码。由于 nextDouble 不使用换行符,因此您需要显式使用该字符

pages = input.nextDouble();
input.readLine() // added - consumes newline

关于java - java中如何输入字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22071259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com