为什么我能';我不能用Java扫描器读我的台词
如何使用为什么我能';我不能用Java扫描器读我的台词,java,Java,如何使用Scanner类读取包含空格的字符串,例如“Bolt Penguin”?我需要删除空白吗? 但我不能,我必须保留两个字符之间的空格。以下是我创建的程序: import java.util.Scanner; public class Shop { public static void main(String args[]){ Scanner You = new Scanner(System.in); //Your Money int mone
Scanner
类读取包含空格的字符串,例如“Bolt Penguin”
?我需要删除空白吗?
但我不能,我必须保留两个字符之间的空格。以下是我创建的程序:
import java.util.Scanner;
public class Shop {
public static void main(String args[]){
Scanner You = new Scanner(System.in);
//Your Money
int money;
String name;
//Your Cards
String Cards;
//Shop (M) Cards
int Bolt_Penguin = 100;
int Eldeen = 95;
int Jinn = 1020;
//Shop (S) Cards
int Dark_Energy = 500;
int Horn_Unicorn = 640;
int Sparks = 90;
//Shop (T) Cards
int Hidden_S = 800;
... //You don't have to see the whole program
name = You.next();
if(name.equals("Bolt Penguin")){ //Here it is Bolt (space) Penguin
System.out.println("Seller: That'll cost " + Bolt_Penguin);
}else if(name.equals("Eldeen")){
System.out.println("Seller: That'll cost " + Eldeen);
}else if(name.equals("Jinn the Mystical Genie of the Lamp")){
System.out.println("Seller: That'll cost " + Jinn);
}else{
System.out.println("Seller: We don't have that Card yet! ");
}
...
我需要做些不同的事情吗?使用:
name = You.nextLine();
用空格通读整行
您可能还希望使用equalsIgnoreCase
更改equals
,但这取决于您的要求:
name = You.nextLine();
if(name.equalsIgnoreCase("Bolt Penguin")) {
System.out.println("Seller: That'll cost " + Bolt_Penguin);
}
替换
name = You.next();
与
替换
name=You.next()代码>
借
name=You.nextLine()代码>
我用输入Bolt Penguin
我的输出是=卖方:这将花费100
请发布错误跟踪。“您不必查看整个程序”-不,但我们希望看到一个简短但完整的程序演示问题,以及对该问题的良好描述。定义“不起作用”你有什么迹象表明出现了问题?如果它不起作用,你需要修复它。你能至少公布你得到的错误是什么吗。
name = You.nextLine();