![](/img/trans.png)
[英]WAP to input a sentence and print how many times “is” appears in that sentence [Java]
[英]How to input a sentence in 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);
}
}
你可以使用scan.nextLine();
閱讀整行。
您可以嘗試以下方法,它會起作用。
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);
}
您應該在上述整數掃描和雙重掃描之后放置一個scan.nextLine()
。 然后使用String s = scan.nextLine()
。 像這樣,
int i = scan.nextInt();
scan.nextLine();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();
試試下面的代碼,這是工作代碼,我嘗試過使用 IntelliJ 和像 Hacker Rank 這樣的在線編譯器。
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d=scan.nextDouble();
scan.nextLine();
String s=scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
在您的代碼中,您正在使用next()
。 這是用於輸入一個單詞。
您可以使用nextLine()
在 java 中輸入句子。
例子:
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s;
s=scan.nextLine();
System.out.println("String: " + s);
}
}
但是上面的代碼(問題)你正在接受三個輸入。 在nextDouble()
之后使用nextLine()
nextDouble()
。 在這種情況下, nextLine()
將 input 作為double input的換行符讀取。 所以輸出字符串為空。 為了避免這種情況,在nextDouble()
之后使用另一個nextLine()
nextDouble()
。 這個nextLine()
讀取雙重輸入的換行符, nextLine()
讀取句子。
所以試試這個:
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();
scan.nextLine();
String s = scan.nextLine();
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
謝謝...!
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//For input a Integer
int i = scan.nextInt();
//For input a Double
double d = scan.nextDouble();
//For input a String
scan.nextLine(); //For using to get the input string that was skipped of the Scanner object.(Use it when scan a string after scanning different type of variables.)
String s = scan.nextLine();
//For print String, Double and Integer variable
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();
// Write your code here.
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
這將正常工作。
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int c = scan.nextInt();
Double b = scan.nextDouble();
scan.nextLine();
String a = scan.nextLine();
System.out.println("String: "+a);
System.out.println("Double: "+b);
System.out.println("Int: "+c);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.