方法扫描程序输入java?

时间:2013-03-22 06:40:56

标签: java methods

我正在尝试制作一个方法,询问用户数量,然后检查金额是否> 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;
}

5 个答案:

答案 0 :(得分:2)

试试这个!!!

import 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);

    }

} 

答案 1 :(得分:0)

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

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

答案 2 :(得分: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;
}

答案 3 :(得分: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;
}

答案 4 :(得分:0)

  1. 无需参数化方法:getPrincipalAmount(double numb1)
  2. getPrincipalAmount必须位于方法范围
  3. 中 必须关闭
  4. Scanner input
  5. 试试这个

    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;
    }
    }