当我使用nextInt()时,它会跳过下一行。

huangapple go评论78阅读模式
英文:

when I use nextInt() it skip on the next line

问题

以下是翻译好的内容:

我正在尝试Java中的对象基础,但在尝试时遇到了这个问题。

我有一个Dogs类

public class Dogs {
    private String Name;
    private int Age;
    private String Color;
    private String Owner;
    
    public Dogs() {
        this.Name = "Rex";
        this.Age = 5;
        this.Color = "black";
        this.Owner = "John";
    }

    public Dogs(String name, int age, String color, String owner) {
        this.Name = name;
        this.Age = age;
        this.Color = color;
        this.Owner = owner;
    }

    // 所有的getter和setter
}

还有Main类

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Dogs my_dog = new Dogs();
        System.out.println("狗的名字是什么?");
        my_dog.setName(input.next());
        System.out.println("狗的年龄是多少?");
        my_dog.setAge(input.nextInt());
        System.out.println("狗的颜色是什么?");
        my_dog.setColor(input.nextLine());
        System.out.println("主人的名字是什么?");
        my_dog.setOwner(input.nextLine());
    }
}

当我运行它时,它很好地打印出了前两个问题,但然后就跳过了下一个问题...

这是结果:

狗的名字是什么?
pil
狗的年龄是多少?
7
狗的颜色是什么?

主人的名字是什么?

我该如何修复它?

英文:

I',m trying the basic of objects in java but I got this problem when I try

I have Dogs class

public class Dogs {
    private String Name;
    private int Age;
    private String Color;
    private String Owner;
    public Dogs() {
        this.Name = "Rex";
        this.Age = 5;
        this.Color = "black";
        this.Owner = "John";
    }

    public Dogs(String name, int age, String color, String owner) {
        this.Name = name;
        this.Age = age;
        this.Color = color;
        this.Owner = owner;
    }

//all the getters and setters

and Main class

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Dogs my_dog = new Dogs();
        System.out.println("What is the dog's name? ");
        my_dog.setName(input.next());
        System.out.println("What is the dog's age? ");
        my_dog.setAge(input.nextInt());
        System.out.println("What is the dog's color? ");
        my_dog.setColor(input.nextLine());
        System.out.println("What is the owner's name? ");
        my_dog.setOwner(input.nextLine());
    }
}

when I run it, it prints the two questions fine but than it skip on the next...

this is the resault:

What is the dog's name? 
pil
What is the dog's age? 
7
What is the dog's color? 
What is the owner's name?

how can I fix it?

答案1

得分: 2

请在读取年龄(整数值)后添加 input.nextLine()。

具体细节可参考此链接

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Dogs my_dog = new Dogs();
        System.out.println("What is the dog's name? ");
        my_dog.setName(input.next());
        System.out.println("What is the dog's age? ");
        my_dog.setAge(input.nextInt());
        // 添加这一行
        input.nextLine();
        System.out.println("What is the dog's color? ");
        my_dog.setColor(input.nextLine());
        System.out.println("What is the owner's name? ");
        my_dog.setOwner(input.nextLine());
    }
}
英文:

Please add input.nextLine() after reading the age i.e integer value.

for details refer this

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Dogs my_dog = new Dogs();
        System.out.println("What is the dog's name? ");
        my_dog.setName(input.next());
        System.out.println("What is the dog's age? ");
        my_dog.setAge(input.nextInt());
        // add this line
        input.nextLine()
        System.out.println("What is the dog's color? ");
        my_dog.setColor(input.nextLine());
        System.out.println("What is the owner's name? ");
        my_dog.setOwner(input.nextLine());
    }
}

答案2

得分: 0

使用input.next()而不是input.nextLine()将会解决这个问题。

nextLine()方法会返回在你使用nextInt()后按下回车键跳过的那一行内容,这是因为nextInt()并不消耗掉回车键输入的换行符。

public static void main(String... args) {

    Scanner input = new Scanner(System.in);
    Dogs my_dog = new Dogs();
    System.out.println("What is the dog's name? ");
    my_dog.setName(input.next());
    System.out.println("What is the dog's age? ");
    my_dog.setAge(input.nextInt());
    System.out.println("What is the dog's color? ");
    my_dog.setColor(input.next());
    System.out.println("What is the owner's name? ");
    my_dog.setOwner(input.next());

}
英文:

Using input.next() instead of input.nextLine() will fix this problem.

nextLine() method returns the line that was skipped by nextInt(), when you pressed the ENTER button after you introduced the dog's age.

public static void main(String... args) {

    Scanner input = new Scanner(System.in);
    Dogs my_dog = new Dogs();
    System.out.println("What is the dog's name? ");
    my_dog.setName(input.next());
    System.out.println("What is the dog's age? ");
    my_dog.setAge(input.nextInt());
    System.out.println("What is the dog's color? ");
    my_dog.setColor(input.next());
    System.out.println("What is the owner's name? ");
    my_dog.setOwner(input.next());

}

huangapple
  • 本文由 发表于 2020年5月3日 17:08:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/61572036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定