因此对于Java编程来说,它仍然是新手,为此,我需要制作一个程序来计算自动售货机的更改。它基于25美分对1美元之间的5美分增量。对于此分配,如果我使用循环来强制用户在范围内输入值,我会获得额外的功劳,因为我本来是要抓挠的,因为我没有得到它,但很快我就重新使用了它。唯一的事情是我的条件之一,它创建了输出消息的无限循环,我不确定为什么。任何建议将不胜感激
/** Carmine A
The purpose of this program is to calculate the change to be dispensed from
a vending machine */
//import scanner so user can input data
import java.util.Scanner;
public class lab2Test{
//declaration of variables to be used in program
float changeGiven;
public static void main(String args[]) {
//ties user input variable to class so scanner can use it
int userInput;
int itemCost;
//initiates the keyboard to be used
Scanner keyboard = new Scanner(System.in);
//print statement to tell user how to enter price
System.out.println("Enter price of item from 25 cents to a dollar in 5-cent increments \n"
+ "Do not enter a decimal point");
//user inputs value to be set to variable
userInput= keyboard.nextInt();
//System.out.println("You entered ." +userInput + " as the price");
//while loop to make sure input stays in bounds
while(userInput<25 || userInput>100){
System.out.println("Invalid amount entered! \n"
+ "Please enter an amount between 25 cents and 1 dollar");
while(userInput>25 && userInput<100){
System.out.println("Price is in bounds");
System.out.println("Please enter a valid amount between 25-100");
itemCost=keyboard.nextInt();
}
}
itemCost=userInput;
//print out item cost based off users input
System.out.println("You enetered: " + itemCost +" as the items cost");
} }
更新
好,按照你们说的做,做这个
//while loop to make sure input stays in bounds
while(userInput<25 || userInput>100){
System.out.println("Invalid amount entered! \n"
+ "Please enter an amount between 25 cents and 1 dollar");
userInput=keyboard.nextInt();
}
感谢您的帮助!知道这有点愚蠢,但这就是为什么我寻求帮助,以便我可以学习
因此,我相信如果我为同一程序创建另一个线程,我将不予理so,因此我将添加到该线程中。
我几乎完成了我需要的一切,但遇到了一些问题。 1.由于某种原因,在我编译并运行我的代码“ Change Due:”后,由于两次打印,我不确定为什么会这样,因为我在print语句中只有一次,但是我可能会丢失它。 2.我需要以货币格式打印,由于某种原因,我尝试了不同的格式设置选项,但没有一个会变圆(我有这种感觉,但是由于第二次“更改到期日”打印而无法显示),但是可以是错的 3.在第55行上,我收到此消息,但不确定为什么C:\ Users \ finst \ Desktop \ Intro to Java \ Labs \ Lab2 \ lab2Test.java:55:错误:不兼容的类型:可能从双精度转换为int changeRemainder =(changeDue *(double)100);
这是我目前拥有的: /** 胭脂红 该程序的目的是计算要分配的更改 自动售货机* /
//import scanner so user can input data
import java.util.Scanner;
public class lab2Test{
//declaration of variables to be used in program
public static void main(String args[]) {
//ties user input variable to class so scanner can use it
double userInput;
double itemCost;
//initiates the keyboard to be used
Scanner keyboard = new Scanner(System.in);
//print statement to tell user how to enter price
System.out.println("Enter price of item from 25 cents to a dollar in
5-cent increments");
//user inputs value to be set to variable
userInput= keyboard.nextDouble();
//while loop to make sure input stays in bounds
while(userInput<(.25) || userInput>(1.00)){
System.out.println("Invalid amount entered! \n"
+ "Please enter an amount between 25 cents and 1
dollar");
userInput=keyboard.nextDouble();
}
//print out item cost based off users input
System.out.println("You entered: " + userInput +" as the items cost");
System.out.println("You entered a dollar to pay with");
//algorithm to calculate change due
int quarters;
int nickels;
int dimes;
int pennies;
int changeRemainder;
double changeDue;
double dollar=1;
//calculates change due
changeDue= (dollar - userInput);
//System.out.printf("%.2f" + "\n" ,changeDue);
//System.out.println("Change due:" + changeDue);
//makes the remainder into a number that can be used
changeRemainder= (changeDue*(double)100);
//calculates the amount of each coin needed to make the change
quarters= (changeRemainder / 25);
changeRemainder= changeRemainder % 25;
dimes= (changeRemainder/10);
changeRemainder= changeRemainder%10;
nickels=(changeRemainder/5);
changeRemainder= changeRemainder%5;
pennies=(changeRemainder);
//output statement to print coin amounts
System.out.println("Quarters: " + quarters);
System.out.println("Dimes: " + dimes);
System.out.println("Nickels: " + nickels);
System.out.println("Pennies: " + pennies);
} }
答案 0 :(得分:0)
您不需要2个while循环;只有一个会做。无论如何,您的while循环都会检查'userInput'变量,但在while循环中永远不会改变;您将改为更新itemCost变量。
答案 1 :(得分:0)
由于userInput
永远不会在循环中更新,因此其值不会改变,并且有可能永远循环。
也许您的意思是userInput=keyboard.nextInt();
,但无论如何您都不需要两个循环。
userInput=keyboard.nextInt();
while (userInput<25 || userInput>100) {
System.out.println("Invalid amount entered! \n"
+ "Please enter an amount between 25 cents and 1 dollar");
userInput=keyboard.nextInt();
}
答案 2 :(得分:0)
我将为您的while循环使用布尔值,并在其中创建if语句,以检查有效条目并计算更改。希望这会有所帮助!
boolean end = true;
while (end) {
//retrieve user input of cost and assign value to variable
//create an if statement check if the value is valid
if(itemCost >=25 && itemCost <=100){
//retrieve user input for change entered
//calculate change for the user and display it
//end loop
end = false;
} else {
//invalid entry
end = false;
}
}