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循环正常工作
这个代码将无法编译。你有什么问题的代码? – f1sh
发布了整个代码 –
你可以做2.25f而不是(浮动)2.35 –