【问题标题】:Java string not showing when run [duplicate]运行时未显示Java字符串[重复]
【发布时间】:2014-11-18 21:55:36
【问题描述】:

我似乎无法弄清楚为什么当我运行它时字符串学院没有显示。我编译时没有错误。我究竟做错了什么?其他一切都在工作。这可能很容易解决,但我是新手,刚开始学习 Java。

import java.util.Scanner;
public class story_a_holloway{
public static void main(String[] args){
   String name;
   String city;
   int age;
   String college;
   String profession;
   String animal;
   String petname;

   Scanner keyboard = new Scanner(System.in);

   // Get name
   System.out.print("What is your name? ");
   name = keyboard.nextLine();

   // Get city
   System.out.print("What city do you live in? ");
   city = keyboard.nextLine();

   // Get age
   System.out.print("What is your age? ");
   age = keyboard.nextInt();

   // Get college
   System.out.print("What college do you attend? ");
   college = keyboard.nextLine();

   keyboard.nextLine();
   // Get profession
   System.out.print("What is your profession? ");
   profession = keyboard.nextLine();

   // Get animal
   System.out.print("What is your favorite animal? ");
   animal = keyboard.nextLine();

   // Get pet name
   System.out.print("What would you name your pet? ");
   petname = keyboard.nextLine();

   System.out.println("There once was a person named " + name + " who lived in " + city + ". At the age of " + age + ", " + name + " went to college at " + college + ". " + name + " graduated and went to work as a " + profession + ". Then " + name + " adopted a(n) " + animal + " named " + petname + ". They both lived happily ever after!");
}
}

【问题讨论】:

  • 你能提供一个示例输出吗?
  • 为什么在college = keyboard.nextLine() 之后有额外的keyboard.nextLine() 调用?

标签: java string


【解决方案1】:

age = keyboard.nextInt(); 之后调用keyboard.nextLine()

当前int 值被读取为年龄,college = keyboard.nextLine(); 读取包含您的int 的行的其余部分,该行是空的。所以正确的形式应该是:

// Get age
System.out.print("What is your age? ");
age = keyboard.nextInt();
keyboard.nextLine();

// Get college
System.out.print("What college do you attend? ");
college = keyboard.nextLine();

避免额外调用nextLine() 的其他可能解决方案是将整行读取为字符串,然后将该字符串解析为整数,例如:

age = Integer.parseInt(keyboard.nextLine());

【讨论】:

  • 您还可以使用两个Scanner 对象:一个用于字符串输入,一个用于数字输入。然而,这太过分了。这里的重要问题是要知道,为了重用Scanner 对象,它必须被完全消耗掉。如前所述,一些nextXXX()methods 会留下换行符。使用nextLine() 方法是清空输入流的一种解决方法,以便下次尝试获取下一个字符串时,它会按照预期的方式工作。
【解决方案2】:

在这行后面加上keyboard.nextLine()

int age=keyboard.nextInt();

这是在Scanner 类的nextInt() 方法之后使用nextLine() 方法时通常会发生的常见问题。

实际发生的情况是,当用户在int age = keyboard.nextInt(); 处输入一个整数时,扫描程序将只获取数字并留下换行符\n。所以你需要通过调用keyboard.nextLine(); 来做一个技巧,只是为了丢弃那个换行符,然后你可以毫无问题地调用String college = keyboard.nextLine();

这取自Reading Strings next() and nextLine() Java

【讨论】:

  • 如果您知道该问题与另一个问题重复,请不要重新发布其答案,而是将其标记为重复。
  • Pshemo 我实际上也将它标记为重复,奇怪的是它没有显示在问题下方的评论字段中。它之前确实说过 mrsimplemind 将其标记为重复和 url。无论如何感谢您的澄清。
【解决方案3】:

你的代码的问题是在 nextInt() 之后使用 nextLine() 并且你有一个额外的 nextLine()

// Get age
System.out.print("What is your age? ");
age = keyboard.nextInt();

// Get college
System.out.print("What college do you attend? ");
college = keyboard.nextLine();

keyboard.nextLine(); --> this line
// Get profession
System.out.print("What is your profession? ");
profession = keyboard.nextLine();

你应该做的是:

// Get age
System.out.print("What is your age? ");
age = keyboard.nextInt();
keyboard.nextLine(); -->add this line

// Get college
System.out.print("What college do you attend? ");
college = keyboard.nextLine();

// keyboard.nextLine(); --> remove this line
// Get profession
System.out.print("What is your profession? ");
profession = keyboard.nextLine();

一个更好的方法是:(阅读Integer.pasrseInt(String)

// Get age
System.out.print("What is your age? ");
age = Integer.parseInt(keyboard.nextLine());

【讨论】:

    【解决方案4】:
        import java.util.Scanner;
        public class story_a_holloway{
        public static void main(String[] args){
            String name;
            String city;
            int age;
            String profession;
            String animal;
            String petname;
            String college;
            Scanner keyboard = new Scanner(System.in);
    
            // Get name
            System.out.print("What is your name? ");
            name = keyboard.nextLine();
    
            // Get city
            System.out.print("What city do you live in? ");
            city = keyboard.nextLine();
    
            // Get age
            System.out.print("What is your age? ");
            age = Integer.parseInt(keyboard.nextLine());
    
            // Get college
            System.out.print("What college do you attend? ");
            college = keyboard.nextLine();
    
            // Get profession
            System.out.print("What is your profession? ");
            profession = keyboard.nextLine();
    
            // Get animal
            System.out.print("What is your favorite animal? ");
            animal = keyboard.nextLine();
    
            // Get pet name
            System.out.print("What would you name your pet? ");
            petname = keyboard.nextLine();
            System.out.println(college);
            System.out.println("There once was a person named " + name + " who lived in " + city + ".    At the age of " + age + ", " + name + " went to college at " + college + ". " + name + " graduated and went to work as a " + profession + ". Then " + name + " adopted a(n) " + animal + " named " + petname + ". They both lived happily ever after!");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多