-1

I'm trying to make a method that will ask the user the amount, then it checks if the amount if >0 and if it is the loop ends and if the input isnt >0 then the loop continues until proper data is entered. I can't figure out my problem..

/** Get principal amount **/
public static double getPrincipalAmount(double numb1) {
    Scanner input = new Scanner(System.in);
    do {
        System.out.print("Enter Loan Amount: ");
        double numb1 = input.nextDouble();
        double getPrincipalAmount = 0;
        if (numb1 > 0) {
            getPrincipalAmount = numb1;
        } else {    
            System.out.println("Data Error: Loan amount must be greater than zero. You entered " + numb1);
        }       
    } while (numb1 < 0);
    return getPrincipalAmount;
}
4

5 に答える 5

2

これを試して!!!

java.util.Scanner をインポートします。

public class PrinipalDemo{



    public static void main(String args[]){

            Scanner input = new Scanner(System.in);
            double numb11;
            double getPrincipalAmount ;
           do{System.out.print("Enter Loan Amount: ");
             numb11 = input.nextDouble();
             getPrincipalAmount = 0.0;
             if(numb11 > 0)
            getPrincipalAmount = numb11;
                  else{   
                          System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb11);
                  }       
                 }while (numb11 < 0);

        System.out.println(getPrincipalAmount);

    }

} 
于 2013-03-22T06:52:41.300 に答える
0
  1. メソッドをパラメータ化する必要はありません。getPrincipalAmount(double numb1)
  2. getPrincipalAmountメソッドスコープ内にある必要があります
  3. Scanner input閉じる必要があります

これを試して

import java.util.Scanner;
public class so7 
{
public static void main(String args[])
{
    getPrincipalAmount();
}
/** Get principal amount **/
public static double getPrincipalAmount() {
    Scanner input = new Scanner(System.in);
    double getPrincipalAmount = 0;
    double numb = 0;
    do {
        System.out.print("Enter Loan Amount: ");
        numb = input.nextDouble();
        if (numb > 0) {
            getPrincipalAmount = numb;
        } else {    
            System.out.println("Data Error: Loan amount must be greater than zero. You entered " + numb);
        }       
    } while (numb < 0);
    input.close();
    return getPrincipalAmount;
}
}
于 2013-03-22T07:00:18.577 に答える
0

このようにしたほうがいいと思います

public static double getPrincipalAmount(double numb1) {
    numb1 = 0;
    Scanner input = new Scanner(System.in);
    do {

        System.out.print("Enter Loan Amount: ");
        numb1 = input.nextDouble();
        double getPrincipalAmount = 0;
        if (numb1 > 0) {
            getPrincipalAmount = numb1;
        } else {    
            System.out.println("Data Error: Loan amount must be greater than zero. You entered " + numb1);
        }       
    } while (numb1 < 0);
    return getPrincipalAmount;
}
于 2013-03-22T06:52:37.630 に答える
0

理想的には、そのような小さな問題を修正する必要があります。

public static double getPrincipalAmount() {
    final Scanner input = new Scanner(System.in);
    double getPrincipalAmount = 0;
    double numb1;
    do {
        System.out.print("Enter Loan Amount: ");
        if ((numb1 = input.nextDouble()) > 0) {
            getPrincipalAmount = numb1;
        } else {
            System.out.println("Data Error: Loan amount must be greater than zero. You entered " + numb1);
        }
    } while (numb1 < 0);
    return getPrincipalAmount;
}
于 2013-03-22T06:53:30.017 に答える
0

あなたのコードにはいくつかの問題があります。numb1まず、変数をパラメーターとしてメソッドに渡しますgetPrincipalAmount(double numb1)。それが、新しい宣言がDuplicate local variable numb1変数を与えている理由です。

次に、 を返していますが、ループgetPrincipalAmount内でのみ初期化されます。do - whileループの外で初期化する必要があります。

于 2013-03-22T06:51:01.683 に答える