2015-05-29 63 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(); 
} 
}