Java扫描程序不返回多个单词变量

时间:2013-09-29 10:32:34

标签: java switch-statement java.util.scanner

我的代码的这一部分无法正常工作。我需要从A,B或X菜单中选择,然后输入一个小句子作为描述(多个)成本的数字。我可以完美地输入所有数据并返回菜单(仅在输入X时退出)。但是变量damageDesc的返回字符串只是抓住空格之前的第一个单词。我整晚都试过这个,仍然无法让它返回字符串中的所有单词。如果有人可以提供帮助,这是代码。谢谢!

do {
     System.out.println("Damage Repair / Traffic Infringement Data Entry "+ 
        "Menu");
     System.out.println("-------------------------------------------------"+
        "---");

     System.out.println();

     System.out.println("A - Record Damage Repair Details");
     System.out.println("B - Record Traffic Infringement Details");
     System.out.println("X - Exit");

     System.out.println();

     System.out.print("Enter your selection (A/B/X):");
     menuOption = console.next().charAt(0);

     switch (menuOption) {

        case 'A':
        case 'a':
           System.out.print("Enter description of damage repair: ");
           damageDesc = console.next();
           console.nextLine();
           System.out.print("Enter repair cost: ");
           damageCost= console.nextDouble();
           //Place damage description and cost on seperate lines 
           damageDescFinal = String.format(damageDescFinal + "- %s ($%.2f ) \n", damageDesc, damageCost);

           //Also could have used the below
           //damageDescFinal = (damageDescFinal+"- "+damageDesc+
           //   " ($"+damageCost+")"+"\n");

           //All damage costs added together 
           damageCostFinal = damageCostFinal + damageCost;
           System.out.println();
        break;


        case 'B':
        case 'b':
           System.out.print("Enter details of traffic infringement: ");
           fineDesc = console.next();
           console.nextLine();
           System.out.print("Enter traffic fine amount: ");
           fineCost = console.nextDouble();
           //Set fine description and cost on seperate lines
           fineDescFinal = String.format(fineDescFinal + "- %s ($%.2f ) \n", fineDesc, fineCost);

           //Also could have used the below
           //fineDescFinal = fineDescFinal+"- "+fineDesc+
           //   " ($"+fineCost+")"+"\n";
           //All fine costs added together 
           fineCostFinal = fineCostFinal + fineCost;
           System.out.println();
           break;

        case 'X':
        case 'x':
           //Exit the damage and fine menu
           System.out.print("Exiting data entry menu..."+"\n");
           System.out.println();
           break;

        default:
           //Error handling for invalid input
           System.out.print("***ERROR*** Invalid Selection!"+"\n");
           System.out.println();
           break;

     }
  } while (menuOption != 'X' && menuOption != 'x');

3 个答案:

答案 0 :(得分:1)

也许,您需要使用nextLine()来阅读整行,而不是next

答案 1 :(得分:0)

通过将Double值更改为字符串然后在输入完成后将它们转换回double来修复此问题。这解决了我遇到的扫描仪问题。这是最终的代码:

  do {
     System.out.println("Damage Repair / Traffic Infringement Data Entry "+ 
        "Menu");
     System.out.println("-------------------------------------------------"+
        "---");
     System.out.println();
     System.out.println("A - Record Damage Repair Details");
     System.out.println("B - Record Traffic Infringement Details");
     System.out.println("X - Exit");
     System.out.println();
     System.out.print("Enter your selection (A/B/X):");
     menuOption = console.next().charAt(0);

     //Switch to allow either upper or lower case menuOption
     switch (menuOption) {

        case 'A':
        case 'a':
           System.out.print("Enter description of damage repair: ");
           //Clear scanner
           console.nextLine();
           //Input damage description
           damageDesc = console.nextLine();
           System.out.print("Enter repair cost: ");
           //Input damage cost
           damageCost= console.nextLine();
           //Convert string fineCost back to a double
           //This was input as a String as scanner produces an error
           double doubleDamageCost = Double.parseDouble(damageCost);
           //Set damage description and cost on seperate lines for each input
           damageDescFinal = String.format(damageDescFinal + "- %s ($%.2f ) \n", damageDesc, doubleDamageCost);
           //Concatenate all fine costs for a final value 
           damageCostFinal = damageCostFinal + doubleDamageCost;
           System.out.println();
           break;

        case 'B':
        case 'b':
           System.out.print("Enter details of traffic infringement: ");
           //Clear scanner
           console.nextLine();
           //Input fine description
           fineDesc = console.nextLine();
           System.out.print("Enter traffic fine amount: ");
           //Input fine cost
           fineCost = console.nextLine();
           //Convert string fineCost back to a double
           //This was input as a String as scanner produces an error
           double doubleFineCost = Double.parseDouble(fineCost);
           //Set fine description and cost on seperate lines for each input
           fineDescFinal = String.format(fineDescFinal + "- %s ($%.2f ) \n", fineDesc, doubleFineCost);
           //Concatenate all fine costs for a final value
           fineCostFinal = fineCostFinal + doubleFineCost;
           System.out.println();
           break;

        case 'X':
        case 'x':
           //Exit the damage and fine menu
           System.out.print("Exiting data entry menu..."+"\n");
           System.out.println();
           break;

        default:
           //Error handling for invalid input
           System.out.print("***ERROR*** Invalid Selection!"+"\n");
           System.out.println();
           break;

     }
  //Exits menu loop and allow code to continue
  } while (menuOption != 'X' && menuOption != 'x');

