2015-05-29 109 views
0

重複溫度編程項目添加一個循環,允許用戶繼續輸入溫度,直到他們輸入Q或 q退出程序的該部分。用戶應該輸入Q或q 退出並返回一個空字符串。 (任何其他條目應導致 問題重複)如果用戶輸入不同的字母 表示溫度爲F或C(大寫或小寫),則打印 錯誤消息並要求用戶輸入正確的溫度不需要詢問新的數值就可以縮放 。溫度不正確的用戶輸入

這是我到目前爲止....我無法得知它是否輸入65D而不是65C。我無法弄清楚如何循環回通過代碼,如果他們進入d,而不是F/F或C/C ...

import java.util.Scanner; 
public class AssignmentFour { 
public static Scanner keyboard = new Scanner(System.in); 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 

    System.out.println("Hello, I can convert Fahrenheit to Celsius!"); 
    System.out.println("Please enter the temperature you want converted."); 
    System.out.println("Followed by: 'C/c' or 'F/f'"); 

    String input2 = keyboard.next(); 

    do{ 
     String temp = input2.trim(); 
     String degreesAsString = temp.substring(0, temp.length()-1); 
     double degrees = Double.parseDouble(degreesAsString); 

     if(temp.endsWith("C") || temp.endsWith("c")){    
      double degreeF = 9 * degrees/5 + 32; 
      System.out.println(input2 + " is equal to: "); 
      System.out.printf("%.2f", degreeF); 
      System.out.println(" Fahrenheit."); 

     } else if(temp.endsWith("F") || temp.endsWith("f")){ 
      double degreeC = 5 * (degrees - 32)/9; 
      System.out.println(input2 +" is equal to: "); 
      System.out.printf("%.2f", degreeC); 
      System.out.println(" Celsius."); 

     } else if (temp.endsWith(""));{ 
      System.out.println("ERROR: Please enter either 'F/f or C/c'.");  
     } 
     break; 
    } while(!input2.equals("")); 

    System.out.println(""); 
} 
+0

您最後的'else if'塊應該是'else'。不需要'if'語句。 –

+0

不起作用。它的左手邊必須是一個變量... –

+0

順便說一句,您正在使用'break'錯誤。 'break'退出一個循環,而'continue'移動到下一次迭代。 –

回答

1

只需將您的... = keyboard.next()裏面do-while循環讀取當你循環時新的用戶輸入。此外,你應該刪除目前正在阻止任何循環完成的語句break;

+0

我將如何結束我的陳述?現在:while(input2.equals(「」));不起作用 –

+0

它不起作用,因爲你沒有刷新它的價值;你必須在循環內部做到這一點。 – KtorZ

0

考慮下面的其他方法。

package lala; 
import java.util.Scanner; 
public class AssignmentFour { 
public static Scanner keyboard = new Scanner(System.in); 
public static void main(String[] args) { 
// TODO Auto-generated method stub 

System.out.println("Hello, I can convert Fahrenheit to Celsius!"); 
System.out.println("Please enter the temperature you want converted."); 
System.out.println("Followed by: 'C/c' or 'F/f'"); 

String input2 = keyboard.next(); 

do{ 
    String temp = input2.trim(); 
    System.out.println(temp); 
    String degreesAsString = deg(temp); 
    System.out.println(degreesAsString); 
    double degrees = Double.parseDouble(degreesAsString); 

    if(temp.endsWith("C") || temp.endsWith("c")){    
     double degreeF = 9 * degrees/5 + 32; 
     System.out.println(input2 + " is equal to: "); 
     System.out.printf("%.2f", degreeF); 
     System.out.println(" Fahrenheit."); 

    } else if(temp.endsWith("F") || temp.endsWith("f")){ 
     double degreeC = 5 * (degrees - 32)/9; 
     System.out.println(input2 +" is equal to: "); 
     System.out.printf("%.2f", degreeC); 
     System.out.println(" Celsius."); 

    }else if(temp.equals("q") || temp.equals("Q")){ 

    } else{ 
     System.out.println("ERROR: Please enter either 'F/f or C/c'.");  
    } 
    System.out.println("\nProvide new temperature:"); 
    input2=keyboard.next(); 
} while(!input2.equals("")); 

System.out.println(""); 
} 

public static String deg(String s){ 
if(s==null || "".equals(s)) return ""; 
StringBuilder sb = new StringBuilder(); 
for(int i=0;i<s.length();i++){ 
    if(Character.isDigit(s.charAt(i))) sb.append(s.charAt(i)); 
} 
System.out.println(sb.toString()); 
return sb.toString(); 
} 
}