-1

我正在尝试制作一种询问用户金额的方法,然后它检查金额是否> 0,如果它是循环结束,如果输入不是> 0,则循环继续,直到输入正确的数据。我无法弄清楚我的问题..

/** 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

您的代码有几个问题。首先,您将变量numb1作为参数传递给方法getPrincipalAmount(double numb1)。这就是为什么新声明给你一个Duplicate local variable numb1变量的原因。

其次,您正在返回getPrincipalAmount,但它仅在do - while循环内初始化。您需要在循环外对其进行初始化。

于 2013-03-22T06:51:01.683 回答
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
  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 回答