答案 2 :(得分:0)

为什么不在这种情况下使用.ToLowerCase或.ToUperCase?通过这种方式,您可以获得更清晰的代码

do {
        System.out.println("Damage Repair / Traffic Infringement Data Entry "+
                "Menu");
        System.out.println("-------------------------------------------------"+
                "---");
        System.out.println();
        System.out.println("A - Record Damage Repair Details");
        System.out.println("B - Record Traffic Infringement Details");
        System.out.println("X - Exit");
        System.out.println();
        System.out.print("Enter your selection (A/B/X):");
        Scanner console = new Scanner(System.in);
        menuOption = console.next().toLowerCase().charAt(0);

        //Switch to allow either upper or lower case menuOption
        switch (menuOption) {


            case 'a':
                System.out.print("Enter description of damage repair: ");
                //Clear scanner
                console.nextLine();
                //Input damage description
                damageDesc = console.nextLine();
                System.out.print("Enter repair cost: ");
                //Input damage cost
                damageCost= console.nextLine();
                //Convert string fineCost back to a double
                //This was input as a String as scanner produces an error
                double doubleDamageCost = Double.parseDouble(damageCost);
                //Set damage description and cost on seperate lines for each input
                damageDescFinal = String.format(damageDescFinal + "- %s ($%.2f ) \n", damageDesc, doubleDamageCost);
                //Concatenate all fine costs for a final value 
                damageCostFinal = damageCostFinal + doubleDamageCost;
                System.out.println();
                break;

            case 'b':
                System.out.print("Enter details of traffic infringement: ");
                //Clear scanner
                console.nextLine();
                //Input fine description
                fineDesc = console.nextLine();
                System.out.print("Enter traffic fine amount: ");
                //Input fine cost
                fineCost = console.nextLine();
                //Convert string fineCost back to a double
                //This was input as a String as scanner produces an error
                double doubleFineCost = Double.parseDouble(fineCost);
                //Set fine description and cost on seperate lines for each input
                fineDescFinal = String.format(fineDescFinal + "- %s ($%.2f ) \n", fineDesc,              doubleFineCost);
                //Concatenate all fine costs for a final value
                fineCostFinal = fineCostFinal + doubleFineCost;
                System.out.println();
                break;

            case 'x':
                //Exit the damage and fine menu
                System.out.print("Exiting data entry menu..."+"\n");
                System.out.println();
                break;

            default:
                //Error handling for invalid input
                System.out.print("***ERROR*** Invalid Selection!"+"\n");
                System.out.println();
                break;

        }
        //Exits menu loop and allow code to continue
    } while (menuOption != 'x');

但你应该做更多的课程,比如

Public void menu(){

 System.out.println("Damage Repair / Traffic Infringement Data Entry "+ 
    "Menu");
 System.out.println("-------------------------------------------------"+
    "---");
 System.out.println();
 System.out.println("A - Record Damage Repair Details");
 System.out.println("B - Record Traffic Infringement Details");
 System.out.println("X - Exit");
 System.out.println();
 System.out.print("Enter your selection (A/B/X):");

}

等等,以获得更有条理和更干净的代码。 segementation是你最好的朋友。