Подтвердить что ты не робот

Как ввести предложение в Java

Код, который я написал, принимает в качестве входных данных только одну строку, а не целое предложение, и я хочу, чтобы в качестве ввода было принято целое предложение:

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int i; 
        i= scan.nextInt();
        double d;
        d=scan.nextDouble();
        String s;
        s=scan.next();
        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}

Тест-сценарий - "Добро пожаловать в Java", и он просто показывает "Приветствие" на выходе. Все остальное работает нормально. Пожалуйста помоги.

4b9b3361

Ответ 1

вы можете использовать scan.nextLine(); для чтения всей строки.

Ответ 2

Вы можете попробовать следующее, это будет работать.

public static void main(String args[]) {    
        // Create a new scanner object
        Scanner scan = new Scanner(System.in); 

        // Scan the integer which is in the first line of the input
        int i = scan.nextInt(); 

        // Scan the double which is on the second line
        double d = scan.nextDouble(); 

        // At this point, the scanner is still on the second line at the end
           of the double, so we need to move the scanner to the next line

        // scans to the end of the previous line which contains the double
        scan.nextLine();    

        // reads the complete next line which contains the string sentence            
        String s = scan.nextLine();    

        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
  }

Ответ 3

Вы должны поставить scan.nextLine() после указанного выше целочисленного сканирования и двойного сканирования. Затем используйте String s = scan.nextLine(). Вот так,

int i = scan.nextInt();
scan.nextLine();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();

Ответ 4

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();
        double d = scan.nextDouble();
        String s = scan.nextLine();
        s = scan.nextLine(); 
        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}