所以我的程序必须计算一个人在扫描仪方法中输入的更改量,但是,如果a)输入不是数字而b)如果缺少输入,则必须弹出两个错误。我对后者有很多麻烦。
import java.util.InputMismatchException;
import java.util.Scanner;
public class MakingChange
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in); // Reading from System.in
System.out.print("How much money do you have: ");
double amountInDollars = 0;
try {
amountInDollars = input.nextDouble();
} catch (InputMismatchException e) {
System.out.println("INVALID"); //print invalid
return;
}
if (input.equals(" ")){
System.out.println("INVALID");
return;
}
int amountInPennies = (int) Math.round(amountInDollars * 100);
amountInPennies = (int) (Math.round(amountInPennies / 5.0) * 5);
//toonies
int numberofToonies = (int)(amountInPennies / 200.00);
amountInPennies = amountInPennies % 200;
//loonies
int numberofLoonies = (int) (amountInPennies / 100.00);
amountInPennies = amountInPennies % 100;
//quarters
int numberofQuarters = (int)(amountInPennies / 25.00);
amountInPennies = amountInPennies % 25;
//dimes
int numberofDimes = (int)(amountInPennies / 10.00);
amountInPennies = amountInPennies % 10;
//nickels
int numberofNickels = (int)(amountInPennies / 5.00);
System.out.println("toonies:" + numberofToonies + ";" + " loonies:" + numberofLoonies + ";" + " quarters:" + numberofQuarters + ";" + " dimes:" + numberofDimes + ";" + " nickels:" + numberofNickels);
}
}
所以预期应该是“INVALID”但是目前,所有返回的都是一个空格。 谢谢!
答案 0 :(得分:1)
检查以下代码,我添加了评论。
<string name="email">$message = \'<center>
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td align="center" bgcolor="#FFFFFF">
<table width="620px" cellpadding="0" cellspacing="0">
<tr>
<td width="400px" bgcolor="#000000" style="color:#FFFFFF; text-
decoration:none; float:left;font-size:10px;margin: -18px 3px -1px 11px; font-weight:bold; padding:2px; text-decoration:none; padding-left:0px;">For Support <span style="text-decoration:none; color:#FFFFFF;">enquiry@makeintern.com</span></td>
<td width="220px" bgcolor="#000000"></td>
</tr>
<tr style="background-color:#737373;">
<td width="290" align="left" valign="middle"><a href="http://makeintern.com" target="_blank"><img src="http://www.makeintern.com/images/makeintern.jpg" width="136" border="0" alt="MakeIntern" style="display:block; padding-left:0px;"></a></td>
<td width="290" align="right" style="text-align:right; font-family:Arial, Segoe UI,Helvetica Neue, Helvetica, sans-serif; font-size:18px; line-height:18px; color:#FFFFFF; font-weight:normal; padding-right:10px;">Contact Form Enquiry !<br>
</td>
</tr>
<tr>
<td height="12px" colspan="2"></td>
</tr>
<tr>
<td colspan="2" align="center" style="line-height:16px; font-family:arial; font-size:12px; color:#888888; text-align:left;">
<table cellspacing="0" cellpadding="0">
<tr>
<td width="200px" style="font-weight:bold;">Name: </td>
<td width="100px">:</td>
<td>\'.$name.\'</td>
</tr>
<tr>
<td width="200px" style="font-weight:bold;">Email: </td>
<td width="100px">:</td>
<td>\'.$email.\'</td>
</tr>
<tr>
<td width="200px" style="font-weight:bold;">Contact: </td>
<td width="100px">:</td>
<td>\'.$mobile.\'</td>
</tr>
<tr>
<td width="200px" style="font-weight:bold;">Message: </td>
<td width="100px">:</td>
<td>\'.$message.\'</td>
</tr>
<tr>
<td width="200px" style="font-weight:bold;">Date-Time: </td>
<td width="100px">:</td>
<td>\'.$current_date.\'</td>
</tr>
</table>
<br/><br/>
Thanks and Regards <br/>
MakeIntern Team <br/>
Account Manager - MakeIntern Cell <br/>
www.makeintern.com<br/>
enquiry@makeintern.com<br/><br/>
<b>If any query Please call us on : 011-45544188</b><br/>
</td>
</tr>
<tr>
<td height="12px"></td>
</tr>
<tr>
<td colspan="2"><img src="http://www.myhotbooking.com/images/email-template/bar.png"></td>
</tr>
<tr>
<td colspan="2" bgcolor="#737373" height="90px">
<table cellpadding="0" cellspacing="0" width="620px">
<tr>
<td height="10px"></td>
</tr>
<tr>
<td align="center" style="color:#FFFFFF;" height="20px"><a href="http://www.makeintern.com/">www.makeintern.com </a>| <span style="color:#FFFFFF; text-decoration:none;">enquiry@makeintern.com</span></td>
</tr>
<tr>
<td align="center" valign="middle">
<a href="https://www.facebook.com/makeinterns" target="_blank"><img src="http://www.myhotbooking.com/images/email-template/icon-facebook.png" width="26" height="26" border="0" alt="Facebook"></a><a href="https://twitter.com/makeintern" target="_blank"><img src="http://www.myhotbooking.com/images/email-template/icon-twitter.png" width="26" height="26" border="0" alt="Twitter"></a><a href="https://www.linkedin.com/company/makeintern" target="_blank"><img src="http://www.myhotbooking.com/images/email-template/icon-linkedin.png" width="26" height="26" border="0" alt="Linkedin"></a><a href="https://plus.google.com/+makeintern" target="_blank"><img src="http://www.myhotbooking.com/images/email-template/icon-google-plus.png" width="26" height="26" border="0" alt="Google+"></a></td>
</tr>
<tr>
<td align="center" style="color:#FFFFFF;"></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</center>\'</string>
答案 1 :(得分:0)
试试这个,应该可以正常使用。
Scanner input = new Scanner(System.in); // Reading from System.in
System.out.print("How much money do you have: ");
double amountInDollars = 0;
String emp = input.nextLine();
try {
// if the user doesn't enter anything and press enter or entered value is empty
if (emp.isEmpty() || emp.equals(" ")) {
System.out.println("Invalid input");
return;
}
}catch (InputMismatchException e){
//catch the message
e.printStackTrace();
}
// if string entered is not double
boolean isnum = emp.chars().allMatch(Character::isAlphabetic);
if (!isnum) { // if is a Number then we pass it in
amountInDollars = Double.parseDouble(emp);
} else {
// we pass exception message to catch
System.out.println("Invalid Input: Double values only please");
return;
}
if (amountInDollars < 0) { // if the value is 'Negative'
System.out.println("INVALID");
return;
}
int amountInPennies = (int) Math.round(amountInDollars * 100);
amountInPennies = (int) (Math.round(amountInPennies / 5.0) * 5);
//toonies
int numberofToonies = (int)(amountInPennies / 200.00);
amountInPennies = amountInPennies % 200;
//loonies
int numberofLoonies = (int) (amountInPennies / 100.00);
amountInPennies = amountInPennies % 100;
//quarters
int numberofQuarters = (int)(amountInPennies / 25.00);
amountInPennies = amountInPennies % 25;
//dimes
int numberofDimes = (int)(amountInPennies / 10.00);
amountInPennies = amountInPennies % 10;
//nickels
int numberofNickels = (int)(amountInPennies / 5.00);
System.out.println("toonies:" + numberofToonies + ";" + " loonies:" + numberofLoonies + ";" + " quarters:" + numberofQuarters + ";" + " dimes:" + numberofDimes + ";" + " nickels:" + numberofNickels);
让我知道它是否成功。
答案 2 :(得分:-1)
据我所知,您正在测试您的扫描仪对象是否等于字符串文字,这完全没有意义。
首先,使用任何扫描程序的nextXXX
- 方法获取字符串。
然后,使用someString.equals("")
检查该字符串是否为空,或者我认为someString.isEmpty()
更好。
答案 3 :(得分:-1)
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BigDecimal money = getBigDecimal(sc); // When using scanner as parameter your performance increases because you don't create new scanners every time if you use the scanner for different purposes or within a recursive function.
System.out.printf("You have %(,.2f dollars%n", money); // Just an example of your output.
}
public static BigDecimal getBigDecimal(Scanner sc) {
System.out.print("How much money do you have? ");
if (sc.hasNextBigDecimal()) { // if a BigDecimal value is typed
return sc.nextBigDecimal();
}
sc.next(); // else ignore input
System.out.println("Please, type amount.");
return getBigDecimal(sc); // recursion
}
为了钱,你最好使用BigDecimal
(相信我,或者不要相信我)。此外,您可以使用BigDecimal
函数进行计算。
https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial http://www.tutorialspoint.com/java/math/java_math_bigdecimal.htm