-1

一部のデータを変更したいが、元の名前の一部を保持したい場合、Enter キーを押したときに次のユーザー入力にスキップするにはどうすればよいですか?

  for (int i = 0; i < myVector.size(); i++)
{
    System.out.println("Press Enter to skip to next input");
    System.out.print("Enter Name: ");
//If user presses Enter/newline, go to the next part where it asks for Name2

    Name = keyboard.nextLine();
    System.out.println("You've entered: " + Name);
    myVector.elementAt(i).setName(Name);

    System.out.println("Press Enter to skip to next input");
    System.out.print("Enter Name2: ");

    Name2 = keyboard.nextLine();
    System.out.println("You've entered: " + Name2);
    myVector.elementAt(i).setName(Name2);

//...etc
}
4

3 に答える 3

1

continue キーワードを使用します (オラクルのドキュメントから)。

于 2012-09-23T18:23:36.807 に答える
1
loop: for (int i = 0; i < myVector.size(); i++)
{
    Scanner sc = new Scanner(System.in);
    if(user presses enter) {
        System.out.println("Press Enter to skip to next input");
        System.out.print("Enter Name: ");
        //If user presses Enter/newline, go to the next part where it asks for Name2

        continue loop;
    }    
    else {
        Name = keyboard.nextLine();
        System.out.println("You've entered: " + Name);
        myVector.elementAt(i).setName(Name);

        System.out.println("Press Enter to skip to next input");
        System.out.print("Enter Name2: ");

        Name2 = keyboard.nextLine();
        System.out.println("You've entered: " + Name2);
        myVector.elementAt(i).setName(Name2);

        //...etc
    }
}
于 2012-09-23T18:24:34.590 に答える
1
Name = keyboard.nextLine();
if (!Name.isEmpty()) {
  System.out.println("You've entered: " + Name);
  myVector.elementAt(i).setName(Name);
}

つまり、Nameが空の場合は、 にスキップしName2ます。

于 2012-09-23T18:26:43.983 に答える