私はJavaで初心者ですので、調査の不足のために判断を下さないでください。ここで私を複数のforループに対して無効な入力をした後に再度プロンプトを表示
このプログラムは、基本的な計算されたプログラムです。ユーザーは、プロジェクトの総コストを計算することができます。
私のプログラムはうまくデバッグできますが、問題は、ユーザー入力が無効な場合です。 ユーザが無効なエントリを入力すると、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");
}
}
}
}
}
行う場合ループは良くなるだろうが、私が考えていたが、私はそれが何であるか、プログラムがよりcomplaicatedにしようとしておりません。
実際の問題があなたが記述している/観察していることは明らかではありません。あなたは具体的になりますか? – David
@Davidプログラムを実行すると、ユーザー入力が無効な場合、プログラムは「Please、amount!」と応答してプログラムを終了します。私は、ユーザが有効なエントリを入力するまで、プログラムがforループを続けるようにしたい。 – krillavilla