我正在尝试创建一个程序,随机生成 1 到 10,000 之间的数字,然后将该数字除以 100 以创建另一个数字,然后用户应输入该数字的更改。 (67.48 是 b x 20、5、1、四分之一等的数量)...如果用户在答案中输入了错误的数字,程序应该显示他们的答案与随机数之间的差异。词汇表中没有任何关于差异或发现差异的内容……而且我从来没有做过像我们目前正在失去的那样广泛的 do-while 循环。我需要一些帮助来找出我做错了什么。
// ---------1---------2---------3---------4---------5---------6---------7
// 1234567890123456789012345678901234567890123456789012345678901234567890
import java.util.Scanner;
import java.util.Random;
public class ChangeGame
{
public static void main(String[] args)
{
int low = 1;
int max = 10000;
int twentyDollars = 0;
int tenDollars = 0;
int fiveDollars = 0;
int oneDollar = 0;
int quarters = 0;
int dimes = 0;
int nickels = 0;
int pennies = 0;
Random random = new Random();
int randomNumber = random.nextInt(max - low + 1);
double randomNumber1 = (randomNumber /100.0);
Scanner keyboard = new Scanner(System.in);
do
{
System.out.println("Enter the change for " + randomNumber1);
System.out.println("Twenties: ");
twentyDollars = keyboard.nextInt();
while( twentyDollars < 0 );
{
System.out.println( "** Invalid Amount **\n" +
"Twenties: " );
twentyDollars = keyboard.nextInt();
}
System.out.println("Enter the change for " + randomNumber1);
System.out.println("Tens: ");
tenDollars = keyboard.nextInt();
while( tenDollars <= 1 )
{
System.out.println( "** Invalid Amount **\n" +
"Tens: " );
tenDollars = keyboard.nextInt();
}
System.out.println("Enter the change for " + randomNumber1);
System.out.println("Fives: ");
fiveDollars = keyboard.nextInt();
while( fiveDollars <= 1 )
{
System.out.println( "** Invalid Amount **\n" +
"Fives: " );
fiveDollars = keyboard.nextInt();
}
System.out.println("Enter the change for " + randomNumber1);
System.out.println("Ones: ");
oneDollar = keyboard.nextInt();
while( oneDollar <= 4 )
{
System.out.println( "** Invalid Amount **\n" +
"Ones: " );
oneDollar = keyboard.nextInt();
}
System.out.println("Enter the change for " + randomNumber1);
System.out.println("Quarters: ");
quarters = keyboard.nextInt();
while( quarters <= 3 )
{
System.out.println( "** Invalid Amount **\n" +
"Quarters: " );
quarters = keyboard.nextInt();
}
System.out.println("Enter the change for " + randomNumber1);
System.out.println("Dimes: ");
dimes = keyboard.nextInt();
while( dimes <= 2 )
{
System.out.println( "** Invalid Amount **\n" +
"Dimes: " );
dimes = keyboard.nextInt();
}
System.out.println("Enter the change for " + randomNumber1);
System.out.println("Nickels: ");
nickels = keyboard.nextInt();
while( nickels <= 1 )
{
System.out.println( "** Invalid Amount **\n" +
"Nickels: " );
nickels = keyboard.nextInt();
}
System.out.println("Enter the change for " + randomNumber1);
System.out.println("Pennies: ");
pennies = keyboard.nextInt();
while( pennies <= 4 )
{
System.out.println( "** Invalid Amount **\n" +
"Pennies: " );
pennies = keyboard.nextInt();
}
while( difference != 0);
{
System.out.println("Incorrect by: ");
}
}
}
最佳答案
这里有一个错误(不确定这是否是唯一的一个):
改变
while( twentyDollars < 0 );
^
{
System.out.println( "** Invalid Amount **\n" +
"Twenties: " );
twentyDollars = keyboard.nextInt();
}
至
while( twentyDollars < 0 )
{
System.out.println( "** Invalid Amount **\n" +
"Twenties: " );
twentyDollars = keyboard.nextInt();
}
while( twentyDollars < 0 );
可能是无限循环(如果 twentyDollars < 0
最初)。
编辑:
这里还有另一个错误:
while( difference != 0);
{
System.out.println("Incorrect by: ");
}
看起来像这样while
意味着结束 do-while 循环。如果是这样的话,您就错过了 }
:
}
while (difference != 0);
目前还不清楚以下内容
{
System.out.println("Incorrect by: ");
}
是打算做的。它与前面的 while 语句没有关系。
关于java - 使用 do-while 循环并找出两个数字之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28892593/