2016-04-24 99 views
-4
import java.util.Scanner; 

public class windowstore { 

public static void main(String[] args) { 

    int width; 
    int height; 
    int area; 
    int perm; 
    int costarea; 
    int costperm; 
    float glasscost; 
    float trimcost; 

    width = 0; 
    height = 0; 
    glasscost = (float) 3.50; 
    trimcost = (float) 2.25; 

    Scanner sc = new Scanner(System.in); 

    String stringheight = ""; 
    String stringwidth = ""; 

    System.out.println("What is the height of your window?"); 

    stringheight = sc.nextLine(); 
    height = Integer.parseInt(stringheight); 

     try { 
      height = Integer.parseInt(stringheight); 
     } catch (Exception ex) { 
      System.out.println("Please enter a number not a word or letter"); 
     } 


    while (height > 20 || height < 1) { 

     if (height > 20) { 
      System.out.println("Sorry " + height + " is above 20. Please enter a number below 20."); 
      System.out.println("What is the height of your window?"); 

      stringheight = sc.nextLine(); 
      height = Integer.parseInt(stringheight); 

     } 
     if (height < 1) { 
      System.out.println("Sorry " + height + " is below 1. Please enter a number above 1."); 
      System.out.println("What is the height of your window?"); 

      stringheight = sc.nextLine(); 
      height = Integer.parseInt(stringheight); 
     } 
    } 
    System.out.println("What is the width of your window?"); 

    stringwidth = sc.nextLine(); 

    height = Integer.parseInt(stringheight); 

    width = Integer.parseInt(stringwidth); 
    while (width > 20 || width < 1) { 

     if (width > 20) { 
      System.out.println("Sorry " + width + " is above 20. Please enter a number below 20."); 
      System.out.println("What is the width of your window?"); 
      stringwidth = sc.nextLine(); 

      height = Integer.parseInt(stringheight); 

      width = Integer.parseInt(stringwidth); 
     } 
     if (width < 1) { 
      System.out.println("Sorry " + width + " is below 1. Please enter a number above 1."); 
      System.out.println("What is the width of your window?"); 

      stringwidth = sc.nextLine(); 

      height = Integer.parseInt(stringheight); 

      width = Integer.parseInt(stringwidth); 
     } 
    } 

    area = width * height; 

    System.out.println("The area of your window is " + area + " inches."); 

    perm = width + width + height + height; 

    System.out.println("The perimeter of your window is " + perm + " inches."); 

    costarea = (int) (area * glasscost); 

    System.out.println("The cost of glass for your window is " + costarea); 

    costperm = (int) (perm * trimcost); 

    System.out.println("The cost of trim for your window is " + costperm); 

    } 
} 

我目前正在努力让程序要求用户“请输入数字而不是一个字或字母”每次他们输入一个字符这不是一个整数。但是,当他们把一个整数代码将正​​常运行。不明白什么插入到while循环正常工作

+2

这个代码将无法编译。你有什么问题的代码? – f1sh

+0

发布了整个代码 –

+0

你可以做2.25f而不是(浮动)2.35 –

回答

2

这里有一个例子或许说明你需要:

public static void main(String[] args) { 
    Scanner sc = new Scanner(System.in); 
    int height; 
    while (true) { 
     System.out.println("What is the height of your window?"); 
     String cmd = sc.nextLine(); 
     try { 
      height = Integer.parseInt(cmd); 
      break; 
     } catch (Exception ex) { 
      System.out.println("Please enter a number not a word or letter"); 
     } 
    } 
    System.out.println("You typed: " + height); 
} 
+1

这个工作完美,但有点困惑,为什么它的工作,你能解释为什么有(while)真正做我想发生什么? –

+1

当然:)条款'while(true)'是一个无限循环。如果我们想要永久性地询问数字,我们应该在其中放入'sc.nextLine()'。 Wean用户输入正确的数字,循环被“break”中断。另一方面,当用户输入不正确的值时,Exception会在'height = Integer.parseInt(cmd)'这一行被抛出,所以打印正确的消息并且循环再次开始(跳过'break')。 –