【发布时间】:2014-05-25 23:47:45
【问题描述】:
我正在编写一个程序,它使用字符串的特定部分来写一个字母。
这是我目前所拥有的(我只是一个初学者)
import java.util.Scanner;
public class AutoInsurance {
public static void main (String[] args)
{
Scanner scan=new Scanner (System.in);
System.out.print("Enter Name:");
String Name;
Name=scan.nextLine();
System.out.print("Enter Street Address:");
String Address;
Address=scan.nextLine();
System.out.print("Enter city, state, and zip code:");
String Location;
Location=scan.nextLine();
System.out.println();
System.out.println();
System.out.println("Dear "+Name+",");
System.out.println(" You have been selected to receive this offer of auto insurance from");
System.out.println("Allspam Insurance Company! Drivers from "++" saved an average "); // I just want it to print the city here, between ++
// I will finish coding once I figure this out, but I'm stumped
}
}
【问题讨论】:
-
当你得到用户的输入时,为什么不把城市保存到不同的变量中
-
您必须根据输入方式拆分位置...我建议您为每个信息(城市/州/邮政编码)执行 scan.nextLine() 除非您建立明确的方式拆分字符串(分隔符),但用户可能会感到困惑并添加额外的不需要的字符
-
我需要将城市、州和邮编保存在同一个字符串中,否则我必须让它们输入三个不同的变量。有没有办法可以做到这一点,但保持相同的格式?
-
@CyborGamer 您是否尝试过类似 While(Scan.hasNext()) 并以这种方式分配变量?