2014-10-11 8 views
0

Я действительно застрял на этом. Мне нужно создать меню добавления плана сотового телефона. Затем мне нужно написать сегмент кода Java, который отобразит меню этих параметров и цикл, чтобы пользователь мог выбрать нужные параметры, пока пользователь не введет -1. Это то, что я до сих пор:Использование дозорного цикла While while в Java, чтобы добавить дополнительные параметры плана сотового телефона

import java.util.Scanner; 


public class CellPhone { 

public static void main(String[] args) { 
    //new array 
    String [] plans = new String[4]; 

    plans[0] = "a. 200 or less text messages a month: $5.00"; 
    plans[1] = "b. Additional line: $9.99"; 
    plans[2] = "c. International calling: $3.99"; 
    plans[3] = "d. Early nights and weekends: $16.99"; 

    System.out.println("Here are your plan options: "); 

    //outputs the contents of the array 
    for(int i = 0; i < 4; i++) { 
     System.out.println(plans[i]); 
    } 

    // Sentinel loop for adding options 
    final String SENTINEL = "-1"; 
    String choices = plans[0]; 
    Scanner scanner = new Scanner(System.in); 
    System.out.println("What would you like to add to your plan (options are a,b,c, or d. Enter -1 when you are finished): "); 
    String yourChoice = scanner.nextLine(); 
    while (yourChoice != SENTINEL) { 




    } 

} 

} 

Как именно я могу сделать это происходит и что мне нужно сделать, чтобы положить внутрь время цикла? Благодаря!

ответ

0

Вы могли бы сделать что-то вроде этого.

while(true) { 
    System.out.println("What would you like to add to your plan (options are a,b,c, or d. Enter -1 when you are finished): "); 
    String yourChoice = scanner.nextLine(); 
    if(yourChoice.equals(SENTINEL)) 
     return; 
} 

или, если вы должны использовать символ:

do { 
     System.out.println("What would you like to add to your plan (options are a,b,c, or d. Enter -1 when you are finished): "); 
     yourChoice = scanner.nextLine(); 
    } while (!yourChoice.equals(SENTINEL)); 

Это один из способов, чтобы получить общую цену:

double price = 0.0; 
do { 
    System.out.println("What would you like to add to your plan (options are a,b,c, or d. Enter -1 when you are finished): "); 
    yourChoice = scanner.nextLine(); 
    switch (yourChoice) { 
     case "a": 
      price += Double.parseDouble(plans[0].substring(plans[0].length()-4, plans[0].length())); 
      break; 
     case "b": 
      price += Double.parseDouble(plans[1].substring(plans[1].length()-4, plans[1].length())); 
      break; 
     case "c": 
      price += Double.parseDouble(plans[2].substring(plans[2].length()-4, plans[2].length())); 
      break; 
     case "d": 
      price += Double.parseDouble(plans[3].substring(plans[3].length()-5, plans[3].length())); 
      break; 
    } 
} while (!yourChoice.equals(SENTINEL)); 

System.out.println("Total price: " + price); 
+0

делает много смысла, я был очень смущен о том, как не говоря уже о String. Большое спасибо! –

+0

У меня есть еще один вопрос: как я могу добавить общую стоимость всех опций? Я знаю, что цены содержатся в массиве String, так как я могу настроить что-то, что, когда цикл while пройдет, это добавит цены? –