2017-02-28 42 views
0

我遇到了一些java代碼的問題,所以我想我會在這裏問它。我被要求添加一行代碼來照顧小寫輸入,但我不確定如何實現它。我可能大大過於尋找一個非常簡單的修復,但任何幫助,非常感謝。如何正確使用toUpperCase();

import java.util.Scanner; 

    public class Worksheet_7 
    { 
     public static void main(String[]args) 
     { 
      Scanner Fred = new Scanner(System.in); 
      System.out.println("Today we practice using switch statements \n\n"); 
      char Ans; 
      System.out.println("What kind of car do you drive"); 
      System.out.println("(F)ord, (C)hevy, (B)uick,(M)azda,(H)onda"); 
      String Word = Fred.next(); 
      Ans=Word.charAt(0); 
      switch(Ans) 
      { 
       case 'C': System.out.println("Chevy-the heartbeat of America"); 
       System.out.println("Buy a ford"); 
       break; 
       case 'F': System.out.println("Ford tough"); 
       System.out.println("Nice job cowboy"); 
       break; 
       case 'B': System.out.println("Buick-Luxury you can afford"); 
       System.out.println("Seriously? A Buick? That's not Luxury"); 
       break; 
       case 'M': System.out.println("Mazda-Zoom, Zoom"); 
       System.out.println("Can't really say anything"); 
       break; 
       case 'H': System.out.println("Honda-Always dependable"); 
       System.out.println("Yeah, yeah, yeah"); 
       break; 
       default: System.out.println("Need a better car...?"); 
      } 
      System.out.println("\n\n done for now.."); 
     } 
    } 
+0

你需要改變你比較'Ans'的情況下,也許從那裏開始 – MadProgrammer

回答

0

Fred.next()String

你需要調用的public String toUpperCase()某處String方法。

注意:它返回一個新的字符串,並沒有就地大寫。


或者是有Character方法public static Character toUpperCase(char ch)


或者你可以更新的情況下

case 'c': 
case 'C': 
    ... 
    break; 
case 'f': 
case 'F': 
    ... 
    break; 
+0

它返回一個字符串的方式,但他清楚地被檢索第一它的性格。不要誤解我的意見,你的建議比目前的解決方案更好。 –

+0

@OusmaneDiaw整個字符串的大寫字母的性能較差,當然 –

+0

它可以產生多少差異? –

0

試試這個,用戶String而不是char

public static void main(String[]args) 
{ 
    Scanner Fred = new Scanner(System.in); 
    System.out.println("Today we practice using switch statements \n\n"); 
    String Ans; 
    System.out.println("What kind of car do you drive"); 
    System.out.println("(F)ord, (C)hevy, (B)uick,(M)azda,(H)onda"); 

    String Word = Fred.next(); 

    Ans=Word.substring(0, 1).toUpperCase(); 

    switch(Ans) 
    { 
     case "C": System.out.println("Chevy-the heartbeat of America"); 
      System.out.println("Buy a ford"); 
      break; 
     case "F": System.out.println("Ford tough"); 
      System.out.println("Nice job cowboy"); 
      break; 
     case "B": System.out.println("Buick-Luxury you can afford"); 
      System.out.println("Seriously? A Buick? That's not Luxury"); 
      break; 
     case "M": System.out.println("Mazda-Zoom, Zoom"); 
      System.out.println("Can't really say anything"); 
      break; 
     case "H": System.out.println("Honda-Always dependable"); 
      System.out.println("Yeah, yeah, yeah"); 
      break; 
     default: System.out.println("Need a better car...?"); 
    } 
    System.out.println("\n\n done for now.."); 
} 
+4

使用'switch(Character.toUpperCase(Ans))'會好於子串 –

0

你可以只改變開關

switch (ans.toUpperCase()){ 
+1

我認爲你的意思是'public static char toUpperCase(char ch)' –