不明白插入while循环以正常工作的内容

时间:2016-04-24 17:13:10

标签: java

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);

  }
}

我正在努力让程序每次输入一个不是整数的字符时都要求用户“请输入一个不是单词或字母的数字”。但是当它们放入一个整数时,代码将正常运行。

1 个答案:

答案 0 :(得分: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);
}