2016-09-21 28 views
0

我想用字母来代替数字。所以这将是R免费停车,C为有盖停车场,G为车库停车场。我试图将其转换为if语句,但似乎无法弄清楚如何让它的工作任何帮助,将不胜感激。如何在java中使用开关大小写字符?

/*get users parking choice*/ 
    System.out.println("What kind of parking would you like press 1 for free parking, " + 
      " 2 for a Covered parking, or 3 for garage parking"); 
    parking = garageType.nextInt(); 

    switch (parking){ 
     case 1: 
      type = "free"; 
      gPrice = 0; 
      break; 
     case 2: 
      type = "covered"; 
      gPrice = 3000; 
      break; 
     case 3: 
      type = "garage"; 
      gPrice = 6000; 
      break; 
     default: 
      System.out.println("That was an invalid option please try again"); 
      break; 
    } 
    /* get total price*/ 
    total = condoPrice+gPrice; 
    /*show the user their choices and condo, parking and total price*/ 
    System.out.println("You have selected a condo with a view of the " 
      + view + " for $" + condoPrice + " with " + type + " parking, " + 
      " your total for this condo and parking is $ " + total); 
} 
+1

只要'garageType.nextChar()',然后将案例更改为''R'等。 – Jameson

+0

谢谢你的帮助。 –

回答

0

要使用字符作为你的决策参数,你需要采取字符作为来自用户的输入。

目前你正在服用的整数作为输入

nextChar() 

你可以使用这个功能来读取字符,然后通过保持你的决定在一个单引号像“R”进行比较

希望它帮助!

+0

谢谢你的帮助。 –

4
 System.out.println("What kind of parking would you like press 'R' for free parking, " + 
      " 'C' for a Covered parking, or 'G' for garage parking"); 
    parking = garageType.nextChar(); 

    switch (parking){ 
     case 'R': 
      type = "free"; 
      gPrice = 0; 
      break; 
     case 'C': 
      type = "covered"; 
      gPrice = 3000; 
      break; 
     case 'G': 
      type = "garage"; 
      gPrice = 6000; 
      break; 
     default: 
      System.out.println("That was an invalid option please try again"); 
      break; 
    } 
    /* get total price*/ 
    total = condoPrice+gPrice; 
    /*show the user their choices and condo, parking and total price*/ 
    System.out.println("You have selected a condo with a view of the " 
      + view + " for $" + condoPrice + " with " + type + " parking, " + 
      " your total for this condo and parking is $ " + total); 
} 
+0

感谢您的帮助。 –

+0

任何时候。如果它回答你的问题,你可以接受它作为答案:) – Yordan

相关问题