我是Java初学者,请耐心等待,不要因缺乏研究而判断。在这里我出多次循环的无效输入后重新提示用户
这个程序是基本的计算程序。它为用户设计计算项目的总成本。
我的程序调试正常,但问题是当用户键入无效的条目。 一旦用户键入无效的条目,for循环刚结束后Please, enter the amount!
我希望程序保持for循环,直到用户键入一个有效的条目。
import java.util.Scanner;
public class icalculator {
public static void main(String[] args) {
Scanner Keyboard = new Scanner(System.in);
double cost, percentage;
int years;
System.out.println("What the estimate cost of the project?");
cost = Keyboard.nextDouble();
if (cost <= 0) {
System.out.println("Please, enter the amount!");
} else if(cost > 0) {
System.out.println("What the estimate percentage cost of the project? ");
percentage = Keyboard.nextDouble();
if (percentage <= 0) {
System.out.println("Please, enter the amount!");
} else if(percentage > 0) {
System.out.println("How long it will take to complete the project? \n"
+ "Please, enter whole numbers.");
years = Keyboard.nextInt();
if (years <= 0) {
System.out.println("Please, enter the amount!");
} else if (years > 0) {
这部分是计算区域:
double c = cost;
double p = percentage;
int y = years;
for (int i = 1; i <= 1; i++) {
c = Math.round(((p/100) * c) + y);
System.out.println("At " + percentage + "% rate, the cost of the project will be $"
+ c + " which take " + years + " years to be complete");
}
}
}
}
}
我在想,如果do while循环会更好,但我没有试图让更多的complaicated那么它是什么节目。
目前尚不清楚你描述/观察的实际问题是什么。你能具体吗? – David
@David当我运行该程序时,如果用户键入无效的条目,程序将回答“请输入数量!”,然后程序结束。我希望程序继续执行for循环,直到用户输入有效的条目。 – krillavilla