2017-06-15 111 views
1

我正在做一项任务,遇到一堵墙,我无法过去。无法弄清楚如何将字符串转换为int?

我无法弄清楚如何接受这个用户选择来继续我的代码。

也许我在思考,但我很茫然,在这个时间太长了笑

import java.util.Scanner; 
import java.util.Arrays; 

    public class DestinationPlanner { 

static Scanner keyboard = new Scanner(System.in); 
static int cityDistance, arrivalCity; 
static double gasCost, MPG, totalCost; 
static String userContinue, userSelection, departCity; 

static double [][] cityTable = { 
    {0, 384, 706, 729, 744, 767, 605, 548, 492, 346}, 
    {384, 0, 399, 628, 437, 473, 291, 714, 390, 244}, 
    {706, 399, 0, 877, 47, 605, 110, 1086, 742, 640}, 
    {729, 628, 877, 0, 914, 384, 825, 395, 259, 406}, 
    {744, 437, 47, 914, 0, 609, 154, 1123, 780, 685}, 
    {767, 473, 605, 384, 609, 0, 563, 708, 367, 420}, 
    {605, 291, 110, 825, 154, 563, 0, 1002, 679, 533}, 
    {548, 714, 1086, 395, 1123, 708, 1002, 0, 344, 469}, 
    {492, 390, 742, 259, 780, 367, 679, 344, 0, 146}, 
    {346, 244, 640, 406, 685, 420, 533, 469, 146, 0}, 
}; 

static String [] citySelection = {"Jacksonville, Fl", "Charlotte, NC", "Washington, DC", "Memphis, TN", 
    "Baltimore, MD", "Louisville, KY", "Richmond, VA", "New Orleans, LA", "Birmingham, AL", "Atlanta, GA" 
}; 

public static void main(String[] args) { 
    System.out.println("Welcome to the Southeast Destination Planner!" 
      + "\nTo help you plan your trip this program will calculate" 
      + "\nthe cost to travel between two popular Southeast cities" 
      + "\nWill you continue?" 
      + "\nEnter 'y' to continiue 'n' to quit"); 
    userContinue = keyboard.nextLine().toLowerCase();  

    while ((userContinue.equals("y")) & (userContinue.equals("n"))){ 
     userContinue = keyboard.nextLine().toLowerCase(); 
    } 
    while (userContinue.equals("n")) { 
     System.out.println("The program will now terminate"); 
     System.exit(0); 
    } 

    while (userContinue.equals("y")) { 

     citySelection(); 
     carInfo(); 
     finalCost(); 

    } 

} 

private static void citySelection() { 
    System.out.println("\nGreat, let's start by determining the city you will be departing"); 
    System.out.println("\nPlease choose from the following cities"); 
    System.out.println("[0] Jacksonville, FL"); 
    System.out.println("[1] Charlotte, NC"); 
    System.out.println("[2] Washington, DC"); 
    System.out.println("[3] Memphis, TNA"); 
    System.out.println("[4] Baltimore, MD"); 
    System.out.println("[5] Louisville, KY"); 
    System.out.println("[6] Richmond, VA"); 
    System.out.println("[7] New Orleans, LA"); 
    System.out.println("[8] Birmingham, AL"); 
    System.out.println("[9] Atlanta, GA"); 

    System.out.println("\nWhere will your trip begin?"); 
    departCity = keyboard.nextLine(); 

    while (!(departCity.equals("1")) && !(departCity.equals("2")) && !(departCity.equals("3")) && 
      !(departCity.equals("4")) && !(departCity.equals("5")) && !(departCity.equals("6")) && 
       !(departCity.equals("7")) && !(departCity.equals("8")) && !(departCity.equals("9")) && 
         !(departCity.equals("0"))){ 

     System.out.println("Please enter a number 0 through 9"); 

    departCity = keyboard.nextLine(); 
    } 
    System.out.println("\nYou have chosen to depart from " + citySelection[departCity]); 



} 

回答

2

您可以使用Integer.parseInt()解析为Integer对象。之后,autoboxing应该照顾它,但是你会想要寻找的情况下,你得到一个null或0.你也可以然后检查数字是在范围内,而不是你的if语句有9个不同的条件。

+0

或'Integer.valueOf'如果你想'Integer'而不是'Integer.parseInt'返回的'int' – alfasin

+1

谢谢,谢谢!这固定它!对此,我真的非常感激。 –

3

最好的办法是代替keyboard.nextLine的用扫描仪已经开始() 。它有一个.nextInt()右建造它,但它需要在一个try catch来包围(以防万一输入是不是数字)

Scanner s = new Scanner(System.in); 
    int result = s.nextInt();