Java 带数字的If语句
我需要这样做,当用户输入单词2而不是数字2时,它会告诉用户数字需要是数字形式Java 带数字的If语句,java,if-statement,Java,If Statement,我需要这样做,当用户输入单词2而不是数字2时,它会告诉用户数字需要是数字形式 package monthly.mortgage.rate; import java.util.Scanner; class MonthlyMortgageRate { public static void main(String[] args) { double Amount; double Rate; double Months; d
package monthly.mortgage.rate;
import java.util.Scanner;
class MonthlyMortgageRate {
public static void main(String[] args) {
double Amount;
double Rate;
double Months;
double outputNum1;
boolean doagain;
doagain = true;
Scanner in = new Scanner(System.in);
while (doagain) {
System.out.print("Enter loan amount:");
Amount = in.nextDouble();
System.out.print("Enter rate:");
Rate = in.nextDouble() / 100 / 12;
System.out.print("Enter year:");
Months = in.nextDouble() * 12;
outputNum1 = Rate * Amount / (1 - Math.pow(1 + Rate, -Months));
if (Amount <= 0)
System.out.println("You must enter positive numeric data!");
if (Rate <= 0)
System.out.println("You must enter positive numeric data!");
if (Months <= 0)
System.out.println("You must enter positive numeric data!");
else
System.out.printf("Monthly payment is: $ %.2f%n", outputNum1);
System.out.println("would you like to calculate again?(y/n)");
if (in.next().toLowerCase().equals("y"))
;
boolean y = doagain;
if (in.next().toLowerCase().equals("n"))
doagain = false;
boolean n = false;
}
}
}
package monthly.mortgage.rate;
导入java.util.Scanner;
每月一班{
公共静态void main(字符串[]args){
双倍金额;
双倍费率;
两个月;
双输出num1;
布尔波达方向增益;
doagain=true;
扫描仪输入=新扫描仪(系统输入);
while(doagain){
系统输出打印(“输入贷款金额:”);
金额=in.nextDouble();
系统输出打印(“输入速率:”);
速率=in.nextDouble()/100/12;
系统输出打印(“输入年份:”;
月份=in.nextDouble()*12;
outputNum1=费率*金额/(1-数学功率(1+费率,-个月));
如果(Amount我从你的问题中得到的是,你想知道是否有人输入了字符串作为数字,而不是int作为数字。基本上,你可以用每个字母创建一个数组:
Array alphabet = {"a", "b"...}
现在,只需查看输入是否包含字母表中的任何字符……或者在我们的示例中,数组字母表
。您可以使用。contains
for(int i =0; i < alphabet.length; i++)
{
if(inputString.contains(alphabet[i]))
{
return true;
}
}
你的问题是什么?你认为;
在if
语句的末尾是什么意思?你的意思是像你之前在代码中做的那样in.nextDouble()==2
吗?如果用户键入的是“2”而不是“2”,那么nextDouble()
应该抛出一个异常让你抓住……如果你是这个意思的话。
try{
}Catch(CATCH THE STRING){
System.out.println("You should put in a number!");
}