方法扫描器输入java?
我试图创建一个方法,询问用户数量,然后检查数量是否大于0,循环是否结束,如果输入不大于0,则循环继续,直到输入正确的数据。我想不出我的问题方法扫描器输入java?,java,methods,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 Amoun
/** 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;
}
/**获取本金金额**/
公共静态双getPrincipalAmount(双编号1){
扫描仪输入=新扫描仪(System.in);
做{
系统输出打印(“输入贷款金额:”);
double numb1=input.nextDouble();
双getPrincipalAmount=0;
如果(1>0){
getPrincipalAmount=numb1;
}否则{
System.out.println(“数据错误:贷款金额必须大于零,输入“+numb1”);
}
}而(1<0);
返回getPrincipalAmount;
}
您的代码有几个问题。
首先,将变量numb1
作为参数getPrincipalAmount(双numb1)
传递到方法中。这就是为什么新的声明给了您一个重复的局部变量numb1
变量
其次,返回的是
getPrincipalAmount
,但它只在do-while
循环中初始化。您需要在循环之外初始化它。我认为您最好这样做
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;
}
公共静态双getPrincipalAmount(双编号1){
numb1=0;
扫描仪输入=新扫描仪(System.in);
做{
系统输出打印(“输入贷款金额:”);
numb1=input.nextDouble();
双getPrincipalAmount=0;
如果(1>0){
getPrincipalAmount=numb1;
}否则{
System.out.println(“数据错误:贷款金额必须大于零,输入“+numb1”);
}
}而(1<0);
返回getPrincipalAmount;
}
试试这个
导入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);
}
}
公共类PrinipalDemo{
公共静态void main(字符串参数[]){
扫描仪输入=新扫描仪(System.in);
双盲11例;
双峰;
do{System.out.print(“输入贷款金额:”);
numb11=input.nextDouble();
getPrincipalAmount=0.0;
如果(11>0)
getPrincipalAmount=numb11;
否则{
System.out.println(“数据错误:贷款金额必须大于零,您输入了”+11);
}
}而(11<0);
System.out.println(getPrincipalAmount);
}
}
理想情况下,您应该解决这样的小问题,但问题还在这里-
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;
}
公共静态双getPrincipalAmount(){
最终扫描仪输入=新扫描仪(System.in);
双getPrincipalAmount=0;
双盲1;
做{
系统输出打印(“输入贷款金额:”);
如果((numb1=input.nextDouble())>0){
getPrincipalAmount=numb1;
}否则{
System.out.println(“数据错误:贷款金额必须大于零,输入“+numb1”);
}
}而(1<0);
返回getPrincipalAmount;
}
getPrincipalAmount(双编号1)
getPrincipalAmount
必须在方法范围内扫描仪输入
必须关闭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;
}
}
import java.util.Scanner;
公共类so7
{
公共静态void main(字符串参数[])
{
getPrincipalAmount();
}
/**获得本金**/
公共静态双getPrincipalAmount(){
扫描仪输入=新扫描仪(System.in);
双getPrincipalAmount=0;
双麻木=0;
做{
系统输出打印(“输入贷款金额:”);
numb=input.nextDouble();
如果(麻木>0){
getPrincipalAmount=numb;
}否则{
System.out.println(“数据错误:贷款金额必须大于零,输入“+numb”);
}
}麻木<0;
input.close();
返回getPrincipalAmount;
}
}
问题出在哪里?上面的代码甚至无法编译,例如numb1定义了两次。请删除编译错误,运行代码,然后发布问题。在while循环中添加了一个=。应该是@Vulcan我说的是while循环。不是if语句。看看我的评论。它应该循环,直到输入一个大于0的数字。因此,当数字小于或等于0时,它会循环。