首先 - 我在这个领域完全是新手,很抱歉,如果这段代码看起来很混乱。
我正在尝试制作“程序”,这将显示我未来的订阅计数。 所以节目问我三个问题 -
之后它会给我回答 -
“在22天内,你将拥有2000个潜艇”
当我键入没有十进制数的速率(例如1,2,...)时它工作正常。 但是当我输入十进制数的速率时,它会抛出这样的错误 -
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at test.maintest.main(maintest.java:16)
代码:
main.class
package test;
import java.util.Scanner;
import java.util.Locale;
public class maintest {
public static void main(String args[]) {
Scanner input = new Scanner(System.in).useLocale(Locale.US);
second classNd = new second();
System.out.println("Your actual sub count is? ");
Double count = input.nextDouble();
System.out.println("How many days? ");
Double day = input.nextDouble();
System.out.println("Your actual rate ");
Double rate = input.nextDouble();
String answerCount = Double.toString(count);
String answerDay = Double.toString(day);
String answerRate = Double.toString(rate);
classNd.getSubs(answerCount);
classNd.getDay(answerDay);
classNd.getRate(answerRate);
classNd.answerMe();
}
}
second.class:
package test;
public class second {
private String gotSubs;
public void getSubs(String answ) {
gotSubs=answ;
}
private String gotRate;
public void getRate(String gtRate) {
gotRate=gtRate;
}
private String gotDay;
public void getDay(String gotDays){
gotDay=gotDays;
}
public void answerMe() {
double getRates = Double.parseDouble(gotRate);
double amount;
double numberSubs = Double.parseDouble(gotSubs);
double numberOf = Double.parseDouble(gotDay);
do{
amount = numberSubs*Math.pow(1+getRates, numberOf);
System.out.println("In " + numberOf + " days, u will have " + amount + " subs");
}while(numberOf<0);
}
}
所以我不得不将Locale事物添加到主类:
import java.util.Scanner;
import java.util.Locale;
public class maintest {
public static void main(String args[]) {
Scanner input = new Scanner(System.in).useLocale(Locale.US);
second classNd = new second();